/**
 * @description AI文件上传相关API
 * @author AI Assistant
 * @since 2025-11-04
 */

import { api } from '@/apis/index';
import type { AiUploadFile, ResultDomain, FileUploadResponse, PageParam } from '@/types';
/**
 * 文件上传API服务
 */
export const fileUploadApi = {
  /**
   * 上传文件用于对话（图文多模态）
   * @param file 文件对象
   * @param agentId 智能体ID
   * @returns Promise<ResultDomain<Record<string, any>>> 返回Dify文件信息
   */
  async uploadFileForChat(file: File, agentId: string): Promise<ResultDomain<Record<string, any>>> {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('agentId', agentId);

    // 关闭加载提示，避免影响用户体验
    const response = await api.post<Record<string, any>>('/ai/file/upload-for-chat', formData, {
      showLoading: false
    });
    return response.data;
  },

  /**
   * 上传单个文件到知识库
   * @param knowledgeId 知识库ID
   * @param file 文件对象
   * @returns Promise<ResultDomain<FileUploadResponse>>
   */
  async uploadFile(knowledgeId: string, file: File): Promise<ResultDomain<FileUploadResponse>> {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('knowledgeId', knowledgeId);

    const response = await api.post<FileUploadResponse>('/ai/file/upload', formData, {
      showLoading: false
    });
    return response.data;
  },

  /**
   * 批量上传文件
   * @param knowledgeId 知识库ID
   * @param files 文件列表
   * @returns Promise<ResultDomain<FileUploadResponse[]>>
   */
  async batchUploadFiles(knowledgeId: string, files: File[]): Promise<ResultDomain<FileUploadResponse[]>> {
    const formData = new FormData();
    files.forEach(file => {
      formData.append('files', file);
    });
    formData.append('knowledgeId', knowledgeId);

    const response = await api.post<FileUploadResponse[]>('/ai/file/batch-upload', formData, {
      showLoading: false
    });
    return response.data;
  },

  /**
   * 删除文件
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteFile(fileId: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/ai/file/${fileId}`);
    return response.data;
  },

  /**
   * 获取文件详情
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<AiUploadFile>>
   */
  async getFileById(fileId: string): Promise<ResultDomain<AiUploadFile>> {
    const response = await api.get<AiUploadFile>(`/ai/file/${fileId}`);
    return response.data;
  },

  /**
   * 获取知识库的文件列表
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<AiUploadFile[]>>
   */
  async listFilesByKnowledge(knowledgeId: string): Promise<ResultDomain<AiUploadFile>> {
    const response = await api.get<AiUploadFile>('/ai/file/list', {
       knowledgeId 
    });
    return response.data;
  },

  /**
   * 分页查询文件
   * @param filter 过滤条件
   * @param pageParam 分页参数
   * @returns Promise<ResultDomain<AiUploadFile>>
   */
  async pageFiles(filter: Partial<AiUploadFile>, pageParam: PageParam): Promise<ResultDomain<AiUploadFile>> {
    const response = await api.post<AiUploadFile>('/ai/file/page', {
      filter,
      pageParam
    });
    return response.data;
  },

  /**
   * 同步文件状态（从Dify）
   * @param fileId 文件ID
   * @returns Promise<ResultDomain<AiUploadFile>>
   */
  async syncFileStatus(fileId: string): Promise<ResultDomain<AiUploadFile>> {
    const response = await api.post<AiUploadFile>(`/ai/file/${fileId}/sync`);
    return response.data;
  },

  /**
   * 批量同步文件状态
   * @param fileIds 文件ID列表
   * @returns Promise<ResultDomain<number>>
   */
  async batchSyncFileStatus(fileIds: string[]): Promise<ResultDomain<number>> {
    const response = await api.post<number>('/ai/file/batch-sync', { fileIds });
    return response.data;
  },

  /**
   * 查询消息关联的文件列表
   * @param messageId 消息ID
   * @returns Promise<ResultDomain<AiUploadFile[]>>
   */
  async listFilesByMessage(messageId: string): Promise<ResultDomain<AiUploadFile[]>> {
    const response = await api.get<AiUploadFile[]>(`/ai/file/message/${messageId}`, {},{
      showLoading: false
    });
    return response.data;
  },

  /**
   * 更新文档启用/禁用状态
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param enabled 是否启用
   * @returns Promise<ResultDomain<Boolean>>
   */
  async updateDocumentStatus(
    datasetId: string,
    documentId: string,
    enabled: boolean
  ): Promise<ResultDomain<Boolean>> {
    const action = enabled ? 'enable' : 'disable';
    const response = await api.post<boolean>(
      `/ai/dify/datasets/${datasetId}/documents/status/${action}`,
      { document_ids: [documentId] }
    );
    return response.data;
  }
};
