/**
 * 问题排查 API 类型
 */

/** 匹配案例 */
export interface MatchedCase {
  rank: number
  score: number
  record_id: string
  project: string
  title: string
  file: string
  phenomenon: string
}

/** 排查请求 */
export interface TroubleshootRequest {
  project_name?: string
  system_type?: string
  apk_product?: string
  query: string
}

/** 排查响应 */
export interface TroubleshootResponse {
  success: boolean
  response: string
  matched_cases: MatchedCase[]
  filter_warnings?: string[]
  api_time: number
  offline?: boolean
}

/** 搜索请求 */
export interface SearchRequest {
  project_name?: string
  query: string
}

/** 搜索响应 */
export interface SearchResponse {
  success: boolean
  matched_cases: MatchedCase[]
  matched_count: number
  search_time: number
}

/** AI 分析请求 */
export interface AnalyzeRequest {
  project_name?: string
  system_type?: string
  apk_product?: string
  query: string
  matched_cases?: MatchedCase[]
}

/** AI 分析响应 */
export interface AnalyzeResponse {
  success: boolean
  response: string
  filter_warnings?: string[]
  api_time: number
  offline?: boolean
}

/** 健康检查响应 */
export interface HealthResponse {
  status: 'ok' | 'error'
  timestamp: string
  version: string
  offline_mode: boolean
  knowledge_base: {
    total_records: number
    last_update: string
  }
  search: {
    mode: string
    vector_loaded: boolean
    embedding_model: string
  }
  cache: {
    total_files: number
    total_size_mb: number
  }
  scheduler: {
    initialized: boolean
    running?: boolean
    job_count?: number
    reason?: string
    error?: string
  }
  components: {
    search_engine: string
    safety_filter: string
    cache_manager: string
  }
}

/** 缓存统计响应 */
export interface CacheStatsResponse {
  success: boolean
  stats: {
    total_files: number
    total_size_mb: number
    oldest: string | null
    newest: string | null
  }
}

/** 清除缓存响应 */
export interface CacheClearResponse {
  success: boolean
  message: string
  cleared_count: number
}

/** 导出请求 */
export interface ExportRequest {
  project_name?: string
  system_type?: string
  apk_product?: string
  query?: string
  matched_cases?: MatchedCase[]
  response?: string
}

/** 提交记录请求 */
export interface SubmitRequest {
  project_name: string
  system_type?: string
  apk_product?: string
  phenomenon: string
  troubleshoot_steps?: string
  root_cause?: string
  solution?: string
  recorder: string
}

/** 提交记录响应 */
export interface SubmitResponse {
  success: boolean
  message: string
  record_id: string
  file: string
}

/** SSE 流式分析事件 */
export interface AnalyzeStreamEvent {
  type: 'start' | 'matched_cases' | 'chunk' | 'done' | 'error'
  message?: string
  cases?: MatchedCase[]
  content?: string
  api_time?: number
  cached?: boolean
  offline?: boolean
}

// ============================================================
// 离线模式类型（产品资料 + 常见问题）
// ============================================================

/** 产品信息 */
export interface Product {
  id: string
  name: string
  system_type?: string
  apk_product?: string
  documents?: ProductDocument[]
  faq_count?: number
  tags?: string[]
}

/** 产品文档资料 */
export interface ProductDocument {
  id: string
  title: string
  filename: string
  type: string
  size?: string
}

/** 常见问题 */
export interface FAQ {
  record_id: string
  title: string
  phenomenon: string
  solution: string
  score?: number
}

/** 产品列表响应 */
export interface ProductsResponse {
  success: boolean
  products: Product[]
  total: number
}

/** 产品详情响应 */
export interface ProductResponse {
  success: boolean
  product?: Product
  message?: string
}

/** 文档列表响应 */
export interface DocumentsResponse {
  success: boolean
  documents: ProductDocument[]
  product_id: string
}

/** 常见问题列表响应 */
export interface FAQsResponse {
  success: boolean
  faqs: FAQ[]
  product_id: string
  total: number
}
