import axios from 'axios';

const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3001';

const api = axios.create({
  baseURL: `${API_BASE_URL}/api/v1`,
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json',
  },
});

/**
 * 知识库类型定义
 */
export interface KnowledgeBase {
  id: string;
  userId: string;
  name: string;
  description?: string;
  difyDatasetId: string;
  fileCount: number;
  totalSizeBytes: number;
  createdAt: string;
  updatedAt: string;
  _count?: {
    documents: number;
  };
}

export interface Document {
  id: string;
  kbId: string;
  userId: string;
  filename: string;
  fileType: string;
  fileSizeBytes: number;
  fileUrl: string;
  difyDocumentId: string;
  status: 'uploading' | 'parsing' | 'indexing' | 'completed' | 'error';
  progress: number;
  errorMessage?: string;
  segmentsCount?: number;
  tokensCount?: number;
  uploadedAt: string;
  processedAt?: string;
}

export interface CreateKnowledgeBaseRequest {
  name: string;
  description?: string;
}

export interface UpdateKnowledgeBaseRequest {
  name?: string;
  description?: string;
}

export interface KnowledgeBaseStats {
  totalDocuments: number;
  completedDocuments: number;
  processingDocuments: number;
  errorDocuments: number;
  totalSizeBytes: number;
  totalTokens: number;
}

/**
 * 知识库管理API
 */
export const knowledgeBaseApi = {
  /**
   * 获取知识库列表
   */
  async getList(): Promise<KnowledgeBase[]> {
    const response = await api.get('/knowledge-bases');
    return response.data.data;
  },

  /**
   * 获取知识库详情
   */
  async getById(id: string): Promise<KnowledgeBase> {
    const response = await api.get(`/knowledge-bases/${id}`);
    return response.data.data;
  },

  /**
   * 创建知识库
   */
  async create(data: CreateKnowledgeBaseRequest): Promise<KnowledgeBase> {
    const response = await api.post('/knowledge-bases', data);
    return response.data.data;
  },

  /**
   * 更新知识库
   */
  async update(id: string, data: UpdateKnowledgeBaseRequest): Promise<KnowledgeBase> {
    const response = await api.put(`/knowledge-bases/${id}`, data);
    return response.data.data;
  },

  /**
   * 删除知识库
   */
  async delete(id: string): Promise<void> {
    await api.delete(`/knowledge-bases/${id}`);
  },

  /**
   * 获取知识库统计信息
   */
  async getStats(id: string): Promise<KnowledgeBaseStats> {
    const response = await api.get(`/knowledge-bases/${id}/stats`);
    return response.data.data;
  },

  /**
   * 检索知识库
   */
  async search(id: string, query: string, topK: number = 3): Promise<any> {
    const response = await api.get(`/knowledge-bases/${id}/search`, {
      params: { query, top_k: topK },
    });
    return response.data.data;
  },
};

/**
 * 文档管理API
 */
export const documentApi = {
  /**
   * 获取文档列表
   */
  async getList(kbId: string): Promise<Document[]> {
    const response = await api.get(`/knowledge-bases/${kbId}/documents`);
    return response.data.data;
  },

  /**
   * 获取文档详情
   */
  async getById(id: string): Promise<Document> {
    const response = await api.get(`/documents/${id}`);
    return response.data.data;
  },

  /**
   * 上传文档
   */
  async upload(kbId: string, file: File, onProgress?: (progress: number) => void): Promise<Document> {
    const formData = new FormData();
    formData.append('file', file);

    const response = await api.post(`/knowledge-bases/${kbId}/documents`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      onUploadProgress: (progressEvent) => {
        if (progressEvent.total && onProgress) {
          const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
          onProgress(percentCompleted);
        }
      },
    });
    return response.data.data;
  },

  /**
   * 删除文档
   */
  async delete(id: string): Promise<void> {
    await api.delete(`/documents/${id}`);
  },

  /**
   * 重新处理文档
   */
  async reprocess(id: string): Promise<void> {
    await api.post(`/documents/${id}/reprocess`);
  },

  /**
   * Phase 2: 获取文档全文（用于逐篇精读模式）
   */
  async getFullText(id: string): Promise<any> {
    const response = await api.get(`/documents/${id}/full-text`);
    return response.data;
  },
};

/**
 * Phase 2: 文档选择API（用于全文阅读模式）
 */
export const documentSelectionApi = {
  /**
   * 获取知识库的文档选择结果
   */
  async getSelection(kbId: string, maxFiles?: number, maxTokens?: number): Promise<any> {
    const params = new URLSearchParams();
    if (maxFiles) params.append('max_files', maxFiles.toString());
    if (maxTokens) params.append('max_tokens', maxTokens.toString());
    
    const url = `/knowledge-bases/${kbId}/document-selection${params.toString() ? '?' + params.toString() : ''}`;
    const response = await api.get(url);
    return response.data.data; // 修复：返回内层的data对象
  },
};


