/**
 * Type definitions for RAG Admin Page
 */

export interface Collection {
  name: string
  displayName: string
  bundesland: string
  useCase: string
  description: string
  documentCount: number
  chunkCount: number
  years: number[]
  subjects: string[]
  status: 'ready' | 'indexing' | 'empty'
  createdAt?: string
}

export interface CreateCollectionData {
  name: string
  display_name: string
  bundesland: string
  use_case: string
  description: string
}

export interface IngestionStatus {
  running: boolean
  lastRun: string | null
  documentsIndexed: number | null
  chunksCreated: number | null
  errors: string[]
}

export interface SearchResult {
  id: string
  score: number
  text: string
  year: number | null
  subject: string | null
  niveau: string | null
  taskNumber: number | null
}

export interface UploadResult {
  filename: string
  status: 'success' | 'error' | 'all_duplicates' | 'partial_duplicates'
  message: string
  documentId?: string
  pdfs_extracted?: number
  pdfs_skipped?: number
  duplicates?: string[]
  target_year?: number
}

export interface LiveProgress {
  processed: number
  total: number
  currentFile: string
  errors: string[]
  status: 'idle' | 'processing' | 'complete' | 'error'
  running?: boolean  // Backend may send this
  startedAt: string | null
  completedAt: string | null
  // Additional properties from backend
  phase?: string
  percent?: number
  current_filename?: string
  current_doc?: number
  total_docs?: number
  documents_indexed?: number
  documents_skipped?: number
  chunks_created?: number
  errors_count?: number
}

export interface IndexedStats {
  documentsIndexed: number
  chunksCreated: number
  lastIndexed: string | null
  // Additional properties from backend
  indexed?: boolean
  total_chunks?: number
  years?: number[]
  subjects?: string[]
}

export interface PendingFile {
  id?: string
  filename: string
  size: number
  uploadedAt: string
  status: 'pending' | 'processing' | 'indexed' | 'error'
  error?: string
  // Additional properties from backend
  year?: number | string
  subject?: string
  niveau?: string
}

export interface PendingFilesData {
  files: PendingFile[]
  totalCount: number
  // Additional properties from backend
  pending_count?: number
  indexed_count?: number
  total_files?: number
  by_year?: Record<string, number>
  pending_files?: PendingFile[]
}

export interface IngestionHistoryEntry {
  id: string
  startedAt: string
  started_at?: string  // Backend uses snake_case
  completedAt: string | null
  status: 'running' | 'completed' | 'failed' | 'success'
  documentsProcessed: number
  chunksCreated: number
  errors: string[]
  error?: string  // Single error message
  collection: string
  stats?: {
    documents_found?: number
    documents_indexed?: number
    chunks_created?: number
    documents_skipped?: number
    errors_count?: number
  }
  filters?: {
    year?: number | string
    subject?: string
    incremental?: boolean
  }
}

export interface IndexedDocument {
  id: string
  doc_id: string  // Backend uses doc_id
  filename: string
  year: number | null
  subject: string | null
  niveau: string | null
  chunkCount: number
  chunk_count: number  // Backend uses snake_case
  doc_type?: string
  sample_text?: string
  indexedAt: string
  status: 'indexed' | 'pending' | 'error'
}

export interface IndexedDocumentsData {
  documents: IndexedDocument[]
  totalCount: number
  page: number
  pageSize: number
  totalPages: number
  years?: string[]
  subjects?: string[]
  total_documents?: number
  total_chunks?: number
  pagination?: {
    offset: number
    has_more: boolean
  }
}

export interface DocumentContent {
  id: string
  doc_id: string  // Backend uses doc_id
  filename: string
  text?: string  // Full document text
  chunk_count?: number
  chunks: {
    id: string
    text: string
    metadata: Record<string, unknown>
  }[]
  metadata: {
    year?: number
    subject?: string
    niveau?: string
    taskNumber?: number
    filename?: string
  }
}

export interface SystemInfo {
  vectorDb: {
    type: string
    status: 'connected' | 'disconnected' | 'error'
    collections: number
    totalVectors: number
  }
  embedding: {
    model: string
    dimension: number
    provider: string
  }
  chunking: {
    strategy: string
    chunkSize: number
    overlap: number
  }
  storage: {
    totalSize: string
    documentsStored: number
  }
}

export type TabId = 'collections' | 'upload' | 'ingestion' | 'documents' | 'search' | 'metrics' | 'system-info'
