// ============================================================
// Common Intermediate Format (CIF) Types
// Universal format for all reflowable ebooks after parsing
// ============================================================

interface EbookCIF {
  // Universal metadata (all formats)
  metadata: {
    title: string;
    author: string;
    language: string;
    publisher?: string;
    isbn?: string;
    coverImage?: Blob;
  };

  // Unified navigation structure
  toc: TOCNode[];

  // Content spine (reading order)
  spine: SpineItem[];

  // Resources (CSS, fonts, images)
  resources: Map<string, Blob>;

  // Progress tracking (minimal - backend handles detailed tracking)
  locations: {
    totalCharacters: number;
    estimatedPages: number;
  };
}

interface SpineItem {
  id: string;
  type: "html" | "image";
  content: string;
  properties?: string[];

  // Minimal position info for UI
  index: number;
}

interface TOCNode {
  id: string;
  title: string;
  href: string;
  children: TOCNode[];
}

// ============================================================
// Parser Types (Procedural, not OOP)
// ============================================================

type ParserFormat =
  | "epub"
  | "fb2"
  | "txt"
  | "html"
  | "mobi"
  | "azw3"
  | "docx"
  | "rtf";

interface ParserCapabilities {
  canParse(mimeType: string, extension: string): boolean;
  parse(file: Blob): Promise<EbookCIF>;
  extractMetadata(file: Blob): Promise<Partial<EbookCIF["metadata"]>>;
}

// ============================================================
// Reader Metadata (from API)
// ============================================================

interface ReaderMetadata {
  // Core identification
  id: string;
  media_item_id: string;
  library_id: string;

  // Content metadata
  title: string;
  author: string;
  cover_image_path: string;
  description: string;
  isbn: string;
  series: string;
  series_number: number;
  tags: string[];
  asin: string;
  date_published: string;
  publisher: string;
  contributors: string[];
  language: string;
  edition: string;
  page_count: number;
  genre: string;

  // Format identification
  format_group: string;
  mime_type: string;
  file_path: string;
  file_size: number;

  // Library classification
  library_type_name: string;
  library_type: string;

  // Comic/Manga specific
  manga_type: string;
  reading_direction: string;
  series_count: number;

  // Additional metadata from server
  volume: number;
  imprint: string;
  age_rating: string;
  web_url: string;
  metadata_notes: string;
  community_rating: number;
  story_arc: string;
  is_black_and_white: boolean;
  alternate_info: string;
  scan_information: string;
  summary: string;
  created_at: string;
  updated_at: string;

  // Progress tracking
  chapter_metadata?: ChapterMetadata;
  total_pages?: number;
  saved_progress?: {
    current_page?: number;
    cfi?: string;
    progress?: number;
  };
}

// ============================================================
// Other Shared Types
// ============================================================

interface ChapterMetadata {
  chapters: Chapter[];
}

interface Chapter {
  id: string;
  title: string;
  start_page: number;
  page_count: number;
  spine_index?: number;
}

interface PanelData {
  media_item_id: string;
  page_number: number;
  detection_method: "grid" | "ml" | "manual";
  panels: Panel[];
  updated_at: string;
}

interface Panel {
  id: string;
  x: number;
  y: number;
  width: number;
  height: number;
  reading_order: number;
}

interface ReadingSpeed {
  words_per_minute: number;
  pages_per_minute: number;
  pages_read: number;
  total_reading_minutes: number;
  last_read_at: string;
}

interface DictionaryEntry {
  word: string;
  definition: string;
  part_of_speech?: string;
  example?: string;
  etymology?: string;
}

interface ReaderSettings {
  chrome_behavior: "auto-hide" | "always-visible" | "hide-on-scroll";
  progress_mode: "pages" | "chapter" | "percentage" | "time-left";

  chrome_theme: string;
  reading_theme: string;

  reading_font:
    | "literata"
    | "crimson"
    | "source-serif"
    | "eb-garamond"
    | "libertinus"
    | "noto-serif"
    | "charis-sil"
    | "ibm-plex";
  font_size: number;
  line_height: number;
  margin_width: number;

  tap_zone_size: number;
  auto_scroll: boolean;

  double_page_spread: boolean;
  pdf_interaction_mode: "select" | "pan" | "text";
  tap_zones_enabled: boolean;
  reading_direction: "ltr" | "rtl" | "vertical";
  reading_mode: "dark" | "light";

  pdf_fit_mode: "fit-width" | "fit-page" | "fit-height" | "none";
  pdf_zoom_level: number;
  pdf_text_layer_enabled: boolean;
  pdf_dual_page_mode: "auto" | "single" | "dual";
  pdf_dual_page_threshold: number;
  pdf_minimap_enabled: boolean;
  pdf_outline_visible: boolean;
  media_bookmarks_visible: boolean;

  hardware_acceleration: boolean;

  // Dockable panel layout configuration (per-user, media-type-aware)
  panel_layout: PanelLayoutSettings;
}

interface PanelLayoutSettings {
  // Per panel state: which side, visible, collapsed (window-shade), width
  toc: PanelState;
  settings: PanelState;
  navigator: PanelState;
  bookmarks: PanelState;

  // Mobile override
  mobile_nav_visible: boolean;
}

interface PanelState {
  side: "left" | "right" | "hidden";
  visible: boolean;
  collapsed: boolean; // Window-shade: true = collapsed to title bar
  width_px: number; // Panel width in pixels
  order: number; // Order within the side
  locked: boolean; // Lock toggle: prevents accidental drag/move
  last_valid_side: "left" | "right" | "hidden"; // Snap-back target if dropped in invalid area
}

interface ProgressDisplay {
  mode: "pages" | "chapter" | "percentage" | "time-left";
  current: number;
  total: number;
  label?: string;
  time_left?: string;
}

interface FoliateTocItem {
  id: number;
  label: string;
  href: string;
  subitems?: FoliateTocItem[];
}
