/**
 * @description Dify 文档分段管理 API
 * @author AI Assistant
 * @since 2025-11-04
 */

import { api } from '@/apis/index';
import type { ResultDomain } from '@/types';
import type {
  DifyChildChunkListResponse,
  DifyChildChunkResponse,
  SegmentUpdateRequest,
  SegmentCreateRequest
} from '@/types/ai';

/**
 * 文档分段管理 API
 */
export const documentSegmentApi = {
  /**
   * 获取文档的所有分段（父级，支持分页）
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param page 页码（默认1）
   * @param limit 每页条数（默认10）
   * @returns Promise<ResultDomain<any>> 返回包含data数组和分页信息的对象
   */
  async getDocumentSegments(
    datasetId: string,
    documentId: string,
    page = 1,
    limit = 20
  ): Promise<ResultDomain<any>> {
    const response = await api.get<any>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments`,
      {
        page,
        limit
      },{showLoading: false}
    );
    return response.data;
  },

  /**
   * 更新文档分段
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @param segment 分段更新数据
   * @returns Promise<ResultDomain<any>>
   */
  async updateSegment(
    datasetId: string,
    documentId: string,
    segmentId: string,
    segment: { content?: string; keywords?: string[]; enabled?: boolean }
  ): Promise<ResultDomain<any>> {
    const response = await api.post<any>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`,
      { segment }
    );
    return response.data;
  },

  /**
   * 创建文档分段
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segments 分段数据数组
   * @returns Promise<ResultDomain<any>>
   */
  async createSegment(
    datasetId: string,
    documentId: string,
    segments: Array<{ content: string; answer?: string; keywords?: string[] }>
  ): Promise<ResultDomain<any>> {
    const response = await api.post<any>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments`,
      { segments }
    );
    return response.data;
  },

  /**
   * 删除文档分段
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @returns Promise<ResultDomain<any>>
   */
  async deleteSegment(
    datasetId: string,
    documentId: string,
    segmentId: string
  ): Promise<ResultDomain<any>> {
    const response = await api.delete<any>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}`
    );
    return response.data;
  },

  /**
   * 获取分段的子块列表
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @returns Promise<ResultDomain<DifyChildChunkListResponse>>
   */
  async getChildChunks(
    datasetId: string,
    documentId: string,
    segmentId: string
  ): Promise<ResultDomain<DifyChildChunkListResponse>> {
    const response = await api.get<DifyChildChunkListResponse>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`
    );
    return response.data;
  },

  /**
   * 更新子块内容
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @param childChunkId 子块ID
   * @param content 新内容
   * @returns Promise<ResultDomain<DifyChildChunkResponse>>
   */
  async updateChildChunk(
    datasetId: string,
    documentId: string,
    segmentId: string,
    childChunkId: string,
    content: string
  ): Promise<ResultDomain<DifyChildChunkResponse>> {
    const requestBody: SegmentUpdateRequest = { content };
    const response = await api.patch<DifyChildChunkResponse>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`,
      requestBody
    );
    return response.data;
  },

  /**
   * 创建新的子块
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @param content 分段内容
   * @returns Promise<ResultDomain<DifyChildChunkResponse>>
   */
  async createChildChunk(
    datasetId: string,
    documentId: string,
    segmentId: string,
    content: string
  ): Promise<ResultDomain<DifyChildChunkResponse>> {
    const requestBody: SegmentCreateRequest = { content };
    const response = await api.post<DifyChildChunkResponse>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks`,
      requestBody
    );
    return response.data;
  },

  /**
   * 删除子块
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @param segmentId 分段ID
   * @param childChunkId 子块ID
   * @returns Promise<ResultDomain<Boolean>>
   */
  async deleteChildChunk(
    datasetId: string,
    documentId: string,
    segmentId: string,
    childChunkId: string
  ): Promise<ResultDomain<Boolean>> {
    const response = await api.delete<boolean>(
      `/ai/dify/datasets/${datasetId}/documents/${documentId}/segments/${segmentId}/child_chunks/${childChunkId}`
    );
    return response.data;
  },

  /**
   * 批量获取所有分段和子块
   * @param datasetId Dify数据集ID
   * @param documentId Dify文档ID
   * @returns Promise<DifyChildChunk[]> 所有子块的扁平列表
   */
  async getAllSegmentsWithChunks(
    datasetId: string,
    documentId: string
  ): Promise<any[]> {
    // 1. 获取所有父级分段
    const segmentsResult = await this.getDocumentSegments(datasetId, documentId);
    
    if (!segmentsResult.success || !segmentsResult.dataList) {
      throw new Error('获取分段列表失败');
    }

    // 2. 对每个父级分段，获取其子块
    const allChunks: any[] = [];
    
    for (const segment of segmentsResult.dataList) {
      try {
        const chunksResult = await this.getChildChunks(
          datasetId,
          documentId,
          segment.id
        );
        
        if (chunksResult.success && chunksResult.data?.data) {
          // 为每个子块添加父级分段信息（用于显示）
          const chunksWithSegmentInfo = chunksResult.data.data.map(chunk => ({
            ...chunk,
            parentSegmentId: segment.id,
            parentPosition: segment.position,
            parentKeywords: segment.keywords
          }));
          allChunks.push(...chunksWithSegmentInfo);
        }
      } catch (error) {
        console.error(`获取分段 ${segment.id} 的子块失败:`, error);
        // 继续处理其他分段
      }
    }

    return allChunks;
  }
};

