/**
 * Dify API 类型定义
 */

// ==================== 知识库相关类型 ====================

/**
 * 知识库信息
 */
export interface Dataset {
  id: string;
  name: string;
  description: string;
  permission: 'only_me' | 'all_team_members';
  data_source_type: 'upload_file' | 'notion_import' | 'website_crawl';
  indexing_technique: 'high_quality' | 'economy';
  app_count: number;
  document_count: number;
  word_count: number;
  created_by: string;
  created_at: number;
  updated_by: string;
  updated_at: number;
}

/**
 * 创建知识库请求参数
 */
export interface CreateDatasetRequest {
  name: string;
  description?: string;
  permission?: 'only_me' | 'all_team_members';
  indexing_technique?: 'high_quality' | 'economy';
  embedding_model?: string;
  embedding_model_provider?: string;
  retrieval_model?: {
    search_method: 'semantic_search' | 'full_text_search' | 'hybrid_search';
    reranking_enable?: boolean;
    reranking_model?: {
      reranking_provider_name: string;
      reranking_model_name: string;
    };
    top_k?: number;
    score_threshold_enabled?: boolean;
    score_threshold?: number;
  };
}

/**
 * 创建知识库响应
 */
export interface CreateDatasetResponse {
  id: string;
  name: string;
  description: string;
  permission: string;
  data_source_type: string;
  indexing_technique: string;
  created_by: string;
  created_at: number;
}

/**
 * 知识库列表响应
 */
export interface DatasetListResponse {
  data: Dataset[];
  has_more: boolean;
  limit: number;
  total: number;
  page: number;
}

// ==================== 文档相关类型 ====================

/**
 * 文档信息
 */
export interface Document {
  id: string;
  position: number;
  data_source_type: string;
  data_source_info: {
    upload_file_id: string;
  };
  dataset_process_rule_id: string;
  name: string;
  created_from: string;
  created_by: string;
  created_at: number;
  tokens: number;
  indexing_status: 'waiting' | 'parsing' | 'cleaning' | 'splitting' | 'indexing' | 'completed' | 'error' | 'paused';
  error?: string;
  enabled: boolean;
  disabled_at?: number;
  disabled_by?: string;
  archived: boolean;
  display_status: string;
  word_count: number;
  hit_count: number;
}

/**
 * 文档列表响应
 */
export interface DocumentListResponse {
  data: Document[];
  has_more: boolean;
  limit: number;
  total: number;
  page: number;
}

/**
 * 上传文件响应
 */
export interface UploadFileResponse {
  id: string;
  name: string;
  size: number;
  extension: string;
  mime_type: string;
  created_by: string;
  created_at: number;
}

/**
 * 创建文档（从上传的文件）请求参数
 */
export interface CreateDocumentByFileRequest {
  indexing_technique: 'high_quality' | 'economy';
  process_rule: {
    rules: {
      pre_processing_rules: Array<{
        id: string;
        enabled: boolean;
      }>;
      segmentation: {
        separator: string;
        max_tokens: number;
      };
    };
    mode: 'automatic' | 'custom';
  };
  original_document_id?: string;
  doc_form?: 'text_model' | 'qa_model';
  doc_language?: string;
}

/**
 * 创建文档响应
 */
export interface CreateDocumentResponse {
  document: Document;
  batch: string;
}

// ==================== 知识库检索相关类型 ====================

/**
 * 知识库检索请求参数
 */
export interface RetrievalRequest {
  query: string;
  retrieval_model?: {
    search_method?: 'semantic_search' | 'full_text_search' | 'hybrid_search';
    reranking_enable?: boolean;
    reranking_model?: {
      reranking_provider_name: string;
      reranking_model_name: string;
    };
    top_k?: number;
    score_threshold_enabled?: boolean;
    score_threshold?: number;
  };
}

/**
 * 检索结果项
 */
export interface RetrievalRecord {
  segment_id: string;
  document_id: string;
  document_name: string;
  position: number;
  score: number;
  content: string;
  hit_count: number;
  word_count: number;
  segment_position: number;
  index_node_hash: string;
  metadata: Record<string, any>;
}

/**
 * 知识库检索响应
 */
export interface RetrievalResponse {
  query: {
    content: string;
  };
  records: RetrievalRecord[];
}

// ==================== 错误类型 ====================

/**
 * Dify API 错误响应
 */
export interface DifyErrorResponse {
  code: string;
  message: string;
  status: number;
}

/**
 * Dify API 错误
 */
export class DifyError extends Error {
  code: string;
  status: number;

  constructor(error: DifyErrorResponse) {
    super(error.message);
    this.name = 'DifyError';
    this.code = error.code;
    this.status = error.status;
  }
}


