import { UploadedFile } from '@/components/QRCodeUpload'

export interface VocabularyEntry {
  id: string
  english: string
  german: string
  example_sentence?: string
  example_sentence_gap?: string
  word_type?: string
  source_page?: number
  selected?: boolean
  extras?: Record<string, string>
}

export interface ExtraColumn {
  key: string
  label: string
}

export interface Session {
  id: string
  name: string
  status: string
  vocabulary_count: number
  image_path?: string
  description?: string
  source_language?: string
  target_language?: string
  created_at?: string
}

export interface StoredDocument {
  id: string
  name: string
  type: string
  size: number
  uploadedAt: Date
  url?: string
}

export interface OcrPrompts {
  filterHeaders: boolean
  filterFooters: boolean
  filterPageNumbers: boolean
  customFilter: string
  headerPatterns: string[]
  footerPatterns: string[]
}

export type TabId = 'upload' | 'pages' | 'vocabulary' | 'spreadsheet' | 'worksheet' | 'export' | 'settings'
export type WorksheetType = 'en_to_de' | 'de_to_en' | 'copy' | 'gap_fill'
export type WorksheetFormat = 'standard' | 'nru'
export type IpaMode = 'auto' | 'en' | 'de' | 'all' | 'none'
export type SyllableMode = 'auto' | 'en' | 'de' | 'all' | 'none'

/** Return type of useVocabWorksheet — used as props by all child components */
export interface VocabWorksheetHook {
  // Mounted (SSR guard)
  mounted: boolean

  // Theme
  isDark: boolean
  glassCard: string
  glassInput: string

  // Tab
  activeTab: TabId
  setActiveTab: (tab: TabId) => void

  // Session
  session: Session | null
  sessionName: string
  setSessionName: (name: string) => void
  isCreatingSession: boolean
  error: string | null
  setError: (err: string | null) => void
  extractionStatus: string

  // Existing sessions
  existingSessions: Session[]
  isLoadingSessions: boolean

  // Documents
  storedDocuments: StoredDocument[]
  selectedDocumentId: string | null
  setSelectedDocumentId: (id: string | null) => void

  // Direct file
  directFile: File | null
  setDirectFile: (f: File | null) => void
  directFilePreview: string | null
  showFullPreview: boolean
  setShowFullPreview: (show: boolean) => void
  directFileInputRef: React.RefObject<HTMLInputElement | null>

  // PDF pages
  pdfPageCount: number
  selectedPages: number[]
  pagesThumbnails: string[]
  isLoadingThumbnails: boolean
  excludedPages: number[]

  // Extra columns
  pageExtraColumns: Record<number, ExtraColumn[]>

  // Upload
  uploadedImage: string | null
  isExtracting: boolean

  // Vocabulary
  vocabulary: VocabularyEntry[]

  // Worksheet
  selectedTypes: WorksheetType[]
  worksheetTitle: string
  setWorksheetTitle: (title: string) => void
  includeSolutions: boolean
  setIncludeSolutions: (inc: boolean) => void
  lineHeight: string
  setLineHeight: (lh: string) => void
  selectedFormat: WorksheetFormat
  setSelectedFormat: (f: WorksheetFormat) => void
  ipaMode: IpaMode
  setIpaMode: (m: IpaMode) => void
  syllableMode: SyllableMode
  setSyllableMode: (m: SyllableMode) => void

  // Export
  worksheetId: string | null
  isGenerating: boolean

  // Processing
  processingErrors: string[]
  successfulPages: number[]
  failedPages: number[]
  currentlyProcessingPage: number | null

  // OCR settings
  ocrPrompts: OcrPrompts
  showSettings: boolean
  setShowSettings: (show: boolean) => void


  // QR
  showQRModal: boolean
  setShowQRModal: (show: boolean) => void
  uploadSessionId: string
  mobileUploadedFiles: UploadedFile[]
  selectedMobileFile: UploadedFile | null
  setSelectedMobileFile: (f: UploadedFile | null) => void
  setMobileUploadedFiles: (files: UploadedFile[]) => void

  // OCR Comparison
  showOcrComparison: boolean
  setShowOcrComparison: (show: boolean) => void
  ocrComparePageIndex: number | null
  ocrCompareResult: any
  isComparingOcr: boolean
  ocrCompareError: string | null

  // Handlers
  handleDirectFileSelect: (e: React.ChangeEvent<HTMLInputElement>) => void
  startSession: () => Promise<void>
  processSelectedPages: () => Promise<void>
  togglePageSelection: (idx: number) => void
  selectAllPages: () => void
  selectNoPages: () => void
  excludePage: (idx: number, e: React.MouseEvent) => void
  restoreExcludedPages: () => void
  runOcrComparison: (pageIdx: number) => Promise<void>
  updateVocabularyEntry: (id: string, field: string, value: string) => void
  addExtraColumn: (page: number) => void
  removeExtraColumn: (page: number, key: string) => void
  getExtraColumnsForPage: (page: number) => ExtraColumn[]
  getAllExtraColumns: () => ExtraColumn[]
  deleteVocabularyEntry: (id: string) => void
  toggleVocabularySelection: (id: string) => void
  toggleAllSelection: () => void
  addVocabularyEntry: (atIndex?: number) => void
  saveVocabulary: () => Promise<void>
  generateWorksheet: () => Promise<void>
  downloadPDF: (type: 'worksheet' | 'solution') => void
  toggleWorksheetType: (type: WorksheetType) => void
  resumeSession: (session: Session) => Promise<void>
  resetSession: () => Promise<void>
  deleteSession: (id: string, e: React.MouseEvent) => Promise<void>
  saveOcrPrompts: (prompts: OcrPrompts) => void
  formatFileSize: (bytes: number) => string
  reprocessPages: (ipa: IpaMode, syllable: SyllableMode) => void
}
