/**
 * @description AI知识库相关API
 * @author AI Assistant
 * @since 2025-11-04
 */

import { api } from '@/apis/index';
import type { AiKnowledge, ResultDomain, PageDomain, PageParam, KnowledgePermissionParams } from '@/types';

/**
 * 知识库API服务
 */
export const knowledgeApi = {
  /**
   * 创建知识库
   * @param knowledge 知识库数据
   * @returns Promise<ResultDomain<AiKnowledge>>
   */
  async createKnowledge(knowledge: AiKnowledge): Promise<ResultDomain<AiKnowledge>> {
    const response = await api.post<AiKnowledge>('/ai/knowledge', knowledge);
    return response.data;
  },

  /**
   * 更新知识库
   * @param knowledge 知识库数据
   * @returns Promise<ResultDomain<AiKnowledge>>
   */
  async updateKnowledge(knowledge: AiKnowledge): Promise<ResultDomain<AiKnowledge>> {
    const response = await api.put<AiKnowledge>('/ai/knowledge', knowledge);
    return response.data;
  },

  /**
   * 删除知识库
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async deleteKnowledge(knowledgeId: string): Promise<ResultDomain<boolean>> {
    const response = await api.delete<boolean>(`/ai/knowledge/${knowledgeId}`);
    return response.data;
  },

  /**
   * 获取知识库详情
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<AiKnowledge>>
   */
  async getKnowledgeById(knowledgeId: string): Promise<ResultDomain<AiKnowledge>> {
    const response = await api.get<AiKnowledge>(`/ai/knowledge/${knowledgeId}`);
    return response.data;
  },

  /**
   * 获取用户可见的知识库列表
   * @returns Promise<ResultDomain<AiKnowledge[]>>
   */
  async listUserKnowledges(): Promise<ResultDomain<AiKnowledge[]>> {
    const response = await api.get<AiKnowledge[]>('/ai/knowledge/user');
    return response.data;
  },

  /**
   * 获取知识库列表（支持过滤）
   * @param filter 过滤条件
   * @returns Promise<ResultDomain<AiKnowledge[]>>
   */
  async listKnowledges(filter?: Partial<AiKnowledge>): Promise<ResultDomain<AiKnowledge[]>> {
    const response = await api.post<AiKnowledge[]>('/ai/knowledge/list', filter || {});
    return response.data;
  },

  /**
   * 分页查询知识库
   * @param filter 过滤条件
   * @param pageParam 分页参数
   * @returns Promise<PageDomain<AiKnowledge>>
   */
  async pageKnowledges(filter: Partial<AiKnowledge>, pageParam: PageParam): Promise<ResultDomain<AiKnowledge>> {
    const response = await api.post<AiKnowledge>('/ai/knowledge/page', {
      filter,
      pageParam
    });
    return response.data;
  },

  /**
   * 同步知识库到Dify
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async syncToDify(knowledgeId: string): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>(`/ai/knowledge/${knowledgeId}/sync`);
    return response.data;
  },

  /**
   * 设置知识库权限
   * @param params 权限参数
   * @returns Promise<ResultDomain<boolean>>

   */
  async setPermissions(params: KnowledgePermissionParams): Promise<ResultDomain<boolean>> {
    const response = await api.post<boolean>('/ai/knowledge/permissions', params);
    return response.data;
  },

  /**
   * 获取知识库权限
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<any>>
   */
  async getPermissions(knowledgeId: string): Promise<ResultDomain<any>> {
    const response = await api.get<any>(`/ai/knowledge/${knowledgeId}/permissions`);
    return response.data;
  },

  /**
   * 检查用户是否有权限访问知识库
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<boolean>>
   */
  async checkPermission(knowledgeId: string): Promise<ResultDomain<boolean>> {
    const response = await api.get<boolean>(`/ai/knowledge/${knowledgeId}/check-permission`);
    return response.data;
  },

  /**
   * 获取知识库统计信息
   * @param knowledgeId 知识库ID
   * @returns Promise<ResultDomain<any>>
   */
  async getStats(knowledgeId: string): Promise<ResultDomain<any>> {
    const response = await api.get<any>(`/ai/knowledge/${knowledgeId}/stats`);
    return response.data;
  },

  /**
   * 获取可用的嵌入模型列表
   * @returns Promise<ResultDomain<any>>
   */
  async getAvailableEmbeddingModels(): Promise<ResultDomain<any>> {
    const response = await api.get<any>('/ai/knowledge/embedding-models');
    return response.data;
  },

  /**
   * 获取可用的Rerank模型列表
   * @returns Promise<ResultDomain<any>>
   */
  async getAvailableRerankModels(): Promise<ResultDomain<any>> {
    const response = await api.get<any>('/ai/knowledge/rerank-models');
    return response.data;
  },

  /**
   * 获取知识库文档列表
   * @param knowledgeId 知识库ID
   * @param page 页码（从1开始）
   * @param limit 每页数量
   * @returns Promise<ResultDomain<any>>
   */
  async getDocumentList(knowledgeId: string, page = 1, limit = 20): Promise<ResultDomain<any>> {
    const response = await api.get<any>(`/ai/knowledge/${knowledgeId}/documents`, {
       page, limit 
    });
    return response.data;
  }
};

