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.AiChatRecordBo;
import org.dromara.ai.domain.vo.AiChatRecordVo;
import org.dromara.ai.service.AiChatRecordService;
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/chat/record")
public class AdminAiChatRecordController extends BaseController {

    private final AiChatRecordService aiChatRecordService;

    /**
     * 查询AI聊天记录列表
     */
    @Operation(summary = "查询AI聊天记录列表", description = "分页查询AI聊天记录列表，支持多条件搜索")
    @SaCheckPermission("ai:chat:record:list")
    @GetMapping("/list")
    public TableDataInfo<AiChatRecordVo> list(@Parameter(description = "聊天记录查询条件") AiChatRecordBo bo,
                                               @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiChatRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI聊天记录列表
     */
    @Operation(summary = "导出AI聊天记录列表", description = "导出AI聊天记录到Excel")
    @SaCheckPermission("ai:chat:record:export")
    @Log(title = "AI聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "聊天记录查询条件") AiChatRecordBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiChatRecordVo> list = aiChatRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI聊天记录", AiChatRecordVo.class, response);
    }

    /**
     * 获取AI聊天记录详细信息
     */
    @Operation(summary = "获取AI聊天记录详情", description = "根据记录ID获取详细信息")
    @SaCheckPermission("ai:chat:record:query")
    @GetMapping("/{id}")
    public R<AiChatRecordVo> getInfo(@Parameter(description = "记录ID", required = true)
                                      @NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(aiChatRecordService.queryById(id));
    }

    /**
     * 批量删除AI聊天记录
     */
    @Operation(summary = "批量删除AI聊天记录", description = "根据ID数组批量删除AI聊天记录")
    @SaCheckPermission("ai:chat:record: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(aiChatRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 归档聊天记录（删除指定日期之前的记录）
     */
    @Operation(summary = "归档聊天记录", description = "删除指定日期之前的聊天记录")
    @SaCheckPermission("ai:chat:record:remove")
    @Log(title = "AI聊天记录归档", businessType = BusinessType.DELETE)
    @PostMapping("/archive")
    public R<Integer> archive(@Parameter(description = "归档日期（删除此日期之前的记录）", required = true)
                               @RequestParam java.util.Date beforeDate) {
        Integer count = aiChatRecordService.archiveChatRecords(beforeDate);
        return R.ok(count);
    }

}
