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

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.nvsigntalk.domain.SendDetailNvSigntalk;
import cokr.xit.ens.modules.nvsigntalk.domain.repository.SendDetailNvSigntalkRepository;
import cokr.xit.ens.modules.nvsigntalk.model.NvSigntalkAcceptReqDTO;
import cokr.xit.ens.modules.nvsigntalk.service.NvSigntalkService;
import io.swagger.v3.oas.annotations.Operation;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

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

    private final NvSigntalkService nvSigntalkService;
    private final SendDetailNvSigntalkRepository sendDetailNvSigntalkRepository;


    @Operation(summary = "접수")
    @PostMapping(value = "/nv/signtalk/accept", produces = MediaType.APPLICATION_JSON_VALUE)
//    public ResponseEntity<EnsResponseVO> accept(@Validated @RequestBody NvSigntalkAcceptReqDTO reqDTO, BindingResult bindingResult){
    public ResponseEntity<EnsResponseVO> accept(@RequestBody NvSigntalkAcceptReqDTO reqDTO) {

//        EnsResponseVO responseVO = null;
//        if (bindingResult.hasErrors()) {
//            List<String> errors = bindingResult.getAllErrors().stream().map(e -> e.getDefaultMessage()).collect(Collectors.toList());
//            responseVO = EnsResponseVO.errRsltBuilder()
//                    .errCode(EnsErrCd.ACPT410)
//                    .errMsg("유효하지 않은 요청값 입니다.")
//                    .resultInfo(errors)
//                    .build();
//        }else{
//            responseVO = NvSigntalkService.accept(reqDTO);
//        }
        EnsResponseVO responseVO = nvSigntalkService.accept(reqDTO);


        return new ResponseEntity<EnsResponseVO>(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 = "제작")
//    @PutMapping(value = "/nv/signtalk/make", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/nv/signtalk/make", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> make(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO responseVO = nvSigntalkService.remake(mParam.get("sendMastId"));

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


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

        EnsResponseVO responseVO = nvSigntalkService.makeAll();

        return new ResponseEntity<EnsResponseVO>(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 = "/nv/signtalk/send/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> sendBulk(@RequestBody Map<String, Long> mParam) {
        EnsResponseVO responseVO = nvSigntalkService.sendBulk(mParam.get("sendMastId"));

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

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

        return new ResponseEntity<EnsResponseVO>(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 = "(대량)문서상태 갱신")
//    @PutMapping(value = "/nv/signtalk/stat/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/nv/signtalk/stat/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> statBulk(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO responseVO = nvSigntalkService.statBulk(mParam.get("sendMastId"));

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

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

        EnsResponseVO responseVO = nvSigntalkService.statBulkAll();

        return new ResponseEntity<EnsResponseVO>(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 = "{\"doc_id\": \"VSf450HzAVaGxSJwbHjdGKfulx5PX4jT2x4WOEIn1fNzyuo8a4_fXzooeamPNhIB\", \"nonce\": \"6pQQqoykcO75WPpj\", \"client_doc_id\": \"A0001\"}")
            })
    })
    @Operation(summary = "토큰(OTT) 검증")
    @PostMapping(value = "/nv/signtalk/token/verify", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> tokenVerify(@RequestBody Map<String, String> mParam) {

        final String docId = mParam.get("doc_id");
        final String nonce = mParam.get("nonce");
        final String clientDocId = mParam.get("client_doc_id");

        SendDetailNvSigntalk sendDetail = sendDetailNvSigntalkRepository.findFetchByClientDocIdAndDocId(clientDocId, docId)
                .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));

        EnsResponseVO responseVO = nvSigntalkService.tokenVerify(sendDetail.getSendMast().getOrgCd(), docId, nonce, clientDocId);

        return new ResponseEntity<EnsResponseVO>(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 = "/nv/signtalk/send/result/provide", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> sendResultProvide(@RequestBody Map<String, Long> mParam) {

        EnsResponseVO responseVO = nvSigntalkService.sendResultProvide(mParam.get("sendMastId"));


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

    @Operation(summary = "ping 체크")
    @GetMapping(value = "/nv/signtalk/ping", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> ping() {

        EnsResponseVO responseVO = nvSigntalkService.ping();

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