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.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.AiProviderConfigHistoryBo;
import org.dromara.ai.domain.vo.AiProviderConfigHistoryVo;
import org.dromara.ai.service.AiProviderConfigHistoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * AI Provider配置历史管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI Provider配置历史管理", description = "AI Provider配置历史管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/provider/config/history")
public class AdminAiProviderConfigHistoryController extends BaseController {

    private final AiProviderConfigHistoryService aiProviderConfigHistoryService;

    /**
     * 查询AI Provider配置历史列表
     */
    @Operation(summary = "查询AI Provider配置历史列表", description = "分页查询AI Provider配置历史列表，支持多条件搜索")
    @SaCheckPermission("ai:provider:config:history:list")
    @GetMapping("/list")
    public TableDataInfo<AiProviderConfigHistoryVo> list(@Parameter(description = "配置历史查询条件") AiProviderConfigHistoryBo bo,
                                                          @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiProviderConfigHistoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI Provider配置历史列表
     */
    @Operation(summary = "导出AI Provider配置历史列表", description = "导出AI Provider配置历史到Excel")
    @SaCheckPermission("ai:provider:config:history:export")
    @Log(title = "AI Provider配置历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "配置历史查询条件") AiProviderConfigHistoryBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiProviderConfigHistoryVo> list = aiProviderConfigHistoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI Provider配置历史", AiProviderConfigHistoryVo.class, response);
    }

    /**
     * 获取AI Provider配置历史详细信息
     */
    @Operation(summary = "获取AI Provider配置历史详情", description = "根据记录ID获取详细信息")
    @SaCheckPermission("ai:provider:config:history:query")
    @GetMapping("/{id}")
    public R<AiProviderConfigHistoryVo> getInfo(@Parameter(description = "记录ID", required = true)
                                                 @NotNull(message = "主键不能为空")
                                                 @PathVariable Long id) {
        return R.ok(aiProviderConfigHistoryService.queryById(id));
    }

    /**
     * 根据配置ID查询历史记录列表
     */
    @Operation(summary = "根据配置ID查询历史记录", description = "根据配置ID查询该配置的所有历史记录")
    @SaCheckPermission("ai:provider:config:history:query")
    @GetMapping("/config/{configId}")
    public R<List<AiProviderConfigHistoryVo>> getHistoryByConfigId(@Parameter(description = "配置ID", required = true)
                                                                    @NotNull(message = "配置ID不能为空")
                                                                    @PathVariable Long configId) {
        return R.ok(aiProviderConfigHistoryService.queryByConfigId(configId));
    }

    /**
     * 批量删除AI Provider配置历史
     */
    @Operation(summary = "批量删除AI Provider配置历史", description = "根据ID数组批量删除AI Provider配置历史")
    @SaCheckPermission("ai:provider:config:history:remove")
    @Log(title = "AI Provider配置历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "记录ID数组", required = true)
                          @NotNull(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(aiProviderConfigHistoryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

}