package top.continew.admin.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import top.continew.admin.business.model.entity.TaskRecordHistoryDO;
import top.continew.admin.business.model.req.*;
import top.continew.admin.business.model.resp.BillingDeductResp;
import top.continew.admin.business.model.resp.TaskCallbackResp;
import top.continew.admin.business.service.BillingService;

/**
 * 计费控制器
 *
 * @author jiay
 * @since 2025/08/28 00:00
 */
@Tag(name = "计费管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/billing")
public class BillingController {

    private final BillingService billingService;

    @Operation(summary = "第三方平台计费预扣减（本地计算费用）", description = "第三方平台计费预扣减接口，本地根据计费规则计算预扣减金额")
    @PostMapping("/judgeBalance")
    public BillingDeductResp judgeBalance(@RequestBody @Valid BillingJudgeReq req) {
        return billingService.judgeBalance(req);
    }

    @Operation(summary = "第三方平台计费预扣减（第三方传入费用）", description = "第三方平台计费预扣减接口，使用第三方平台传入的预扣减金额")
    @PostMapping("/judgeBalanceWithAmount")
    public BillingDeductResp judgeBalanceWithAmount(@RequestBody @Valid BillingJudgeWithAmountReq req) {
        return billingService.judgeBalanceWithAmount(req);
    }

    @Operation(summary = "任务完成回调（本地计算费用）", description = "第三方平台任务完成后回调接口，本地根据计费规则计算实际消费金额")
    @PostMapping("/callback")
    public TaskCallbackResp taskCallback(@RequestBody @Valid TaskCallbackReq req) {
        return billingService.taskCallback(req);
    }

    @Operation(summary = "查询任务集合", description = "根据根任务ID查询任务集合")
    @PostMapping("/queryTaskListByRootId")
    public Page<TaskRecordHistoryDO> queryTaskListByRootId(@RequestBody @Valid TaskQueryReq req) {
        return billingService.queryTaskListByRootId(req);
    }

    @Operation(summary = "任务完成回调（第三方传入费用）", description = "第三方平台任务完成后回调接口，使用第三方平台传入的实际消费金额")
    @PostMapping("/callbackWithAmount")
    public TaskCallbackResp taskCallbackWithAmount(@RequestBody @Valid TaskCallbackWithAmountReq req) {
        return billingService.taskCallbackWithAmount(req);
    }
}