package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiUserQuotaBo;
import org.dromara.ai.domain.vo.AiUserQuotaVo;
import org.dromara.ai.service.AiUserQuotaService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * AI用户配额管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI用户配额管理", description = "AI用户配额管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/user/quota")
public class AdminAiUserQuotaController extends BaseController {

    private final AiUserQuotaService aiUserQuotaService;

    /**
     * 查询AI用户配额列表
     */
    @Operation(summary = "查询AI用户配额列表", description = "分页查询AI用户配额列表，支持多条件搜索")
    @SaCheckPermission("ai:user:quota:list")
    @GetMapping("/list")
    public TableDataInfo<AiUserQuotaVo> list(@Parameter(description = "配额查询条件") AiUserQuotaBo bo,
                                              @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiUserQuotaService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI用户配额列表
     */
    @Operation(summary = "导出AI用户配额列表", description = "导出AI用户配额到Excel")
    @SaCheckPermission("ai:user:quota:export")
    @Log(title = "AI用户配额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "配额查询条件") AiUserQuotaBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiUserQuotaVo> list = aiUserQuotaService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI用户配额", AiUserQuotaVo.class, response);
    }

    /**
     * 获取AI用户配额详细信息
     */
    @Operation(summary = "获取AI用户配额详情", description = "根据配额ID获取详细信息")
    @SaCheckPermission("ai:user:quota:query")
    @GetMapping("/{id}")
    public R<AiUserQuotaVo> getInfo(@Parameter(description = "配额ID", required = true)
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(aiUserQuotaService.queryById(id));
    }

    /**
     * 根据用户ID获取配额信息
     */
    @Operation(summary = "根据用户ID获取配额信息", description = "根据用户ID获取配额信息")
    @SaCheckPermission("ai:user:quota:query")
    @GetMapping("/user/{userId}")
    public R<AiUserQuotaVo> getByUserId(@Parameter(description = "用户ID", required = true)
                                         @NotNull(message = "用户ID不能为空")
                                         @PathVariable Long userId) {
        return R.ok(aiUserQuotaService.queryByUserId(userId));
    }

    /**
     * 新增AI用户配额
     */
    @Operation(summary = "新增AI用户配额", description = "新增AI用户配额")
    @SaCheckPermission("ai:user:quota:add")
    @Log(title = "AI用户配额", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Valid @RequestBody AiUserQuotaBo bo) {
        return toAjax(aiUserQuotaService.insertByBo(bo));
    }

    /**
     * 修改AI用户配额
     */
    @Operation(summary = "修改AI用户配额", description = "修改AI用户配额")
    @SaCheckPermission("ai:user:quota:edit")
    @Log(title = "AI用户配额", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Valid @RequestBody AiUserQuotaBo bo) {
        return toAjax(aiUserQuotaService.updateByBo(bo));
    }

    /**
     * 批量删除AI用户配额
     */
    @Operation(summary = "批量删除AI用户配额", description = "根据ID数组批量删除AI用户配额")
    @SaCheckPermission("ai:user:quota:remove")
    @Log(title = "AI用户配额", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "配额ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiUserQuotaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 重置用户配额
     */
    @Operation(summary = "重置用户配额", description = "重置用户的日配额或月配额")
    @SaCheckPermission("ai:user:quota:edit")
    @Log(title = "AI用户配额", businessType = BusinessType.UPDATE)
    @PostMapping("/reset/{userId}")
    public R<Void> resetQuota(@Parameter(description = "用户ID", required = true)
                               @NotNull(message = "用户ID不能为空")
                               @PathVariable Long userId,
                               @Parameter(description = "重置类型(DAILY/MONTHLY)", required = true)
                               @RequestParam String resetType) {
        return toAjax(aiUserQuotaService.resetQuota(userId, resetType));
    }

}
