import type { Locale } from "@/lib/i18n";
import type { CrosswordResponse } from "@/lib/types";

function buildCells(solutionRows: string[]) {
  const cells = [];
  const numbering = new Map<string, number>([
    ["0:0", 1],
    ["0:9", 2],
    ["2:0", 3],
    ["4:0", 4],
    ["4:6", 5],
    ["6:0", 6],
  ]);

  for (let row = 0; row < solutionRows.length; row += 1) {
    for (let col = 0; col < solutionRows[row].length; col += 1) {
      const char = solutionRows[row][col];
      if (char === "#") {
        cells.push({
          row,
          col,
          kind: "block" as const,
          solution: null,
          display: null,
          number: null,
          across_entry_id: null,
          down_entry_id: null,
          is_prefilled: false,
        });
        continue;
      }

      cells.push({
        row,
        col,
        kind: "letter" as const,
        solution: char,
        display: "",
        number: numbering.get(`${row}:${col}`) ?? null,
        across_entry_id: null,
        down_entry_id: null,
        is_prefilled: false,
      });
    }
  }

  return cells;
}

const solutionRows = [
  "AMBU#####PI",
  "L###T#####S",
  "TRENO#####T",
  "A###R#####A",
  "NAVE##MOTO#",
  "Z###R###O##",
  "AEROPORTO##",
  "######R#####",
];

const titles: Record<Locale, string> = {
  it: "Cruciverba a tema trasporti",
  en: "Transport-themed crossword",
  es: "Crucigrama sobre transportes",
};

const subtitles: Record<Locale, string> = {
  it: "Demo iniziale dell'interfaccia web",
  en: "Initial demo of the web interface",
  es: "Demo inicial de la interfaz web",
};

const clueTexts: Record<Locale, string[]> = {
  it: [
    "Inizio di un mezzo di soccorso stradale e sanitario.",
    "Superficie destinata al decollo e all'atterraggio.",
    "Mezzo di trasporto che corre su rotaie.",
    "Grande mezzo per viaggiare sul mare.",
    "Veicolo a due ruote con motore.",
    "Complesso destinato al traffico degli aerei.",
  ],
  en: [
    "Opening of a road and medical rescue vehicle.",
    "Surface used for takeoff and landing.",
    "Means of transport that runs on rails.",
    "Large vehicle used to travel by sea.",
    "Two-wheeled motor vehicle.",
    "Facility dedicated to airplane traffic.",
  ],
  es: [
    "Inicio de un vehículo de socorro vial y sanitario.",
    "Superficie destinada al despegue y aterrizaje.",
    "Medio de transporte que circula sobre rieles.",
    "Gran vehículo para viajar por mar.",
    "Vehículo de dos ruedas con motor.",
    "Complejo destinado al tráfico de aviones.",
  ],
};

const baseEntries = [
  {
    entry_id: "A1",
    number: 1,
    direction: "across" as const,
    answer: "AMBU",
    answer_length: 4,
    row: 0,
    col: 0,
    cells: [[0, 0], [0, 1], [0, 2], [0, 3]] as [number, number][],
    clue_source: "demo",
    topics: ["transport", "health"],
    pos: "NOUN",
    is_seed: true,
    added_by_filler: false,
    confidence: 0.8,
  },
  {
    entry_id: "A2",
    number: 2,
    direction: "across" as const,
    answer: "PISTA",
    answer_length: 5,
    row: 0,
    col: 9,
    cells: [[0, 9], [0, 10], [0, 11], [1, 11], [2, 11]] as [number, number][],
    clue_source: "semantic_definition",
    topics: ["transport", "aviation"],
    pos: "NOUN",
    is_seed: false,
    added_by_filler: true,
    confidence: 0.94,
  },
  {
    entry_id: "A3",
    number: 3,
    direction: "across" as const,
    answer: "TRENO",
    answer_length: 5,
    row: 2,
    col: 0,
    cells: [[2, 0], [2, 1], [2, 2], [2, 3], [2, 4]] as [number, number][],
    clue_source: "semantic_definition",
    topics: ["transport"],
    pos: "NOUN",
    is_seed: true,
    added_by_filler: false,
    confidence: 0.98,
  },
  {
    entry_id: "A4",
    number: 4,
    direction: "across" as const,
    answer: "NAVE",
    answer_length: 4,
    row: 4,
    col: 0,
    cells: [[4, 0], [4, 1], [4, 2], [4, 3]] as [number, number][],
    clue_source: "semantic_definition",
    topics: ["transport", "sea"],
    pos: "NOUN",
    is_seed: true,
    added_by_filler: false,
    confidence: 0.96,
  },
  {
    entry_id: "A5",
    number: 5,
    direction: "across" as const,
    answer: "MOTO",
    answer_length: 4,
    row: 4,
    col: 6,
    cells: [[4, 6], [4, 7], [4, 8], [4, 9]] as [number, number][],
    clue_source: "semantic_definition",
    topics: ["transport"],
    pos: "NOUN",
    is_seed: true,
    added_by_filler: false,
    confidence: 0.95,
  },
  {
    entry_id: "A6",
    number: 6,
    direction: "across" as const,
    answer: "AEROPORTO",
    answer_length: 9,
    row: 6,
    col: 0,
    cells: [[6, 0], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7], [6, 8]] as [number, number][],
    clue_source: "semantic_definition",
    topics: ["transport", "aviation"],
    pos: "NOUN",
    is_seed: true,
    added_by_filler: false,
    confidence: 0.97,
  },
];

export function getMockCrosswordResponse(locale: Locale): CrosswordResponse {
  const texts = clueTexts[locale];
  const entries = baseEntries.map((entry, index) => ({
    ...entry,
    clue: texts[index],
  }));

  return {
    schema_version: "1.0",
    request_id: "req-demo-transport",
    crossword_id: "demo-transport",
    generated_at: "2026-04-29T11:30:00+02:00",
    status: "ok",
    generator: {
      topic: ["transport"],
      difficulty: "medium",
      seed: 2,
      runtime_lexicon: "lexicon_it_curated_llm_aggressive.json",
    },
    summary: {
      title: titles[locale],
      subtitle: subtitles[locale],
      rows: solutionRows.length,
      cols: solutionRows[0].length,
      total_words: 6,
      intersections: 8,
    },
    grid: {
      rows: solutionRows.length,
      cols: solutionRows[0].length,
      cell_size_hint: 42,
      cells: buildCells(solutionRows),
    },
    entries,
    clues: {
      across: entries.map((entry) => ({
        number: entry.number,
        entry_id: entry.entry_id,
        text: entry.clue,
        enumeration: entry.answer_length,
        topic_match: true,
        source: entry.clue_source,
      })),
      down: [],
    },
    solution: {
      grid_rows: solutionRows,
      words: ["AMBU", "PISTA", "TRENO", "NAVE", "MOTO", "AEROPORTO"],
    },
    diagnostics: {
      seed_words_requested: 19,
      seed_words_placed: 19,
      filler_words_added: 1,
      filled_cells: 35,
      empty_cells: 61,
      empty_ratio: 0.6354,
      target_empty_ratio: 0.1667,
      topic_words: 6,
      off_topic_words: 0,
      pos_counts: {
        sostantivi: 6,
        aggettivi: 0,
        verbi: 0,
        avverbi: 0,
        preposizioni: 0,
        congiunzioni: 0,
        altri: 0,
      },
      generation_seconds: 11.4,
    },
    artifacts: {
      pdf_player: null,
      pdf_solution: null,
      thumbnail: null,
      html_preview: null,
    },
  };
}
