package com.cf.imes.module.system.controller.admin.pay;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.pay.core.client.dto.order.PayOrderRespDTO;
import com.cf.imes.module.system.controller.admin.pay.vo.PayProductOrderUnifiedReqVO;
import com.cf.imes.module.system.controller.admin.pay.vo.PayRechargeOrderReqVO;
import com.cf.imes.module.system.service.pay.PayOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import java.util.Map;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

/**
 * @author Gqr
 * @since 2025/4/8 16:09
 */
@Tag(name = "管理后台 - 统一支付")
@RestController
@RequestMapping("/system/pay")
@Validated
@Slf4j
public class PayController {

    @Resource
    private PayOrderService payOrderService;

    @PostMapping("/recharge")
    @Operation(summary = "创建充值支付订单")
    public CommonResult<PayOrderRespDTO> recharge(@RequestBody PayRechargeOrderReqVO payRechargeOrderReqVO) {
        PayOrderRespDTO payOrderRespDTO = payOrderService.rechargePay(payRechargeOrderReqVO);
        return success(payOrderRespDTO);
    }

    @PostMapping("/product")
    @Operation(summary = "创建产品支付订单")
    public CommonResult<PayOrderRespDTO> productPay(@RequestBody PayProductOrderUnifiedReqVO payProductOrderUnifiedReqVO) {
        PayOrderRespDTO payOrderRespDTO = payOrderService.productPay(payProductOrderUnifiedReqVO);
        return success(payOrderRespDTO);
    }

    @GetMapping("/order/{channelCode}/{outTradeNo}")
    @Operation(summary = "查询支付订单")
    @Parameter(name = "outTradeNo", description = "支付流水号", required = true, example = "P1O1955")
    @Parameter(name = "channelCode", description = "支付渠道", required = true, example = "0")
    public CommonResult<Boolean> queryPayOrder(@PathVariable("outTradeNo") String outTradeNo, @PathVariable("channelCode") int channelCode) {
        payOrderService.queryPayOrderStatus(outTradeNo, channelCode);
        return success(true);
    }


    @PostMapping(value = "/notify/order/{channelCode}")
    @Operation(summary = "支付渠道回调")
    @PermitAll
    public String notifyOrder(@PathVariable("channelCode") Integer channelCode,
                              @RequestParam(required = false) Map<String, String> params,
                              @RequestBody(required = false) String body,
                              @RequestHeader Map<String, String> headers) {
        log.debug("[notifyOrder][channelCode({}) 回调数据({}/{}/{})]", channelCode, params, body, headers);
        payOrderService.doOrderNotify(channelCode, params, body, headers);
        // 支付接口回调相应必须是"success"
        return "success";
    }
}
