package top.continew.admin.controller.business;

/**
 * 用户充值消费历史记录管理 API
 *
 * @author jiay
 * @since 2025/05/23 07:51
 */

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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 top.continew.admin.business.model.query.ConsumptionHistoryQuery;
import top.continew.admin.business.model.req.ConsumptionHistoryAddReq;
import top.continew.admin.business.model.req.ConsumptionHistoryEditReq;
import top.continew.admin.business.model.req.ConsumptionReq;
import top.continew.admin.business.model.resp.ConsumptionHistoryResp;
import top.continew.admin.business.service.ConsumptionHistoryService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

@Tag(name = "用户充值消费历史记录管理 API")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/consumptionHistory")
public class ConsumptionHistoryController {

    private final ConsumptionHistoryService consumptionHistoryService;

    @Operation(summary = "用户充值消费历史记录", description = "用户充值消费历史记录")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Valid ConsumptionHistoryAddReq req) {
        return consumptionHistoryService.addConsumptionHistory(req) ? R.ok() : R.fail();
    }

    @Operation(summary = "编辑用户充值消费历史记录", description = "编辑用户充值消费历史记录")
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody @Valid ConsumptionHistoryEditReq req) {
        return consumptionHistoryService.editConsumptionHistory(req) ? R.ok() : R.fail();
    }

    @Operation(summary = "删除用户充值消费历史记录", description = "删除用户充值消费历史记录")
    @DeleteMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        return consumptionHistoryService.deleteConsumptionHistory(id) ? R.ok() : R.fail();
    }

    @Operation(summary = "获取用户充值消费历史记录", description = "获取用户充值消费历史记录")
    @GetMapping("/get")
    public R<ConsumptionHistoryResp> getConsumptionHistory(@RequestParam(name = "id", required = true) Long id) {
        return R.ok(consumptionHistoryService.getConsumptionHistory(id));
    }

    @Operation(summary = "查询用户充值消费历史记录列表", description = "查询用户充值消费历史记录列表")
    @GetMapping("/list")
    public R<List<ConsumptionHistoryResp>> getConsumptionHistoryList(ConsumptionHistoryQuery query) {
        return R.ok(consumptionHistoryService.getConsumptionHistoryList(query));
    }

    @Operation(summary = "分页查询用户充值消费历史记录列表", description = "分页查询用户充值消费历史记录列表")
    @GetMapping
    public R<PageResp<ConsumptionHistoryResp>> page(ConsumptionHistoryQuery query, @Validated PageQuery pageQuery) {
        return R.ok(consumptionHistoryService.getConsumptionHistoryPage(query, pageQuery));
    }

    @Operation(summary = "判断余额是否充足", description = "判断余额是否充足")
    @GetMapping("/checkBalance")
    public R checkBalance(ConsumptionReq req) {
        return consumptionHistoryService.checkBalance(req);
    }
}