import type { OcrWordBox } from '@/app/(admin)/ai/ocr-kombi/types'

// Re-export for convenience
export type { OcrWordBox }

/** Layout metrics derived from OCR word positions for faithful grid reconstruction. */
export interface LayoutMetrics {
  page_width_px: number
  page_height_px: number
  avg_row_height_px: number
  font_size_suggestion_px: number
}

/** Dictionary detection result from backend analysis. */
export interface DictionaryDetection {
  is_dictionary: boolean
  confidence: number
  signals: Record<string, unknown>
  article_col_index: number | null
  headword_col_index: number | null
}

/** Page number extracted from footer region of the scan. */
export interface PageNumber {
  text: string
  y_pct: number
  number?: number
}

/** A complete structured grid with zones, ready for the Excel-like editor. */
export interface StructuredGrid {
  session_id: string
  image_width: number
  image_height: number
  zones: GridZone[]
  boxes_detected: number
  summary: GridSummary
  formatting: GridFormatting
  layout_metrics?: LayoutMetrics
  dictionary_detection?: DictionaryDetection
  page_number?: PageNumber | null
  duration_seconds: number
  edited?: boolean
  layout_dividers?: LayoutDividers
}

export interface GridSummary {
  total_zones: number
  total_columns: number
  total_rows: number
  total_cells: number
  total_words: number
  recovered_colored?: number
  color_stats?: Record<string, number>
}

export interface GridFormatting {
  bold_columns: number[]
  header_rows: number[]
}

/** A horizontal zone of the page — either content or a bordered box. */
export interface GridZone {
  zone_index: number
  zone_type: 'content' | 'box'
  bbox_px: BBox
  bbox_pct: BBox
  border: ZoneBorder | null
  word_count: number
  columns: GridColumn[]
  rows: GridRow[]
  cells: GridEditorCell[]
  header_rows: number[]
  layout_hint?: 'left_of_vsplit' | 'right_of_vsplit' | 'middle_of_vsplit'
  vsplit_group?: number
  box_layout_type?: 'flowing' | 'columnar' | 'bullet_list' | 'header_only'
  box_grid_reviewed?: boolean
  box_bg_color?: string
  box_bg_hex?: string
}

export interface BBox {
  x: number
  y: number
  w: number
  h: number
}

export interface ZoneBorder {
  thickness: number
  confidence: number
}

export interface GridColumn {
  index: number
  label: string
  x_min_px: number
  x_max_px: number
  x_min_pct: number
  x_max_pct: number
  bold: boolean
}

export interface GridRow {
  index: number
  y_min_px: number
  y_max_px: number
  y_min_pct: number
  y_max_pct: number
  is_header: boolean
  is_footer?: boolean
}

export interface GridEditorCell {
  cell_id: string
  zone_index: number
  row_index: number
  col_index: number
  col_type: string
  text: string
  confidence: number
  bbox_px: BBox
  bbox_pct: BBox
  word_boxes: OcrWordBox[]
  ocr_engine: string
  is_bold: boolean
  /** Manual color override: hex string or null to clear. */
  color_override?: string | null
  /** Number of columns this cell spans (merged cell). Default 1. */
  colspan?: number
  /** Source zone type when in unified grid. */
  source_zone_type?: 'content' | 'box'
  /** Box visual metadata for cells from box zones. */
  box_region?: {
    bg_hex?: string
    bg_color?: string
    border?: boolean
  }
}

/** Layout dividers for the visual column/margin editor on the original image. */
export interface LayoutDividers {
  horizontals: {
    top_margin?: number
    header_bottom?: number
    footer_top?: number
    bottom_margin?: number
  }
}

/** Cell formatting applied by the user in the editor. */
export interface CellFormatting {
  bold: boolean
  fontSize: 'small' | 'normal' | 'large'
  align: 'left' | 'center' | 'right'
}
