package com.seer.teach.pay.app.notify.controller;

import cn.hutool.core.util.StrUtil;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.pay.app.pay.service.IAppPayOrderService;
import com.seer.teach.pay.app.refund.service.IAppRefundOrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 支付回调控制器
 */
@Slf4j
@RestController
@RequestMapping("/app/callback")
@Tag(name = "支付回调接口")
@RequiredArgsConstructor
public class AppPayCallbackController {

    private final IAppPayOrderService appPayOrderService;
    private final IAppRefundOrderService appRefundOrderService;

    @Operation(summary = "支付回调")
    @PostMapping("/pay/{channelCode}")
    @LogPrint
    public ResponseEntity<String> payCallback(
            @RequestHeader Map<String, String> headers,
            @RequestParam(required = false) Map<String, String> params,
            @RequestBody(required = false) String body,
            @PathVariable("channelCode") String channelCode) {
        
        log.info("收到支付回调请求，渠道：{}，请求头：{}，请求体：{}", channelCode, headers, body);
        
        // 校验渠道编码
        if (StrUtil.isBlank(channelCode)) {
            log.warn("渠道编码不能为空");
            return ResponseEntity.badRequest().body("渠道编码不能为空");
        }
        
        // 使用统一的处理方法
        boolean handleResult = appPayOrderService.payCallback(channelCode, headers,params, body);
        
        if (handleResult) {
            log.info("支付回调处理成功，渠道：{}", channelCode);
            // 返回成功响应
            return ResponseEntity.ok("success");
        } else {
            log.warn("支付回调处理失败，渠道：{}", channelCode);
            return ResponseEntity.ok("fail");
        }
    }

    @Operation(summary = "退款回调")
    @PostMapping("/refund/{channelCode}")
    @LogPrint
    public ResponseEntity<String> refundCallback(
            @RequestHeader Map<String, String> headers,
            @RequestParam(required = false) Map<String, String> params,
            @RequestBody(required = false) String body,
            @PathVariable("channelCode") String channelCode) {
        log.info("收到退款回调请求，渠道：{}，请求头：{}，请求体：{}", channelCode, headers, body);
        
        // 校验渠道编码
        if (StrUtil.isBlank(channelCode)) {
            log.warn("渠道编码不能为空");
            return ResponseEntity.badRequest().body("渠道编码不能为空");
        }
        
        // 使用统一的处理方法
        boolean handleResult = appRefundOrderService.refundCallback(channelCode, headers,params, body);
        if (handleResult) {
            log.info("支付回调处理成功，渠道：{}", channelCode);
            // 返回成功响应
            return ResponseEntity.ok("success");
        } else {
            log.warn("支付回调处理失败，渠道：{}", channelCode);
            return ResponseEntity.ok("fail");
        }
    }
}