package cokr.xit.ens.modules.common.ctgy.sys.mng.presentation;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.TmpltMngDTO;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.TmpltMngSearchDTO;
import cokr.xit.ens.modules.common.ctgy.sys.mng.service.TmpltMngService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "TmpltMngController")
@Slf4j
@RequiredArgsConstructor
@RestController
public class TmpltMngController {

    private final TmpltMngService tmpltMngService;

    @Operation(summary = "템플릿(타입별) 목록 조회")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "dType"),
    })
    @GetMapping(value = "/sys/mng/tmplt/{dType}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO<List<? extends TmpltMngDTO>>> findAll(@PathVariable String dType, TmpltMngSearchDTO searchDTO) {
        EnsResponseVO<List<? extends TmpltMngDTO>> responseVO = tmpltMngService.findAllBySearchDTO(searchDTO, dType);

        return new ResponseEntity<EnsResponseVO<List<? extends TmpltMngDTO>>>(responseVO, HttpStatus.OK);
    }


    @Operation(summary = "템플릿(타입별) 조회")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "dType"),
            @Parameter(in = ParameterIn.PATH, name = "orgCd"),
            @Parameter(in = ParameterIn.PATH, name = "tmpltCd"),
    })
    @GetMapping(value = "/sys/mng/tmplt/{dType}/{orgCd}/{tmpltCd}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO<? extends TmpltMngDTO>> find(@PathVariable String dType, @PathVariable String orgCd, @PathVariable String tmpltCd) {
        TmpltMngSearchDTO searchDTO = TmpltMngSearchDTO.builder()
                .schOrgCd(orgCd)
                .schTmpltCd(tmpltCd)
                .build();
        EnsResponseVO<? extends TmpltMngDTO> responseVO = tmpltMngService.find(orgCd, tmpltCd, dType);

        return new ResponseEntity<EnsResponseVO<? extends TmpltMngDTO>>(responseVO, HttpStatus.OK);
    }


    @Operation(summary = "템플릿 신규 등록")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "dType"),
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Example_01"
                            , summary = "기본(basic)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT001\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\"}"),
                    @ExampleObject(name = "Example_02"
                            , summary = "카카오 알림톡(kkoat)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT002\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"biztalkOrgCd\":\"\",\"biztalkTmpltCode\":\"CEPHIS_001\"}"),
                    @ExampleObject(name = "Example_03"
                            , summary = "카카오 내문서함(kkomd)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT003\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"csNumber\":\"02-123-1234\",\"csName\":\"고객센터\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"}"),
                    @ExampleObject(name = "Example_04"
                            , summary = "네이버 고지서(nvst)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT004\",\"title\":\"테스트 템플릿\",\"message\":\"null...\",\"callCenterNo\":\"02-1234-1234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\",\"messageDataFmt\":{\"data\":[{\"수신인\":\"#{RECV_NAME}\"},{\"주소\":\"#{ADDRESS}\"},{\"과태료\":\"#{AMOUNT}\"},{\"전화번호\":\"#{CALL_CENTER_NO}\"}]}}"),
                    @ExampleObject(name = "Example_05"
                            , summary = "통합고지(intgrn)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT005\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"try1\":\"kkoMydoc\",\"try2\":\"nvSigntalk\",\"try3\":\"kkoAlimtalk\",\"kkoAlimtalk\":{\"biztalkOrgCd\":\"\",\"biztalkTmpltCode\":\"CEPHIS_001\"},\"kkoMydoc\":{\"csNumber\":\"02-123-4567\",\"csName\":\"고객센터\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"},\"nvSigntalk\":{\"callCenterNo\":\"02-123-1234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\",\"messageDataFmt\":{\"data\":[{\"수신인\":\"#{RECV_NAME}\"},{\"주소\":\"#{ADDRESS}\"},{\"과태료\":\"#{AMOUNT}\"},{\"전화번호\":\"#{CALL_CENTER_NO}\"}]}},\"ktSigntalk\":{\"sndTelNo\":\"0212345678\",\"sendTel\":\"0256781234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"},\"ktGibis\":{\"msgCd\":\"MC001\",\"mtype\":\"mms\",\"optType\":1,\"sndTelNo\":\"044-211-3377\",\"sendTel\":\"044-211-3377\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"Y\"}}"),
                    @ExampleObject(name = "Example_06"
                            , summary = "KT 인증톡(ktst)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT006\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"sndTelNo\":\"0212345678\",\"sendTel\":\"0256781234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"}"),
                    @ExampleObject(name = "Example_07"
                            , summary = "KT 인증톡(ktgbs)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT007\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"msgCd\":\"MC001\",\"mtype\":\"mms\",\"optType\":1,\"sndTelNo\":\"044-211-3377\",\"sendTel\":\"044-211-3377\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"Y\"}")
            })
    })
    @PostMapping(value = "/sys/mng/tmplt/{dType}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> add(@PathVariable String dType, @RequestBody Map<String, Object> mParam) {
        EnsResponseVO responseVO = tmpltMngService.add(mParam, dType);

        return new ResponseEntity<EnsResponseVO>(responseVO, HttpStatus.OK);
    }


    @Operation(summary = "템플릿 수정")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "dType"),
    })
    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Example_01"
                            , summary = "기본(basic)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT001\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\"}"),
                    @ExampleObject(name = "Example_02"
                            , summary = "카카오 알림톡(kkoat)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT002\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"biztalkOrgCd\":\"\",\"biztalkTmpltCode\":\"CEPHIS_001\"}"),
                    @ExampleObject(name = "Example_03"
                            , summary = "카카오 내문서함(kkomd)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT003\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"csNumber\":\"02-123-1234\",\"csName\":\"고객센터\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"}"),
                    @ExampleObject(name = "Example_04"
                            , summary = "네이버 고지서(nvst)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT004\",\"title\":\"테스트 템플릿\",\"message\":\"null...\",\"callCenterNo\":\"02-1234-1234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\",\"messageDataFmt\":{\"data\":[{\"수신인\":\"#{RECV_NAME}\"},{\"주소\":\"#{ADDRESS}\"},{\"과태료\":\"#{AMOUNT}\"},{\"전화번호\":\"#{CALL_CENTER_NO}\"}]}}"),
                    @ExampleObject(name = "Example_05"
                            , summary = "통합고지(intgrn)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT005\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"try1\":\"kkoMydoc\",\"try2\":\"nvSigntalk\",\"try3\":\"kkoAlimtalk\",\"kkoAlimtalk\":{\"biztalkOrgCd\":\"\",\"biztalkTmpltCode\":\"CEPHIS_001\"},\"kkoMydoc\":{\"csNumber\":\"02-123-4567\",\"csName\":\"고객센터\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"},\"nvSigntalk\":{\"callCenterNo\":\"02-123-1234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\",\"messageDataFmt\":{\"data\":[{\"수신인\":\"#{RECV_NAME}\"},{\"주소\":\"#{ADDRESS}\"},{\"과태료\":\"#{AMOUNT}\"},{\"전화번호\":\"#{CALL_CENTER_NO}\"}]}},\"ktSigntalk\":{\"sndTelNo\":\"0212345678\",\"sendTel\":\"0256781234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"},\"ktGibis\":{\"msgCd\":\"MC001\",\"mtype\":\"mms\",\"optType\":1,\"sndTelNo\":\"044-211-3377\",\"sendTel\":\"044-211-3377\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"Y\"}}"),
                    @ExampleObject(name = "Example_06"
                            , summary = "KT 인증톡(ktst)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT006\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"sndTelNo\":\"0212345678\",\"sendTel\":\"0256781234\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"N\"}"),
                    @ExampleObject(name = "Example_07"
                            , summary = "KT 인증톡(ktgbs)"
                            , value = "{\"orgCd\":\"EX_ORG001\",\"tmpltCd\":\"EX_TMPLT007\",\"title\":\"테스트 템플릿\",\"message\":\"해당 안내문은 다음과 같습니다.\",\"msgCd\":\"MC001\",\"mtype\":\"mms\",\"optType\":1,\"sndTelNo\":\"044-211-3377\",\"sendTel\":\"044-211-3377\",\"ciTransUseYn\":\"Y\",\"tmpltMsgUseYn\":\"Y\",\"tmpltCsInfoUseYn\":\"Y\"}")
            })
    })
    @PutMapping(value = "/sys/mng/tmplt/{dType}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> modify(@PathVariable String dType, @RequestBody Map<String, Object> mParam) {
        EnsResponseVO responseVO = tmpltMngService.modify(mParam, dType);

        return new ResponseEntity<EnsResponseVO>(responseVO, HttpStatus.OK);
    }


    @Operation(summary = "템플릿 삭제")
    @DeleteMapping(value = "/sys/mng/tmplt/{orgCd}/{tmpltCd}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> delete(@PathVariable String orgCd, @PathVariable String tmpltCd) {
        EnsResponseVO responseVO = tmpltMngService.delete(orgCd, tmpltCd);

        return new ResponseEntity<EnsResponseVO>(responseVO, HttpStatus.OK);
    }

}
