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

import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.model.KkoPayNoticeRespData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.model.KkoPayUrlRespData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.PayUseSysController;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.code.PayUseSysResCd;
import com.google.gson.Gson;
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.HashMap;
import java.util.Map;

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

    private Gson gson = new Gson();


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "청구서링크생성 정보"
                            , value = "{\"data\":{\"billerUserKey\":\"빌러유저키\"}}")
            })
    })
    @Operation(summary = "청구서링크생성 정보 샘플")
    @PostMapping(value = "/bill/kko/sample/gnrUrl", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity gnrUrl(@RequestBody Map<String, Object> param) {
        log.info("[Request Data] {}", param.toString());


        /* ============================================
        * [정상 응답코드]
        *   OK 
        * [실패 응답코드]
        *   GRNURL_FIND_FAILED   
        ============================================ */
        PayUseSysResCd resCd = PayUseSysResCd.OK;

        
        String expireAt = "20231231235959";
        KkoPayUrlRespData data = KkoPayUrlRespData.builder()
                .billedYearMonth(null)
                .ordinal(null)
                .expireAt(expireAt)
                .parameters(null)
                .build();
        Map<String, Object> result = new HashMap<>();
        result.put("res_code", resCd.getCode());
        result.put("message", resCd.getCodeNm());
        result.put("data", data);

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }

    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "납부(결제)정보"
                            , value = "{\"data\":{\"biller_user_key\":\"빌러유저키\",\"user_birth\":\"\",\"parameters\":{\"some_param\":\"\"}}}"),
            })
    })
    @Operation(summary = "납부(결제)정보 샘플")
    @PostMapping(value = "/bill/kko/sample/paynotice", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<Map<String, Object>> payNotice(@RequestBody Map<String, Object> param) {
        log.info("[Request Data] {}", param.toString());



        /* ============================================
        * [정상 응답코드]
        *   OK 
        * [실패 응답코드]
        *   NOT_FOUND_PAYDATA   
        *   PAYDATA_FIND_FAILED   
        ============================================ */
        PayUseSysResCd resCd = PayUseSysResCd.OK;

        
        String payExpireDate = "20231231"; 
        KkoPayNoticeRespData data = KkoPayNoticeRespData.builder()
                .title("테스트 청구서")
                .amount(1000)
                .taxFreeAmount(null)
                .vatAmount(null)
                .expireType("D1")
                .payExpireDate(payExpireDate)
                .secondPayExpireDate(null)
                .bankAccounts(null)
                .details(null)
                .build();
        Map<String, Object> result = new HashMap<>();
        result.put("res_code", resCd.getCode());
        result.put("message", resCd.getCodeNm());
        result.put("data", data);

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "납부(결제)가능여부"
                            , value = "{\"data\":{\"biller_user_key\":\"빌러유저키\",\"amount\":29000,\"parameters\":{\"pay_rqt_seq\":\"KT00000001\"}}}")
            })
    })
    @Operation(summary = "납부(결제)가능여부 샘플")
    @PostMapping(value = "/bill/kko/sample/payable", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<Map<String, Object>> payAble(@RequestBody Map<String, Object> param) {
        log.info("[Request Data] {}", param.toString());


        /* ============================================
        * [정상 응답코드]
        *   OK 
        * [실패 응답코드]
        *   NO_AMOUNT_PAYABLE   
        *   OVERPAYABLE_AMOUNT  
        *   NO_TARGET_PAYABLE   
        *   EXPIRED_DATE        
        *   FAILED_CHECK_INFO   
        ============================================ */
        PayUseSysResCd resCd = PayUseSysResCd.OK;

        
        Map<String, Object> result = new HashMap<>();
        result.put("res_code", resCd.getCode());
        result.put("message", resCd.getCodeNm());
        result.put("data", null);


        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }


    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
            @Content(mediaType = "application/json", examples = {
                    @ExampleObject(name = "Sample Example..."
                            , summary = "납부(결제)결과"
                            , value = "{\"data\":{\"biller_user_key\":\"빌러유저키\",\"parameters\":{\"pay_rqt_seq\":\"KT00000001\"},\"pay_by\":\"MONEY\",\"pay_type\":\"P\",\"pay_detail1\":\"\",\"pay_detail2\":\"\",\"amount\":20000,\"pay_amount\":20000,\"pay_fee_type\":\"BEFORE\",\"pay_fee\":150,\"pay_fee_tax\":15,\"paid_at\":\"20190220161656\",\"pay_id\":2748877}}")
            })
    })
    @Operation(summary = "납부(결제)결과 샘플")
    @PostMapping(value = "/bill/kko/sample/payresult", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<Map<String, Object>> payResult(@RequestBody Map<String, Object> param) {
        log.info("[Request Data] {}", param.toString());

        PayUseSysResCd resCd = PayUseSysResCd.OK;

        
        Map<String, Object> result = new HashMap<>();
        result.put("res_code", resCd.getCode());
        result.put("message", resCd.getCodeNm());
        result.put("data", null);

        return new ResponseEntity<Map<String, Object>>(result, HttpStatus.OK);
    }


    //    @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, content = {
//            @Content(mediaType = "application/json", examples = {
//                    @ExampleObject(name = "Sample Example..."
//                            , summary = "결제취소정보"
//                            , value = "{}")
//            })
//    })
    @Operation(summary = "결제취소정보 샘플")
    @PostMapping(value = "/bill/kko/sample/cancelresult", produces = MediaType.APPLICATION_JSON_VALUE)
    @Override
    public ResponseEntity<Map<String, Object>> payCancel(Map<String, Object> param) {
        throw new EnsException(EnsErrCd.SERVICE_NOT_SUPPORTED, EnsErrCd.SERVICE_NOT_SUPPORTED.getCodeNm());
    }

}
