package com.hotwj.platform.pay.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.hotwj.platform.pay.controller.vo.PayRechargeChannelRespVO;
import com.hotwj.platform.pay.controller.vo.PayRechargeStatusRespVO;
import com.hotwj.platform.pay.controller.vo.PayRechargeUnifiedOrderReqVO;
import com.hotwj.platform.pay.controller.vo.PayRechargeUnifiedOrderRespVO;
import com.hotwj.platform.pay.controller.vo.PayWalletBalanceRespVO;
import com.hotwj.platform.pay.service.PayRechargeOpenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.springframework.http.MediaType;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/pay/open")
@Tag(name = "开放支付", description = "钱包充值支付能力")
public class PayOpenController {

    private final PayRechargeOpenService payRechargeOpenService;

    @GetMapping("/recharge/channels")
    @Operation(summary = "查询钱包充值可用支付方式")
    public R<List<PayRechargeChannelRespVO>> listRechargeChannels() {
        return R.ok(payRechargeOpenService.listRechargeChannels());
    }

    @GetMapping("/wallet/balance")
    @Operation(summary = "查询钱包余额")
    public R<PayWalletBalanceRespVO> queryWalletBalance(@RequestParam(value = "walletType", required = false) Long walletType) {
        return R.ok(payRechargeOpenService.queryWalletBalance(walletType));
    }

    @PostMapping("/recharge/unified-order")
    @Operation(summary = "钱包充值统一下单")
    public R<PayRechargeUnifiedOrderRespVO> unifiedOrder(@Valid @RequestBody PayRechargeUnifiedOrderReqVO reqVO,
                                                         HttpServletRequest request) {
        return R.ok(payRechargeOpenService.unifiedOrder(reqVO, request));
    }

    @GetMapping("/recharge/status/{rechargeOrderNo}")
    @Operation(summary = "查询钱包充值状态")
    public R<PayRechargeStatusRespVO> queryRechargeStatus(@PathVariable String rechargeOrderNo) {
        return R.ok(payRechargeOpenService.queryRechargeStatus(rechargeOrderNo));
    }

    @SaIgnore
    @PostMapping(value = "/notify/alipay/{channelId}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @Operation(summary = "支付宝支付回调")
    public String alipayNotify(@PathVariable Long channelId, @RequestParam Map<String, String> params) {
        return payRechargeOpenService.handleAlipayNotify(channelId, params);
    }

    @SaIgnore
    @PostMapping(value = "/notify/wechat/{channelId}", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "微信支付回调")
    public String wechatNotify(@PathVariable Long channelId, @RequestBody String body, HttpServletRequest request) {
        return payRechargeOpenService.handleWechatNotify(channelId, body, request);
    }
}
