package com.madeu.crm.callLog.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.madeu.common.service.MenuAuthService;
import com.madeu.constants.Constants;
import com.madeu.crm.callLog.dto.CallLogDTO;
import com.madeu.crm.callLog.dto.CallLogSearchDTO;
import com.madeu.crm.callLog.dto.CallLogStatsDTO;
import com.madeu.crm.callLog.dto.CallMemoDTO;
import com.madeu.crm.callLog.map.CallLogMAP;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CallLogService {

    @Autowired
    private CallLogMAP callLogMAP;

    @Autowired
    private MenuAuthService menuAuthService;

    /**
     * 통화 로그 화면 이동
     */
    public HashMap<String, Object> moveCallLogList(HashMap<String, Object> paramMap) throws Exception {
        log.debug("moveCallLogList START");

        HashMap<String, Object> map = menuAuthService.getMenuAuthority(paramMap);

        log.debug("loginMemberId : " + paramMap.get("loginMemberId"));
        log.debug("menuClass     : " + paramMap.get("menuClass"));
        log.debug("moveCallLogList END");
        return map;
    }

    /**
     * 통화 로그 목록 조회 (통계 포함)
     */
    public HashMap<String, Object> getCallLogList(CallLogSearchDTO searchDTO) throws Exception {
        log.debug("getCallLogList START");

        HashMap<String, Object> map = new HashMap<>();

        List<CallLogDTO> listMap = new ArrayList<>();

        int totalCount = callLogMAP.getCallLogCnt(searchDTO);

        if (0 < totalCount) {
            listMap = callLogMAP.getCallLogList(searchDTO);
        }

        // 통계 집계
        CallLogStatsDTO stats = callLogMAP.getCallLogStats(searchDTO);

        map.put("msgCode", Constants.OK);
        map.put("success", "true");
        map.put("totalCount", totalCount);
        map.put("rows", listMap);
        map.put("stats", stats);

        log.debug("getCallLogList END");
        return map;
    }

    /**
     * 통화 메모 저장
     */
    public HashMap<String, Object> saveCallMemo(CallMemoDTO memoDTO) throws Exception {
        log.debug("saveCallMemo START");

        HashMap<String, Object> map = new HashMap<>();

        int existCnt = callLogMAP.getCallMemoCnt(memoDTO);
        int result = 0;

        if (existCnt > 0) {
            // 기존 메모 수정
            result = callLogMAP.modCallMemo(memoDTO);
        } else {
            // 신규 메모 등록
            result = callLogMAP.saveCallMemo(memoDTO);
        }

        if (0 < result) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
            map.put("msgDesc", "메모가 저장되었습니다.");
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
            map.put("msgDesc", "메모 저장에 실패하였습니다.");
        }

        log.debug("saveCallMemo END");
        return map;
    }

    /**
     * CTI 로그 저장 (Webhook에서 호출)
     */
    public HashMap<String, Object> putCtiLog(CallLogDTO logDTO) throws Exception {
        log.debug("putCtiLog START");

        HashMap<String, Object> map = new HashMap<>();

        int result = callLogMAP.putCtiLog(logDTO);

        if (0 < result) {
            map.put("msgCode", Constants.OK);
            map.put("success", "true");
        } else {
            map.put("msgCode", Constants.FAIL);
            map.put("success", "false");
        }

        log.debug("putCtiLog END");
        return map;
    }

    /**
     * CTI 로그 회원 매핑
     */
    public void updateCtiLogMember(HashMap<String, Object> paramMap) throws Exception {
        callLogMAP.modCtiLogMember(paramMap);
    }
}
