// ===== Video Item (from get_library_info items array) =====

export interface VideoItem {
  index: number;
  fid: string;
  name: string;
  title: string;
  relpath: string;
  depth: number;
  pipes: boolean[];
  is_last: boolean;
  has_prev_in_parent: boolean;
  pos: number;
  watched: number;
  duration: number | null;
  finished: boolean;
  note_len: number;
  last_open: number;
  has_sub: boolean;
}

// ===== Library Info (from get_library / open_folder_path / select_folder) =====

export interface NextUp {
  index: number;
  title: string;
}

export interface LibraryInfo {
  ok: boolean;
  error?: string;
  cancelled?: boolean;
  folder?: string;
  library_id?: string;
  count?: number;
  current_index?: number;
  current_fid?: string | null;
  current_time?: number;
  folder_volume?: number;
  folder_autoplay?: boolean;
  folder_rate?: number;
  items?: VideoItem[];
  has_subdirs?: boolean;
  overall_progress?: number | null;
  durations_known?: number;
  finished_count?: number;
  remaining_count?: number;
  remaining_seconds_known?: number | null;
  top_folders?: { name: string; total: number; finished: number }[];
  next_up?: NextUp | null;
}

// ===== Preferences =====

export interface WindowState {
  width: number;
  height: number;
  x: number | null;
  y: number | null;
}

export interface Prefs {
  version: number;
  ui_zoom: number;
  split_ratio: number;
  dock_ratio: number;
  always_on_top: boolean;
  window: WindowState;
  last_folder_path: string | null;
  last_library_id: string | null;
  updated_at: number;
}

// ===== API Responses =====

export interface OkResponse {
  ok: boolean;
  error?: string;
}

export interface PrefsResponse {
  ok: boolean;
  prefs: Prefs;
}

export interface RecentItem {
  name: string;
  path: string;
}

export interface RecentsResponse {
  ok: boolean;
  items: RecentItem[];
}

export interface NoteResponse {
  ok: boolean;
  note: string;
  len?: number;
}

// ===== Video Metadata =====

export interface BasicFileMeta {
  ext: string;
  size: number;
  mtime: number;
  folder: string;
}

export interface SubtitleTrack {
  index: number;
  codec: string;
  language: string;
  title: string;
}

export interface ProbeMeta {
  v_codec?: string;
  width?: number;
  height?: number;
  fps?: number;
  v_bitrate?: number;
  pix_fmt?: string;
  color_space?: string;
  a_codec?: string;
  channels?: number;
  sample_rate?: string;
  a_bitrate?: number;
  subtitle_tracks?: SubtitleTrack[];
  container_bitrate?: number;
  duration?: number;
  format_name?: string;
  container_title?: string;
  encoder?: string;
}

export interface VideoMetaResponse {
  ok: boolean;
  error?: string;
  fid?: string;
  basic?: BasicFileMeta;
  probe?: ProbeMeta | null;
  ffprobe_found?: boolean;
}

// ===== Subtitles =====

export interface SubtitleResponse {
  ok: boolean;
  has?: boolean;
  url?: string;
  label?: string;
  cancelled?: boolean;
  error?: string;
}

export interface SidecarSub {
  path: string;
  label: string;
  format: string;
}

export interface EmbeddedSub {
  index: number;
  label: string;
  codec: string;
  language: string;
}

export interface AvailableSubsResponse {
  ok: boolean;
  sidecar: SidecarSub[];
  embedded: EmbeddedSub[];
}

export interface EmbeddedSubsResponse {
  ok: boolean;
  tracks: SubtitleTrack[];
}

// ===== FFmpeg Download Progress =====

export interface FfmpegProgress {
  percent: number;
  downloaded_bytes: number;
  total_bytes: number;
}
