package top.continew.admin.controller.business;

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.PlantformBalanceQuery;
import top.continew.admin.business.model.req.CheckPlantBalanceReq;
import top.continew.admin.business.model.req.PlantformBalanceAddReq;
import top.continew.admin.business.model.req.PlantformBalanceEditReq;
import top.continew.admin.business.model.resp.PlantformBalanceResp;
import top.continew.admin.business.service.PlantformBalanceService;
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;
import cn.dev33.satoken.stp.StpUtil;

/**
 * 用户子账号余额 API
 *
 * @author jiay
 * @since 2025/06/24 16:47
 */
@Tag(name = "用户子账号余额 API")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/plantformBalance")
public class PlantformBalanceController {
    private final PlantformBalanceService plantformBalanceService;

    @Operation(summary = "添加用户子账号余额", description = "添加用户子账号余额")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Valid PlantformBalanceAddReq req) {
        return R.ok(plantformBalanceService.addPlantformBalance(req));
    }

    @Operation(summary = "编辑用户子账号余额", description = "编辑用户子账号余额")
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody @Valid PlantformBalanceEditReq req) {
        return plantformBalanceService.editPlantformBalance(req) ? R.ok() : R.fail();
    }

    @Operation(summary = "删除用户子账号余额", description = "删除用户子账号余额")
    @DeleteMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        return plantformBalanceService.deletePlantformBalance(id) ? R.ok() : R.fail();
    }

    @Operation(summary = "获取用户子账号余额", description = "获取用户子账号余额")
    @GetMapping("/get")
    public R<PlantformBalanceResp> getPlantformBalance(@RequestParam(name = "id", required = true) Long id) {
        return R.ok(plantformBalanceService.getPlantformBalance(id));
    }

    @Operation(summary = "查询用户子账号余额列表", description = "查询用户子账号余额列表")
    @GetMapping("/list")
    public R<List<PlantformBalanceResp>> getPlantformBalanceList(PlantformBalanceQuery query) {
        return R.ok(plantformBalanceService.getPlantformBalanceList(query));
    }

    @Operation(summary = "分页查询用户子账号余额列表", description = "分页查询用户子账号余额列表")
    @GetMapping
    public R<PageResp<PlantformBalanceResp>> page(PlantformBalanceQuery query, @Validated PageQuery pageQuery) {
        return R.ok(plantformBalanceService.getPlantformBalancePage(query, pageQuery));
    }

    @Operation(summary = "添加子账户余额", description = "添加子账户余额")
    @GetMapping("/addPlantBalance")
    public R addPlantBalance(CheckPlantBalanceReq req) {
        return plantformBalanceService.checkPlantBalance(null);
    }

    @Operation(summary = "减少子账户余额", description = "减少子账户余额")
    @GetMapping("/reducePlantBalance")
    public R reducePlantBalance(CheckPlantBalanceReq req) {
        return plantformBalanceService.checkPlantBalance(null);
    }

    @Operation(summary = "获取当前用户子账户余额列表", description = "根据token解析userId，获取用户所有子账户余额，按taskType区分")
    @GetMapping("/userBalances")
    public R<List<PlantformBalanceResp>> getUserBalances() {
        Long userId = StpUtil.getLoginIdAsLong();
        return R.ok(plantformBalanceService.getUserBalancesByUserId(userId));
    }

    @Operation(summary = "判断平台子账户余额是否充足", description = "判断平台子账户余额是否充足")
    @GetMapping("/checkPlantBalance")
    public R checkPlantBalance(CheckPlantBalanceReq req) {
        return plantformBalanceService.checkPlantBalance(req);
    }
}