package shop.alien.store.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.*;
import shop.alien.entity.result.R;
import shop.alien.store.service.MerchantPaymentQueryService;
import shop.alien.store.strategy.merchantPayment.MerchantPaymentStrategyFactory;

import java.util.Map;

/**
 * 商户支付接口（预订订单 user_reservation_order，使用 StorePaymentConfig 按门店配置）
 * 与 /payment 现有接口隔离，不影响原有支付接口。
 *
 * @author system
 */
@Slf4j
@Api(tags = {"商户支付（预订订单）"})
@RestController
@RequestMapping("/merchantPayment")
@RequiredArgsConstructor
public class MerchantPaymentController {

    private final MerchantPaymentStrategyFactory merchantPaymentStrategyFactory;
    private final MerchantPaymentQueryService merchantPaymentQueryService;

    @ApiOperation("预订订单-创建预支付（支付宝，使用门店支付配置）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "orderId", value = "预订订单ID user_reservation_order.id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "amountYuan", value = "支付金额（元）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "subject", value = "订单描述/商品标题", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "payType", value = "支付类型 alipay", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/prePay")
    public R<Map<String, Object>> prePay(
            @RequestParam Integer storeId,
            @RequestParam Integer orderId,
            @RequestParam String amountYuan,
            @RequestParam String subject,
            @RequestParam Integer userId,
            @RequestParam(defaultValue = "alipay") String payType,
            @RequestParam String smid) {
        log.info("MerchantPaymentController.prePay storeId={}, orderId={}, payType={}", storeId, orderId, payType);
        return merchantPaymentStrategyFactory.getStrategy(payType).createPrePay(storeId, orderId, amountYuan, subject, userId, smid);
    }

    @ApiOperation("预订订单-查询支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "outTradeNo", value = "商户订单号（预支付返回）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "payType", value = "支付类型 alipay", required = true, paramType = "query", dataType = "String")
    })
    @GetMapping("/queryStatus")
    public R<Object> queryStatus(
            @RequestParam Integer storeId,
            @RequestParam String outTradeNo,
            @RequestParam(defaultValue = "alipay") String payType) {
        log.info("MerchantPaymentController.queryStatus storeId={}, outTradeNo={}", storeId, outTradeNo);
        return merchantPaymentQueryService.queryPayStatusWithRetry(storeId, outTradeNo, payType);
    }

    @ApiOperation("预订订单-退款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "outTradeNo", value = "商户订单号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "refundAmount", value = "退款金额（元）", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "refundType", value = "退款类型 0:用户取消 1:商家退款 2:部分退款等 3.扫码核销成功", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "refundReason", value = "退款原因", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "payType", value = "支付类型 alipay", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/refund")
    public R<String> refund(
            @RequestParam Integer storeId,
            @RequestParam String outTradeNo,
            @RequestParam String refundAmount,
            @RequestParam Integer refundType,
            @RequestParam(required = false) String refundReason,
            @RequestParam(defaultValue = "alipay") String payType) {
        log.info("MerchantPaymentController.refund storeId={}, outTradeNo={}", storeId, outTradeNo);
        return merchantPaymentStrategyFactory.getStrategy(payType).refund(storeId, outTradeNo, refundAmount, refundReason, refundType);
    }
}
