import { test, expect } from "./fixtures/console-gate";

// TIRREDESIGN-10 — Onlineboard list rows must surface "Купить билет"
// and "Онлайн регистрация" inside the expanded body when the per-flight
// visibility rules pass:
//   • Buy: now ∈ [departure - buyTicketMaxHours, departure - buyTicketMinHours]
//   • Register: leg.transition.registration.status === "InProgress"
//
// We use a route view loaded via a synthetic fetch hook would be brittle,
// so this spec drives the live API via /onlineboard/route/MOW-LED-<today>
// and walks the rendered list to find a Запланирован row whose departure
// is far enough in the future to clear the buy window. If no such row
// exists (e.g. late evening when no scheduled flights remain), the test
// is skipped — it cannot assert against an empty board.

test("Onlineboard expanded row shows Купить билет + Онлайн регистрация when applicable", async ({
  page,
  consoleMessages,
}) => {
  // Today in the harness clock.
  const today = new Date().toISOString().slice(0, 10).replace(/-/g, "");
  await page.goto(`/ru-ru/onlineboard/route/MOW-LED-${today}`);
  await expect(page.locator(".flight-card").first()).toBeVisible({
    timeout: 15000,
  });

  // Walk Scheduled rows whose departure ≥ +2h from now and try to expand
  // them until we find one that surfaces the Buy button (Register depends
  // on the live `transition.registration.status` flag — when present we
  // also assert it; when absent the assertion is skipped).
  const cards = page.locator(".flight-card--clickable");
  const count = await cards.count();
  let proven = false;

  for (let i = 0; i < count; i++) {
    const card = cards.nth(i);
    const text = (await card.innerText()).replace(/\s+/g, " ");
    if (!/Запланирован/.test(text)) continue;
    await card.click();
    const expanded = page
      .locator(".flight-card--expanded")
      .filter({ hasText: text.split(" ").slice(0, 2).join(" ") });
    const buy = expanded.locator('[data-testid="buy-ticket-button"]');
    if ((await buy.count()) === 0) {
      // collapse and try the next
      await card.click();
      continue;
    }
    await expect(buy).toBeVisible();
    await expect(buy).toHaveText("Купить билет");

    // Register button is conditional on registration.status === InProgress —
    // assert it only when the row's body shows the Регистрация panel.
    const hasRegPanel = await expanded
      .getByText("Регистрация")
      .first()
      .isVisible()
      .catch(() => false);
    if (hasRegPanel) {
      const reg = expanded.locator('[data-testid="registration-button"]');
      await expect(reg).toBeVisible();
      await expect(reg).toHaveText("Онлайн регистрация");
    }
    proven = true;
    break;
  }

  test.skip(
    !proven,
    "no Scheduled flight ≥ +2h from now in the live MOW→LED board — cannot assert (live data dependency)",
  );
});
