package cokr.xit.ens.modules.kkotalk.presentation;

import java.util.*;

import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import cokr.xit.ens.core.aop.*;
import cokr.xit.ens.core.exception.*;
import cokr.xit.ens.core.exception.code.*;
import cokr.xit.ens.modules.kkotalk.mapper.*;
import cokr.xit.ens.modules.kkotalk.model.*;
import cokr.xit.ens.modules.kkotalk.service.*;
import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.media.*;
import io.swagger.v3.oas.annotations.tags.*;
import lombok.*;
import lombok.extern.slf4j.*;

/**
 * <pre>
 * description : 카카오톡 전자 문서 발송 controller
 * packageName : kr.xit.ens.kakao.talk.web
 * fileName    : KkotalkEltrcDocController
 * author      : julim
 * date        : 2024-08-12
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2024-08-12    julim       최초 생성
 *
 * </pre>
 */
@Tag(name = "KkotalkController", description = "카카오톡 인증톡")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/kko/talk")
public class KkotalkController {
    private final KkoTalkService service;
    private final IKkoTalkMapper talkMapper;

    @Operation(summary = "접수")
    @PostMapping(value = "/accept", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> accept(@RequestBody KkotalkDTO.KkoTalkAcceptReqDTO reqDTO) {
        EnsResponseVO<?> responseVO = service.accept(reqDTO);
        return new ResponseEntity<>(responseVO, HttpStatus.OK);
    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "제작"
                , value = "{\"sendMastId\": 1}")
        })
    })
    @Operation(summary = "제작")
    @PostMapping(value = "/make", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> make(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO<?> responseVO = service.remake(mParam.get("sendMastId"));

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


    @Operation(summary = "제작(일괄)")
    //    @PutMapping(value = "/kko/mydoc/make/all", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/make/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> makeAll() {

        EnsResponseVO<?> responseVO = service.makeAll();

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


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "(대량)전송요청"
                , value = "{\"sendMastId\": 1}")
        })
    })
    @Operation(summary = "(대량)전송요청")
    @PostMapping(value = "/send/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> sendBulk(@RequestBody Map<String, Long> mParam) {
        EnsResponseVO<?> responseVO = service.sendBulk(mParam.get("sendMastId"));

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

    @Operation(summary = "(대량)전송요청 일괄")
    @PostMapping(value = "/send/bulk/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> sendBulkAll() {
        EnsResponseVO<?> responseVO = service.sendBulkAll();

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


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "(대량)문서상태 갱신"
                , value = "{\"sendMastId\": 1}")
        })
    })
    @Operation(summary = "(대량)문서상태 갱신")
    @PostMapping(value = "/stat/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> statBulk(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO<?> responseVO = service.statBulk(mParam.get("sendMastId"));

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

    @Operation(summary = "(대량)문서상태 일괄 갱신")
    //    @PutMapping(value = "/kko/mydoc/stat/bulk/all", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/stat/bulk/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> statBulkAll() {

        EnsResponseVO<?> responseVO = service.statBulkAll();

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


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "토큰(OTT) 검증"
                , value = "{\"envelopeId\": \"BIN-ff806328863311ebb61432ac599d6150\", \"token\": \"ZDU3NGU1ZTYtNTk2Yy99OGNjLWJiYjUtYjZmMjI9NzRhOTA4\", \"externalId\": \"A0001\"}")
        })
    })
    @Operation(summary = "토큰(OTT) 검증")
    @PostMapping(value = "/token/verify", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> tokenVerify(@RequestBody Map<String, String> mParam) {

        final String envelopeId = mParam.get("envelopeId");
        final String token = mParam.get("token");
        final String externalId = mParam.get("externalId");


        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("envelopeId", envelopeId);
        paramMap.put("externalId", externalId);
        SendDetailKkoTalkDTO sendDetail = talkMapper.findFetchByExternalIdAndEnvelopeId(paramMap)
            .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));

        EnsResponseVO<?> responseVO = service.tokenVerify("getOrgCd()", envelopeId, token, externalId);

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

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "문서열람 완료처리"
                , value = "{\"envelopeId\": \"BIN-ff806328863311ebb61432ac599d6150\"}")
        })
    })
    @Operation(summary = "문서열람 완료처리")
    @PostMapping(value = "/read/cmplt", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> readCmplt(@RequestBody Map<String, String> mParam) {

        final String envelopeId = mParam.get("envelopeId");

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("envelopeId", envelopeId);
        paramMap.put("externalId", null);
        SendDetailKkoTalkDTO sendDetail = talkMapper.findFetchByExternalIdAndEnvelopeId(paramMap)
            .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));

        EnsResponseVO<?> responseVO = service.readCmplt("getOrgCd()", envelopeId);


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

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
        @Content(mediaType = "application/json", examples = {
            @ExampleObject(name = "Sample Example..."
                , summary = "전송결과 가져오기"
                , value = "{\"sendMastId\": 1}")
        })
    })
    @Operation(summary = "전송결과 가져오기")
    @PostMapping(value = "/send/result/provide", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> sendResultProvide(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO<?> responseVO = service.sendResultProvide(mParam.get("sendMastId"));


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