package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.coze.CozeApiCallPageRequest;
import com.emotion.dto.request.coze.CozeApiCallCreateRequest;
import com.emotion.dto.request.coze.CozeApiCallUpdateRequest;
import com.emotion.dto.response.coze.CozeApiCallResponse;
import com.emotion.service.CozeApiCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * Coze API调用记录控制器
 * 
 * @author huazhongmin
 * @date 2025-07-23
 */
@RestController
@RequestMapping("/coze-api-call")
public class CozeApiCallController {

    @Autowired
    private CozeApiCallService cozeApiCallService;

    /**
     * 分页查询API调用记录
     */
    @GetMapping(value = "/page")
    public Result<PageResult<CozeApiCallResponse>> getCozeApiCallPage(@Validated CozeApiCallPageRequest request) {
        PageResult<CozeApiCallResponse> pageResult = cozeApiCallService.getPage(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取API调用记录
     */
    @GetMapping(value = "/detail")
    public Result<CozeApiCallResponse> getCozeApiCallById(@RequestParam String id) {
        CozeApiCallResponse response = cozeApiCallService.getById(id);
        if (response == null) {
            return Result.notFound("API调用记录不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建API调用记录
     */
    @PostMapping(value = "/create")
    public Result<CozeApiCallResponse> createCozeApiCall(@RequestBody @Validated CozeApiCallCreateRequest request) {
        CozeApiCallResponse response = cozeApiCallService.create(request);
        return Result.success(response);
    }

    /**
     * 更新API调用记录
     */
    @PutMapping(value = "/update")
    public Result<CozeApiCallResponse> updateCozeApiCall(@RequestBody @Validated CozeApiCallUpdateRequest request) {
        CozeApiCallResponse response = cozeApiCallService.update(request);
        if (response == null) {
            return Result.error("更新失败，记录不存在");
        }
        return Result.success(response);
    }

    /**
     * 删除API调用记录
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> deleteCozeApiCall(@RequestParam String id) {
        boolean deleted = cozeApiCallService.delete(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 统计用户的API调用次数
     */
    @GetMapping(value = "/countByUser")
    public Result<Long> countByUserId(@RequestParam String userId) {
        Long count = cozeApiCallService.countByUserId(userId);
        return Result.success(count);
    }

    /**
     * 统计Bot的API调用次数
     */
    @GetMapping(value = "/countByBot")
    public Result<Long> countByBotId(@RequestParam String botId) {
        Long count = cozeApiCallService.countByBotId(botId);
        return Result.success(count);
    }

    /**
     * 统计指定状态的API调用次数
     */
    @GetMapping(value = "/countByStatus")
    public Result<Long> countByStatus(@RequestParam String status) {
        Long count = cozeApiCallService.countByStatus(status);
        return Result.success(count);
    }

    /**
     * 统计用户的Token使用量
     */
    @GetMapping(value = "/tokensByUser")
    public Result<Long> sumTokensByUserId(@RequestParam String userId) {
        Long totalTokens = cozeApiCallService.sumTokensByUserId(userId);
        return Result.success(totalTokens);
    }

    /**
     * 统计用户的API调用费用
     */
    @GetMapping(value = "/costByUser")
    public Result<BigDecimal> sumCostByUserId(@RequestParam String userId) {
        BigDecimal totalCost = cozeApiCallService.sumCostByUserId(userId);
        return Result.success(totalCost);
    }
}