package cokr.xit.ens.modules.common.ctgy.intgrnbill.support;

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.common.ctgy.intgrnbill.support.service.repair.PayRepairServiceFactory;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.repair.model.RepairPayRsltFwdFailSearchDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;

@Tag(name = "PayRepairController")
@Slf4j
@RestController
@RequiredArgsConstructor
public class PayRepairController {
    private final PayRepairServiceFactory payRepairServiceFactory;

    @Parameters({
            @Parameter(name = "paySe", example = "kko", required = true, in = ParameterIn.PATH),
    })
    @Operation(summary = "납부(결제)결과전달 실패자료 조회")
    @PostMapping(value = "/bill/pay/repair/{paySe}/trsf/fail/payresult/find", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findTransferFailData(@PathVariable String paySe, @RequestBody RepairPayRsltFwdFailSearchDTO searchDTO) {

        EnsResponseVO<Map<String, Object>> result = null;
        try {
           
            if (Arrays.stream(PayRepairServiceFactory.PRepairPaySe.values()).noneMatch(pTriggerPaySe -> pTriggerPaySe.name().equals(paySe)))
                throw new EnsException(EnsErrCd.INVALID_REQUEST_URL, EnsErrCd.INVALID_REQUEST_URL.getCodeNm());
            final PayRepairServiceFactory.PRepairPaySe pTriggerPaySe = PayRepairServiceFactory.PRepairPaySe.valueOf(paySe);

            result = EnsResponseVO.<Map<String, Object>>okBuilder()
                    .resultInfo((Map<String, Object>) payRepairServiceFactory.getService(pTriggerPaySe).findTransferFailedPayResultDataList(searchDTO))
                    .build();
        } catch (EnsException e) {
            result = EnsResponseVO.<Map<String, Object>>errBuilder()
                    .errCode(e.getErrCd())
                    .errMsg(e.getMessage())
                    .build();
        }
        return new ResponseEntity<EnsResponseVO<Map<String, Object>>>(result, HttpStatus.OK);
    }

    @Parameters({
            @Parameter(name = "paySe", example = "kko", required = true, in = ParameterIn.PATH),
    })
    @Operation(summary = "납부(결제)결과전달 실패자료 전송")
    @PostMapping(value = "/bill/pay/repair/{paySe}/trsf/fail/payresult", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity sendTransferFailData(@PathVariable String paySe, @RequestBody RepairPayRsltFwdFailSearchDTO searchDTO) {

        EnsResponseVO<Map<String, Object>> result = null;
        try {
           
            if (Arrays.stream(PayRepairServiceFactory.PRepairPaySe.values()).noneMatch(pTriggerPaySe -> pTriggerPaySe.name().equals(paySe)))
                throw new EnsException(EnsErrCd.INVALID_REQUEST_URL, EnsErrCd.INVALID_REQUEST_URL.getCodeNm());
            final PayRepairServiceFactory.PRepairPaySe pTriggerPaySe = PayRepairServiceFactory.PRepairPaySe.valueOf(paySe);

            result = EnsResponseVO.<Map<String, Object>>okBuilder()
                    .resultInfo((Map<String, Object>) payRepairServiceFactory.getService(pTriggerPaySe).sendTransferFailedPayResultDataList(searchDTO))
                    .build();
        } catch (EnsException e) {
            result = EnsResponseVO.<Map<String, Object>>errBuilder()
                    .errCode(e.getErrCd())
                    .errMsg(e.getMessage())
                    .build();
        }
        return new ResponseEntity<EnsResponseVO<Map<String, Object>>>(result, HttpStatus.OK);
    }
}
