import { expect, test } from "@playwright/test";

import {
  enterChatFromSplash,
  submitEmailLogin,
  switchToEmailSignIn,
} from "@e2e/fixtures/test-helpers";

const preReleaseHost = "frontend-test.banlv-ai.com";
const baseURL = process.env.PLAYWRIGHT_BASE_URL ?? "";

test.describe("pre-release email login smoke", () => {
  test.skip(
    !baseURL.includes(preReleaseHost),
    `Run this test only with PLAYWRIGHT_BASE_URL pointing at ${preReleaseHost}.`,
  );

  test("guest can switch to email login from other sign-in options and return to chat", async ({
    page,
  }) => {
    await enterChatFromSplash(page, {
      expectedUrl: /\/chat(?:\?.*)?$/,
      timeout: 20_000,
      waitUntil: "domcontentloaded",
    });

    await expect(
      page.getByRole("button", { name: "Sign up to unlock more features" }),
    ).toBeVisible();
    await Promise.all([
      page.waitForURL(/\/auth(?:\?.*)?$/, { timeout: 20_000 }),
      page
        .getByRole("button", { name: "Sign up to unlock more features" })
        .click(),
    ]);

    await switchToEmailSignIn(page);
    await submitEmailLogin(page, {
      expectedUrl: /\/chat(?:\?.*)?$/,
      waitForRequest: false,
      urlTimeout: 20_000,
    });

    await expect(page.getByRole("button", { name: "Menu" })).toBeVisible();
    await expect(page.getByRole("textbox", { name: "Message" })).toBeVisible();
  });
});
