package cn.reghao.tnb.user.app.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.user.app.model.dto.BuyVip;
import cn.reghao.tnb.user.app.model.vo.VipPlan;
import cn.reghao.tnb.user.app.model.vo.MyVip;
import cn.reghao.tnb.user.app.service.UserVipService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-06-22 14:14:09
 */
@Tag(name = "用户会员接口")
@RestController
@RequestMapping("/api/user/vip")
public class UserVipController {
    private final UserVipService userVipService;

    public UserVipController(UserVipService userVipService) {
        this.userVipService = userVipService;
    }

    @Operation(summary = "获取用户的会员信息", description = "N")
    @AuthUser
    @GetMapping("/my")
    public String getMyVip() {
        long loginUser = UserContext.getUserId();
        MyVip myVip = userVipService.getMyVip(loginUser);
        return WebResult.success(myVip);
    }

    @Operation(summary = "获取会员计划", description = "N")
    @AuthUser
    @GetMapping("/plan")
    public String getVipPlan() {
        List<VipPlan> list = userVipService.getVipPlans();
        return WebResult.success(list);
    }

    @Operation(summary = "购买会员", description = "N")
    @AuthUser
    @PostMapping("/buy")
    public String buyVip(@RequestBody @Validated BuyVip buyVip) throws Exception {
        Result result = userVipService.buyVip(buyVip);
        return WebResult.result(result);
    }
}
