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

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.user.api.dto.ChargeReq;
import cn.reghao.tnb.user.api.iface.UserWalletService;
import cn.reghao.tnb.user.app.model.dto.WalletDto;
import cn.reghao.tnb.user.app.model.po.WalletBill;
import cn.reghao.tnb.user.app.model.vo.MyWallet;
import cn.reghao.tnb.user.app.service.WalletService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-04-16 16:15:04
 */
@Tag(name = "用户钱包接口")
@RestController
@RequestMapping("/api/user/wallet")
public class UserWalletController {
    private final UserWalletService userWalletService;
    private final WalletService walletService;

    public UserWalletController(UserWalletService userWalletService, WalletService walletService) {
        this.userWalletService = userWalletService;
        this.walletService = walletService;
    }

    @Operation(summary = "获取用户钱包", description = "N")
    @AuthUser
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUserWallet() {
        MyWallet myWallet = walletService.getMyWallet();
        return WebResult.success(myWallet);
    }

    @Operation(summary = "获取钱包账单", description = "N")
    @AuthUser
    @GetMapping(value = "/bill", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getWalletBill() {
        int pageNumber = 1;
        List<WalletBill> list = walletService.getWalletBillList(pageNumber);
        return WebResult.success(list);
    }

    @Operation(summary = "向钱包充值", description = "N")
    @AuthUser
    @PostMapping("/charge")
    public String chargeWallet(@RequestBody @Validated ChargeReq chargeReq) {
        userWalletService.charge(chargeReq);
        return WebResult.success();
    }

    @Operation(summary = "使用钱包支付", description = "N")
    @AuthUser
    @PostMapping("/pay")
    public String pay(@RequestBody @Validated WalletDto walletDto) throws Exception {
        double quantity = 1.2;
        long loginUser = UserContext.getUserId();
        walletService.pay(quantity, loginUser);
        return WebResult.success();
    }
}
