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

test.describe("Schedule flight details button", () => {
  test("flight details button is visible in expanded flight body", async ({
    page,
  }) => {
    await page.goto("/ru-ru/schedule/route/SVO-LED-20260415");

    const cards = page.locator(".flight-card--clickable");
    await expect(cards.first()).toBeVisible({ timeout: 30000 });

    await cards.first().click();

    const actions = page.locator('[data-testid="schedule-flight-body-actions"]');
    await expect(actions).toBeVisible({ timeout: 10000 });

    const detailsBtn = actions.locator('[data-testid="flight-details-button"]');
    await expect(detailsBtn).toBeVisible();
  });

  test("flight details button has correct label (Russian)", async ({ page }) => {
    await page.goto("/ru-ru/schedule/route/SVO-LED-20260415");

    const cards = page.locator(".flight-card--clickable");
    await expect(cards.first()).toBeVisible({ timeout: 30000 });

    await cards.first().click();

    const detailsBtn = page.locator('[data-testid="flight-details-button"]');
    await expect(detailsBtn).toBeVisible();
    const text = await detailsBtn.textContent();
    expect(text).toContain("Детали");
  });

  test("flight details button navigates to flight details page", async ({
    page,
  }) => {
    await page.goto("/ru-ru/schedule/route/SVO-LED-20260415");

    const cards = page.locator(".flight-card--clickable");
    await expect(cards.first()).toBeVisible({ timeout: 30000 });

    await cards.first().click();

    const detailsBtn = page.locator('[data-testid="flight-details-button"]');
    await detailsBtn.click();

    await expect(page).toHaveURL(/\/ru-ru\/schedule\/[A-Z]{3}\/SU\d+-\d{8}\/[A-Z]{3}/);
  });

  test("flight details button works for connecting flights", async ({
    page,
  }) => {
    await page.goto("/ru-ru/schedule/route/MOW-MMK-20260427-20260503");

    const cards = page.locator(".flight-card--clickable");
    await expect(cards.first()).toBeVisible({ timeout: 30000 });

    const firstCard = cards.first();
    await firstCard.click();

    const detailsBtn = page.locator('[data-testid="flight-details-button"]');
    await expect(detailsBtn).toBeVisible({ timeout: 10000 });
    await detailsBtn.click();

    await expect(page).toHaveURL(
      /\/ru-ru\/schedule\/[A-Z]{3}\/SU\d+-\d{8}\/[A-Z]{3}\/SU\d+-\d{8}\/[A-Z]{3}/,
    );
  });

  test("flight details button preserves search context in URL", async ({
    page,
  }) => {
    await page.goto("/ru-ru/schedule/route/SVO-LED-20260415");

    const cards = page.locator(".flight-card--clickable");
    await expect(cards.first()).toBeVisible({ timeout: 30000 });

    await cards.first().click();

    const detailsBtn = page.locator('[data-testid="flight-details-button"]');
    await detailsBtn.click();

    const url = page.url();
    expect(url).toContain("?request=");
    expect(url).toContain("schedule-route-SVO-LED-20260415");
  });
});
