import { expect, test } from "@playwright/test";
import {
  gotoLiveView,
  newIsolatedContext,
  projectEmail,
  registerAndConfirm,
} from "./helpers";

test("public language selection persists across pages", async ({ browser }) => {
  const context = await newIsolatedContext(browser);
  const page = await context.newPage();

  await page.goto("/?locale=ru");
  await expect(page.locator("html")).toHaveAttribute("lang", "ru");
  await expect(page.getByRole("heading", { name: "Помощь может быть ближе, чем кажется." })).toBeVisible();

  await page.goto("/safety");
  await expect(page.getByRole("heading", { name: "Правила безопасности" })).toBeVisible();

  await page.goto("/?locale=uk");
  await expect(page.locator("html")).toHaveAttribute("lang", "uk");
  await expect(page.getByRole("heading", { name: "Допомога може бути ближче, ніж здається." })).toBeVisible();

  await context.close();
});

test("authenticated LiveView uses the selected browser language", async ({
  browser,
  request,
}, testInfo) => {
  const user = await registerAndConfirm(
    browser,
    request,
    projectEmail("localization", testInfo.project.name),
    "E2E Localization",
  );

  await user.page.goto("/?locale=uk");
  await gotoLiveView(user.page, "/requests");

  await expect(user.page.locator("html")).toHaveAttribute("lang", "uk");
  await expect(user.page.getByRole("heading", { name: "Хто потребує допомоги?" })).toBeVisible();
  await expect(user.page.getByRole("link", { name: "Створити термінову заявку" })).toBeVisible();

  await user.context.close();
});
