import { test, expect } from "./fixtures/console-gate";
import type { Page } from "@playwright/test";

// Angular's breadcrumb trail (audited live on flights.test.aeroflot.ru):
//   /schedule                                      → [Главная]
//   /schedule/route/...                            → [Главная, Расписание рейсов]
//   /schedule/<dep>/<flt>/<arr>                    → [Главная, Расписание рейсов]
//   /schedule/<dep>/<flt>/<arr>?request=schedule-route-…
//       → [Главная, Расписание рейсов, "{depCity} - {arrCity}"]
//         (no "Маршрут:" prefix; leaf links back to /schedule/route/...)
//   /onlineboard                                   → [Главная]
//   /onlineboard/route/...                         → [Главная, Онлайн-Табло]
//   /onlineboard/<flt>-<date>                      → [Главная, Онлайн-Табло]
//   /onlineboard/<flt>-<date>?request=onlineboard-flight-…
//       → […, "Рейс: SU 6188"]   (carrier and number space-separated)
//   /onlineboard/<flt>-<date>?request=onlineboard-route-…
//       → […, "Маршрут: {depCity} - {arrCity}"]
//   /onlineboard/<flt>-<date>?request=onlineboard-departure-<airportIATA>-…
//       → […, "Вылет: {airportName}"]   (airport name, e.g. SVO → "Шереметьево")
//   /onlineboard/<flt>-<date>?request=onlineboard-arrival-<cityIATA>-…
//       → […, "Прилет: {cityName}"]

async function readCrumbs(page: Page) {
  return page.evaluate(() =>
    Array.from(document.querySelectorAll(".breadcrumbs__item")).map((li) => ({
      text:
        li.textContent
          ?.replace(/\s+/g, " ")
          .replace(/\/$/, "")
          .trim() ?? "",
      href: li.querySelector("a")?.getAttribute("href") ?? null,
    })),
  );
}

const cases: { name: string; url: string; expected: { text: string; href: string | null }[] }[] = [
  {
    name: "Schedule start page",
    url: "/ru-ru/schedule",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
    ],
  },
  {
    name: "Schedule route page",
    url: "/ru-ru/schedule/route/MOW-MMK-20260427-20260503",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Расписание рейсов", href: "/ru-ru/schedule" },
    ],
  },
  {
    name: "Schedule details page (share-link, no ?request=)",
    url: "/ru-ru/schedule/SVO/SU6951-20260427/LED",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Расписание рейсов", href: "/ru-ru/schedule" },
    ],
  },
  {
    name: "Schedule details page (with ?request=schedule-route)",
    url: "/ru-ru/schedule/SVO/SU6951-20260427/LED?request=schedule-route-MOW-LED-20260427-20260503",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Расписание рейсов", href: "/ru-ru/schedule" },
      {
        text: "Москва - Санкт-Петербург",
        href: "/ru-ru/schedule/route/MOW-LED-20260427-20260503",
      },
    ],
  },
  {
    name: "Onlineboard start page",
    url: "/ru-ru/onlineboard",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
    ],
  },
  {
    name: "Onlineboard route page",
    url: "/ru-ru/onlineboard/route/MOW-LED-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
    ],
  },
  {
    name: "Onlineboard details page (share-link, no ?request=)",
    url: "/ru-ru/onlineboard/SU0006-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
    ],
  },
  {
    name: "Onlineboard details page (with ?request=onlineboard-flight)",
    url: "/ru-ru/onlineboard/SU6188-20260423?request=onlineboard-flight-SU6188-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
      {
        text: "Рейс: SU 6188",
        href: "/ru-ru/onlineboard/flight/SU6188-20260423",
      },
    ],
  },
  {
    name: "Onlineboard details page (with ?request=onlineboard-route)",
    url: "/ru-ru/onlineboard/SU6188-20260423?request=onlineboard-route-MOW-LED-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
      {
        text: "Маршрут: Москва - Санкт-Петербург",
        href: "/ru-ru/onlineboard/route/MOW-LED-20260423",
      },
    ],
  },
  {
    name: "Onlineboard details page (with ?request=onlineboard-departure, airport IATA)",
    url: "/ru-ru/onlineboard/SU6188-20260423?request=onlineboard-departure-SVO-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
      {
        text: "Вылет: Шереметьево",
        href: "/ru-ru/onlineboard/departure/SVO-20260423",
      },
    ],
  },
  {
    name: "Onlineboard details page (with ?request=onlineboard-arrival, city IATA)",
    url: "/ru-ru/onlineboard/SU6188-20260423?request=onlineboard-arrival-LED-20260423",
    expected: [
      { text: "Главная", href: "https://www.aeroflot.ru" },
      { text: "Онлайн-Табло", href: "/ru-ru/onlineboard" },
      {
        text: "Прилет: Санкт-Петербург",
        href: "/ru-ru/onlineboard/arrival/LED-20260423",
      },
    ],
  },
];

test.describe("Breadcrumb parity with Angular", () => {
  for (const c of cases) {
    test(c.name, async ({ page, consoleMessages }) => {
      await page.goto(c.url);
      await expect(page.getByTestId("breadcrumbs")).toBeVisible({ timeout: 15000 });
      // Poll on the full items array — the leaf depends on dictionaries
      // fetched asynchronously, so labels arrive after the initial paint.
      await expect
        .poll(async () => readCrumbs(page), { timeout: 15000 })
        .toEqual(c.expected);
    });
  }
});
