package com.sqx.modules.member.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.member.service.VipService;
import com.sqx.modules.member.vo.VipReduceVO;
import com.sqx.modules.pay.vo.PayTransactionsVO;
import icu.xuyijie.secureapi.annotation.DecryptParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author codingliang
 * @date 2025-11-12
 */
@RestController
@RequestMapping(value = "app/vip")
@RequiredArgsConstructor
public class AppVipController {

    private final VipService vipService;

    @Login
    @ApiOperation(value = "获取用户是否可以会员立减")
    @GetMapping(value = "get-can-reduce/{orderId}")
    public Result getCanReduce(@RequestAttribute("userId") Long userId, @PathVariable("orderId") Long orderId){
        VipReduceVO vipReduceVO = vipService.getVipReduceInfo(userId, orderId);
        return Result.success().put("data", vipReduceVO);
    }

    @Login
    @ApiOperation(value = "获取购买vip支付参数", notes = "promoCode 为推荐码")
    @GetMapping(value = "get-pay-param")
    public Result getPayParam(@RequestAttribute("userId") Long userId, @DecryptParam(value = "promoCode") String promoCode){
        PayTransactionsVO payParamVO = vipService.getPayParam(userId, promoCode);

        return Result.success(BeanUtil.beanToMap(payParamVO, false, true));
    }

    @Login
    @ApiOperation(value = "余额购买vip", notes = "promoCode 为推荐码")
    @GetMapping(value = "balance-pay")
    public Result balancePay(@RequestAttribute("userId") Long userId, @DecryptParam(value = "promoCode") String promoCode){
        vipService.balancePay(userId, promoCode);
        return Result.success();
    }
}
