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

// When the user clicks a connecting itinerary in the Schedule list, the
// resulting flight-details URL must include EVERY leg, not just the
// first one — so the page renders both flights' detail cards (timeline,
// 'Детали рейса', schedule summary) and a Пересадка transfer bar
// between them. The fix is in ScheduleSearchPage.handleFlightClick:
// it now interleaves airport codes between each child flight ID when
// the row's `_childFlightIds` carries more than one entry.

test("connecting itinerary navigates to a multi-segment URL with both legs rendered", async ({
  page,
  consoleMessages,
}) => {
  await page.goto("/ru-ru/schedule/route/MOW-MMK-20260427-20260503");
  await expect(page.locator(".flight-card").first()).toBeVisible({
    timeout: 15000,
  });

  // Click "Детали рейса" inside the first connecting itinerary's
  // expanded body. The first row of MOW→MMK is a connecting flight
  // (e.g. SU 6188 → SU 6341 via LED).
  const firstCard = page
    .locator(".flight-card--clickable")
    .filter({ hasText: /SU \d+,\s*SU \d+/ })
    .first();
  const detailsBtn = firstCard
    .getByRole("button", { name: /Детали рейса/ })
    .first();
  await expect(detailsBtn).toBeVisible({ timeout: 10000 });
  await detailsBtn.click();

  // URL must now carry TWO flight segments separated by airport codes.
  // Pattern: /schedule/{depAir}/{flight1}-{date}/{midAir}/{flight2}-{date}/{arrAir}
  await expect(page).toHaveURL(
    /\/ru-ru\/schedule\/[A-Z]{3}\/SU\d+-\d{8}\/[A-Z]{3}\/SU\d+-\d{8}\/[A-Z]{3}/,
  );

  // Both flight cards present in the DOM.
  await expect(
    page.locator(".schedule-details__flight"),
  ).toHaveCount(2, { timeout: 15000 });

  // Transfer bar between them.
  await expect(page.getByText(/Пересадка/).first()).toBeVisible();
});
