export type Page<T> = {
  items: T[]
  total: number
  offset: number
  limit: number
}

export type DocumentStatus = 'pending' | 'indexing' | 'indexed' | 'failed' | 'deleting'

export type Document = {
  id: string
  filename: string
  file_type: 'pdf' | 'md' | 'txt'
  mime_type: string
  file_size: number
  status: DocumentStatus
  chunk_count: number
  error_code: string | null
  error_message: string | null
  created_at: string
  updated_at: string
}

export type Chunk = {
  id: string
  document_id: string
  order_index: number
  content: string
  page_number: number | null
  char_count: number
  created_at: string
}

export type Session = {
  id: string
  title: string | null
  created_at: string
  updated_at: string
}

export type Source = {
  label: string
  document_id: string
  chunk_id: string
  filename: string
  order_index: number
  page_number: number | null
  content: string
  score: number
}

export type Message = {
  id: string
  session_id: string
  role: 'user' | 'assistant'
  content: string
  status: 'generating' | 'completed' | 'failed'
  error_code: string | null
  order_index: number
  created_at: string
  sources: Source[]
  deleted_source_labels: string[]
}

export type ApiErrorBody = {
  error: {
    code: string
    message: string
    request_id: string
    retryable?: boolean
    details?: unknown
  }
}

export type SSEEvent =
  | { event: 'status'; data: { phase: 'researching' | 'answering' } }
  | { event: 'sources'; data: { sources: Source[] } }
  | { event: 'token'; data: { content: string } }
  | { event: 'done'; data: { message_id: string } }
  | { event: 'error'; data: { code: string; message: string; retryable: boolean } }
