/** Shared types for the CultRoll scraper worker */

export interface ScraperEnv {
  DB: D1Database;
  SCRAPER_QUEUE: Queue;
  SCRAPER_ADMIN_TOKEN?: string;
  TMDB_API_KEY?: string;
}

export interface ScrapedTitle {
  /** Chain's native title ID — used as the key in title_chain_refs. Not the canonical titles.id. */
  chainMovieId: string;
  title_en: string;
  title_ar?: string;
  title_original?: string;
  language?: string;
  subtitles?: string;
  genre?: string;
  duration_min?: number;
  /** Local content classification from the exhibitor (e.g. 'PG', 'R', 'TBC'). Stored in title_chain_refs, not titles. */
  exhibitor_rating?: string;
  synopsis_en?: string;
  /** Optional cast hints from the exhibitor when available. */
  cast?: string[];
  poster_url?: string;
  trailer_url?: string;
  imdb_id?: string;
  release_date?: string;
  /** Chain flagged this as "coming soon" — no showtimes expected yet. */
  is_coming_soon?: boolean;
}

/** @deprecated Use ScrapedTitle */
export type ScrapedMovie = ScrapedTitle;

export interface ScrapedShowtime {
  id: string;
  cinema_id: string;
  title_id: string;
  showtime: string;
  screen_type?: string;
  language?: string;
  subtitles?: string;
  price_display?: string;
  booking_url?: string;
}

export interface ScraperResult {
  chainId: string;
  titles: ScrapedTitle[];
  showtimes: ScrapedShowtime[];
}

export interface ScraperModule {
  id: string;
  cities: string[];
  scrape: (env: ScraperEnv) => Promise<ScraperResult>;
}

export interface AdapterConfig {
  [key: string]: unknown;
}

export interface AdapterRunResult {
  titles?: number;
  showtimes?: number;
  enriched?: number;
  deduped?: number;
  removed?: number;
  updated?: number;
  [key: string]: number | string | boolean | null | undefined;
}

export interface ScraperAdapter {
  id: string;
  run(config: AdapterConfig, env: ScraperEnv): Promise<AdapterRunResult>;
}

interface BaseTargetRow {
  id: string;
  adapter_id: string;
  label: string;
  config: string;
  interval_hours: number;
  enabled: number;
  last_run_at: string | null;
  next_run_at: string | null;
  created_at: string;
}

export interface SourceTargetRow extends BaseTargetRow {
  chain_id: string | null;
  country_code: string;
}

export interface SourceTargetCinemaRow {
  id: number;
  source_target_id: string;
  cinema_id: string | null;
  external_cinema_id: string;
  external_cinema_name: string | null;
  country_code: string;
  enabled: number;
  priority: number;
  config_override: string;
  created_at: string;
  updated_at: string;
}
