package shop.alien.lawyer.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.lawyer.payment.PaymentStrategyFactory;


import java.util.Map;

/**
 * @author lyx
 * @version 1.0
 * @date 2025/11/20 17:34
 */
@Slf4j
@Api(tags = {"2.5-支付接口"})
@CrossOrigin
@RestController
@RequestMapping("/payment")
@RequiredArgsConstructor
public class PaymentController {

    private final PaymentStrategyFactory paymentStrategyFactory;


    @ApiOperation("创建预支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "订单金额", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subject", value = "订单标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "payType", value = "支付类型(alipay:支付宝, wechatPay:微信支付)", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping("/prePay")
    public R prePay(String price, String subject, String payType) {
        log.info("PaymentController:prePay, price: {}, subject: {}, payType: {}", price, subject, payType);
        try {
            return paymentStrategyFactory.getStrategy(payType).createPrePayOrder(price, subject);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 通知接口 之后可能会用
     * @param notifyData
     * @return
     */
    @RequestMapping("/notify")
    public R notify(String notifyData) {
        return  null;
    }

    /**
     * 查询订单状态
     * @param transactionId 交易订单号（微信支付订单号/商户订单号）
     * @param payType 支付类型
     * @param id 订单id
     * @param id 订单str
     * @return 订单状态信息
     */
    @RequestMapping("/searchOrderByOutTradeNoPath")
    public R searchOrderByOutTradeNoPath(String transactionId, String payType ,Integer id ,String orderStr) {
        try {
            return paymentStrategyFactory.getStrategy(payType).searchOrderByOutTradeNoPath(transactionId,id,orderStr);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 退款接口
     * @param params 退款参数（包含订单号、退款金额等）
     * @return 退款结果
     */
    @RequestMapping("/refunds")
    public R refunds(@RequestBody Map<String, String> params) {
        try {
            return R.data(paymentStrategyFactory.getStrategy(params.get("payType")).handleRefund(params));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
