export type PriceType = "free" | "paid" | "freemium";
export type GameStatus = "seeded" | "draft" | "ready" | "published";

export interface FaqItem {
  question: string;
  answer: string;
}

export interface SpecBlock {
  os?: string;
  cpu?: string;
  gpu?: string;
  ram?: string;
  storage?: string;
  [key: string]: string | undefined;
}

export interface SystemRequirements {
  min?: SpecBlock;
  recommended?: SpecBlock;
}

export interface OfficialLinks {
  steam?: string;
  play_store?: string;
  app_store?: string;
  ps_store?: string;
  xbox?: string;
  epic?: string;
}

export interface TipBlock {
  heading: string;
  body: string;
}

export interface Platform {
  id: string;
  name: string;
  slug: string;
  icon_url: string | null;
}

export interface Genre {
  id: string;
  name: string;
  slug: string;
  icon_url: string | null;
}

export interface Game {
  id: string;
  slug: string;
  title: string;

  icon_image: string | null;
  cover_image: string | null;
  screenshots: string[];

  trailer_youtube_id: string | null;
  trailer_enabled: boolean;

  developer: string | null;
  publisher: string | null;
  release_date: string | null;

  price_type: PriceType | null;
  size_by_platform: Record<string, string>;
  age_rating: string | null;

  editor_score: number | null;
  verdict_short: string | null;

  overview: string | null;
  gameplay: string | null;
  who_its_for: string | null;

  pros: string[];
  cons: string[];
  tips: TipBlock[];

  system_requirements: SystemRequirements;
  faq: FaqItem[];

  official_links: OfficialLinks;

  meta_title: string | null;
  meta_description: string | null;
  og_image: string | null;

  status: GameStatus;
  last_verified_date: string | null;

  created_at: string;
  updated_at: string;

  // populated via joins, not columns
  platforms?: Platform[];
  genres?: Genre[];
}

/**
 * The minimum bar for `status` to move from 'ready' to 'published',
 * per the admin panel's content-status workflow.
 */
export function computeCompleteness(game: Partial<Game>): {
  percent: number;
  missing: string[];
} {
  const checks: Array<[string, boolean]> = [
    ["Overview (600+ words across content)", !!game.overview && game.overview.trim().split(/\s+/).length > 0],
    ["Pros (at least 1)", !!game.pros && game.pros.length > 0],
    ["Cons (at least 1)", !!game.cons && game.cons.length > 0],
    ["Screenshots (4 minimum)", !!game.screenshots && game.screenshots.length >= 4],
    ["Tips (3 minimum)", !!game.tips && game.tips.length >= 3],
    ["Editor score", game.editor_score !== null && game.editor_score !== undefined],
    ["Verdict", !!game.verdict_short],
    ["Cover image", !!game.cover_image],
  ];
  const missing = checks.filter(([, ok]) => !ok).map(([label]) => label);
  const percent = Math.round(((checks.length - missing.length) / checks.length) * 100);
  return { percent, missing };
}
