/**
 * Extended TypeScript types for Abitur-Archiv
 * Builds upon abitur-docs-types.ts with additional search and integration features
 */

import { AbiturDokument, AbiturDocsFilter } from './abitur-docs-types'

// Theme suggestion for autocomplete search
export interface ThemaSuggestion {
  label: string           // "Gedichtanalyse Romantik"
  count: number           // 12 Dokumente
  aufgabentyp: string     // "gedichtanalyse"
  zeitraum?: string       // "Romantik"
  kategorie?: string      // Category for grouping
}

// Extended filter with theme search
export interface AbiturArchivFilter extends AbiturDocsFilter {
  thema?: string          // Semantic theme search query
  aufgabentyp?: string    // Specific task type filter
}

// Similar document result from RAG
export interface SimilarDocument {
  id: string
  dateiname: string
  similarity_score: number
  fach: string
  jahr: number
  niveau: 'eA' | 'gA'
  typ: 'aufgabe' | 'erwartungshorizont'
  aufgaben_nummer: string
}

// Extended document with similar documents
export interface AbiturDokumentExtended extends AbiturDokument {
  similar_documents?: SimilarDocument[]
  themes?: string[]       // Extracted themes from content
}

// Response for archive listing
export interface AbiturArchivResponse {
  documents: AbiturDokumentExtended[]
  total: number
  page: number
  limit: number
  total_pages: number
  themes?: ThemaSuggestion[]  // Available themes for current filter
}

// Response for theme suggestions
export interface ThemaSuggestResponse {
  suggestions: ThemaSuggestion[]
  query: string
}

// Response for similar documents
export interface SimilarDocsResponse {
  document_id: string
  similar: SimilarDocument[]
}

// Klausur creation from archive
export interface KlausurFromArchivRequest {
  archiv_dokument_id: string
  aufgabentyp: string
  title?: string
}

export interface KlausurFromArchivResponse {
  klausur_id: string
  eh_id?: string
  success: boolean
  message?: string
}

// View mode for document display
export type ViewMode = 'grid' | 'list'

// Sort options
export type SortField = 'jahr' | 'fach' | 'datum' | 'dateiname'
export type SortDirection = 'asc' | 'desc'

export interface SortConfig {
  field: SortField
  direction: SortDirection
}

// Predefined theme categories
export const THEME_CATEGORIES = {
  textanalyse: {
    label: 'Textanalyse',
    subcategories: ['pragmatisch', 'literarisch', 'sachtext', 'rede', 'kommentar']
  },
  gedichtanalyse: {
    label: 'Gedichtanalyse',
    subcategories: ['romantik', 'expressionismus', 'barock', 'klassik', 'moderne']
  },
  dramenanalyse: {
    label: 'Dramenanalyse',
    subcategories: ['klassisch', 'modern', 'episches_theater']
  },
  prosaanalyse: {
    label: 'Prosaanalyse',
    subcategories: ['roman', 'kurzgeschichte', 'novelle', 'erzaehlung']
  },
  eroerterung: {
    label: 'Eroerterung',
    subcategories: ['textgebunden', 'materialgestuetzt', 'frei']
  },
  sprachreflexion: {
    label: 'Sprachreflexion',
    subcategories: ['sprachwandel', 'sprachkritik', 'kommunikation']
  }
} as const

// Popular theme suggestions (static fallback)
export const POPULAR_THEMES: ThemaSuggestion[] = [
  { label: 'Textanalyse', count: 45, aufgabentyp: 'textanalyse_pragmatisch', kategorie: 'Analyse' },
  { label: 'Gedichtanalyse', count: 38, aufgabentyp: 'gedichtanalyse', kategorie: 'Analyse' },
  { label: 'Eroerterung', count: 32, aufgabentyp: 'eroerterung_textgebunden', kategorie: 'Argumentation' },
  { label: 'Dramenanalyse', count: 28, aufgabentyp: 'dramenanalyse', kategorie: 'Analyse' },
  { label: 'Prosaanalyse', count: 25, aufgabentyp: 'prosaanalyse', kategorie: 'Analyse' },
]

// Quick action types for document cards
export type QuickAction = 'preview' | 'download' | 'add_to_klausur' | 'view_similar'

// Fullscreen viewer state
export interface ViewerState {
  isFullscreen: boolean
  zoom: number
  currentPage: number
  totalPages: number
}

// Default viewer state
export const DEFAULT_VIEWER_STATE: ViewerState = {
  isFullscreen: false,
  zoom: 100,
  currentPage: 1,
  totalPages: 1
}

// Zoom levels
export const ZOOM_LEVELS = [25, 50, 75, 100, 125, 150, 175, 200] as const
export const MIN_ZOOM = 25
export const MAX_ZOOM = 200
export const ZOOM_STEP = 25

// Helper functions
export function getThemeLabel(aufgabentyp: string): string {
  const entries = Object.entries(THEME_CATEGORIES)
  for (const [key, value] of entries) {
    if (aufgabentyp.startsWith(key)) {
      return value.label
    }
  }
  return aufgabentyp
}

export function formatSimilarityScore(score: number): string {
  return `${Math.round(score * 100)}%`
}
