package cokr.xit.ens.modules.ktsigntalk.direct.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.ktsigntalk.direct.domain.SendDetailKtSigntalk;
import cokr.xit.ens.modules.ktsigntalk.direct.model.KtSigntalkAcceptReqDTO;
import cokr.xit.ens.modules.ktsigntalk.direct.service.KtSigntalkService;
import cokr.xit.ens.modules.ktsigntalk.direct.domain.repository.SendDetailKtSigntalkRepository;
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.*;

import java.util.Map;

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

    private final KtSigntalkService ktSigntalkService;
    private final SendDetailKtSigntalkRepository sendDetailKtSigntalkRepository;


    @Operation(summary = "접수")
    @PostMapping(value = "/kt/signtalk/accept", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> accept(@RequestBody KtSigntalkAcceptReqDTO reqDTO) {

        EnsResponseVO responseVO = ktSigntalkService.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 = "/kt/signtalk/make", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/kt/signtalk/make", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> make(@RequestBody Map<String, Long> mParam) {

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

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


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

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

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

    @Operation(summary = "(대량)전송요청 일괄")
    @PostMapping(value = "/kt/signtalk/send/bulk/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> sendBulkAll() {
        EnsResponseVO responseVO = ktSigntalkService.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 = "/kt/signtalk/stat/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/kt/signtalk/stat/bulk", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> statBulk(@RequestBody Map<String, Long> mParam) {

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

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

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

        EnsResponseVO responseVO = ktSigntalkService.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 = "{\"src_key\": \"BIN-ff806328863311ebb61432ac599d6150\", \"token\": \"ZDU3NGU1ZTYtNTk2Yy99OGNjLWJiYjUtYjZmMjI9NzRhOTA4\", \"external_document_uuid\": \"A0001\"}")
            })
    })
    @Operation(summary = "토큰(OTT) 검증")
    @PostMapping(value = "/kt/signtalk/token/verify", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<EnsResponseVO> tokenVerify(@RequestBody Map<String, String> mParam) {

        final String srcKey = mParam.get("src_key");
        final String token = mParam.get("token");


        SendDetailKtSigntalk sendDetail = sendDetailKtSigntalkRepository.findFetchByClientSrcKeyAndSrcKey(null, srcKey)
                .orElseThrow(() -> new EnsException(EnsErrCd.ERR404, "문서를 찾을 수 없습니다."));

        EnsResponseVO responseVO = ktSigntalkService.tokenVerify(sendDetail.getSendMast().getOrgCd(), srcKey, token);

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

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


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

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

        EnsResponseVO responseVO = ktSigntalkService.ping(accessToken);

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