package cokr.xit.ens.modules.common.ctgy.intgrnbill.nv.presentation;

import cokr.xit.ens.modules.common.ctgy.intgrnbill.nv.service.NvPayProcessService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.PayController;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.model.PayApiRespDTO;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "NvPayProcessController")
@Slf4j
@RequiredArgsConstructor
@RestController
public class NvPayProcessController implements PayController<ResponseEntity<PayApiRespDTO>, Map<String, Object>> {

    private final NvPayProcessService nvPayProcessService;


    @Operation(summary = "청구서링크 생성")
    @PostMapping(value = "/bill/nv/ep/gnrurl", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<PayApiRespDTO> gnrUrl(Map<String, Object> param) {
        return new ResponseEntity<PayApiRespDTO>(nvPayProcessService.gnrUrl(param), HttpStatus.OK);
    }

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "결제정보 요청"
                            , value = "{\"billUid\":\"청구서UID\"}")
            })
    })
    @Operation(summary = "납부(결제)정보 요청")
    @PostMapping(value = "/bill/nv/ep/notice", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<PayApiRespDTO> payNotice(@RequestBody Map<String, Object> param) {
        return new ResponseEntity<PayApiRespDTO>(nvPayProcessService.payNotice(param), HttpStatus.OK);
    }

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Success Example..."
                            , summary = "납부(결제)가능여부 확인"
                            , value = "{\"billUid\":\"청구서UID\",\"resultCode\":\"Success\",\"paymentId\":\"네이버페이 결제번호\"}"),
                    @ExampleObject(name = "Fail Example..."
                            , summary = "납부(결제)가능여부 확인"
                            , value = "{\"billUid\":\"청구서UID\",\"resultCode\":\"TimeExpired\",\"resultMessage\":\"결제 시간 초과(reserveId 생성 후 30분을 초과한 경우)\",\"reserveId\":\"결제예약ID\"}")
            })
    })
    @Operation(summary = "납부(결제)가능여부 확인")
    @PostMapping(value = "/bill/nv/ep/able", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<PayApiRespDTO> payAble(@RequestBody Map<String, Object> param) {
        return new ResponseEntity<PayApiRespDTO>(nvPayProcessService.payAble(param), HttpStatus.OK);
    }

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "결과전달"
                            , value = "{\"code\":\"Success\",\"message\":\"detail message(optional)\",\"body\":{\"paymentId\":\"네이버페이 결제번호\",\"detail\":{\"productName\":\"샘플상품\",\"merchantId\":\"loginId\",\"merchantName\":\"가맹점명\",\"cardNo\":\"465887**********\",\"admissionYmdt\":\"20170201151722\",\"payHistId\":\"20170201NP1043587781\",\"totalPayAmount\":1000,\"primaryPayAmount\":1000,\"npointPayAmount\":0,\"giftCardAmount\":0,\"taxScopeAmount\":1000,\"taxExScopeAmount\":0,\"environmentDepositAmount\":0,\"primaryPayMeans\":\"CARD\",\"merchantPayKey\":\"청구서UID\",\"merchantUserKey\":\"빌러유저키\",\"cardCorpCode\":\"C0\",\"paymentId\":\"20170201NP1043587746\",\"admissionTypeCode\":\"01\",\"settleExpectAmount\":971,\"payCommissionAmount\":29,\"admissionState\":\"SUCCESS\",\"tradeConfirmYmdt\":\"20170201152510\",\"cardAuthNo\":\"17545616\",\"cardInstCount\":0,\"usedCardPoint\":false,\"bankCorpCode\":\"\",\"bankAccountNo\":\"\",\"settleExpected\":false,\"extraDeduction\":false,\"useCfmYmdt\":\"20180703\"}}}")
            })
    })
    @Operation(summary = "납부(결제)결과 전달")
    @PostMapping(value = "/bill/nv/ep/result", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<PayApiRespDTO> payResult(@RequestBody Map<String, Object> param) {
        return new ResponseEntity<PayApiRespDTO>(nvPayProcessService.payResult(param), HttpStatus.OK);
    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "결제취소"
                            , value = "{\"billUid\":\"청구서UID\",\"cancelReason\":\"결제취소 테스트\",\"cancelRequester\":\"2\"}")
            })
    })
    @Operation(summary = "결제취소")
    @PostMapping(value = "/bill/nv/ep/cancel", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<PayApiRespDTO> payCancel(@RequestBody Map<String, Object> param) {
        return new ResponseEntity<PayApiRespDTO>(nvPayProcessService.payCancel(param), HttpStatus.OK);
    }
}
