package com.madeu.crm.settings.code.ctrl;

import com.madeu.constants.Constants;
import com.madeu.util.HttpUtil;
import com.madeu.crm.settings.code.service.CodeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/settings/code")
public class CodeController {

    private final CodeService codeService;

    /**
     * 코드 관리 메인 화면 이동
     */
    @RequestMapping("/codeList.do")
    public ModelAndView codeList(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController codeList START");
        log.debug("CodeController codeList END");

        return new ModelAndView("/web/settings/code/codeList");
    }

    /**
     * 코드 그룹 리스트 조회
     */
    @PostMapping("/getCodeGroupList.do")
    public HashMap<String, Object> getCodeGroupList(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController getCodeGroupList START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.getCodeGroupList(paramMap);
        } catch (Exception e) {
            log.error("getCodeGroupList : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    /**
     * 코드 그룹 상세 조회
     */
    @PostMapping("/getCodeGroup.do")
    public HashMap<String, Object> getCodeGroup(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController getCodeGroup START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        return codeService.getCodeGroup(paramMap);
    }

    /**
     * 코드 그룹 저장 (신규/수정)
     */
    @PostMapping("/saveCodeGroup.do")
    public HashMap<String, Object> saveCodeGroup(@RequestBody HashMap<String, Object> paramMap,
            HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController saveCodeGroup START");
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.saveCodeGroup(paramMap);
        } catch (Exception e) {
            log.error("saveCodeGroup : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    /**
     * 코드 그룹 삭제
     */
    @PostMapping("/deleteCodeGroup.do")
    public HashMap<String, Object> deleteCodeGroup(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController deleteCodeGroup START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.deleteCodeGroup(paramMap);
        } catch (Exception e) {
            log.error("deleteCodeGroup : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    // ===================================
    // 상세 코드 관련
    // ===================================

    /**
     * 상세 코드 리스트 조회
     */
    @PostMapping("/getCodeList.do")
    public HashMap<String, Object> getCodeList(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController getCodeList START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.getCodeList(paramMap);
        } catch (Exception e) {
            log.error("getCodeList : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    /**
     * 상세 코드 상세 조회
     */
    @PostMapping("/getCode.do")
    public HashMap<String, Object> getCode(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController getCode START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        return codeService.getCode(paramMap);
    }

    /**
     * 상세 코드 저장 (신규/수정)
     */
    @PostMapping("/saveCode.do")
    public HashMap<String, Object> saveCode(@RequestBody HashMap<String, Object> paramMap, HttpServletRequest request,
            HttpServletResponse response) {
        log.debug("CodeController saveCode START");
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.saveCode(paramMap);
        } catch (Exception e) {
            log.error("saveCode : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    /**
     * 상세 코드 삭제
     */
    @PostMapping("/deleteCode.do")
    public HashMap<String, Object> deleteCode(HttpServletRequest request, HttpServletResponse response) {
        log.debug("CodeController deleteCode START");
        HashMap<String, Object> paramMap = HttpUtil.getParameterMap(request);
        HashMap<String, Object> map = new HashMap<>();

        try {
            map = codeService.deleteCode(paramMap);
        } catch (Exception e) {
            log.error("deleteCode : ", e);
            map.put("msgCode", Constants.FAIL);
            map.put("msgDesc", "서버 오류가 발생했습니다.");
        }
        return map;
    }

    // ===================================
    // 팝업 화면 라우팅
    // ===================================

    @RequestMapping("/codeGroupPop.do")
    public ModelAndView codeGroupPop(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/web/settings/code/popup/codeGroupPop");
    }

    @RequestMapping("/codePop.do")
    public ModelAndView codePop(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("/web/settings/code/popup/codePop");
    }
}
