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

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 page.goto("/splash", { waitUntil: "domcontentloaded" });

    await expect(page.getByRole("button", { name: "Skip" })).toBeVisible();
    await Promise.all([
      page.waitForURL(/\/chat(?:\?.*)?$/, { timeout: 20_000 }),
      page.getByRole("button", { name: "Skip" }).click(),
    ]);

    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 page.getByRole("button", { name: "Other Sign In Options" }).click();
    await expect(
      page.getByRole("dialog", { name: "Other sign-in options" }),
    ).toBeVisible();

    await page.getByRole("button", { name: "Email Sign In" }).click();
    await expect(page.getByPlaceholder("Email")).toHaveValue(
      "chanwillian0@gmail.com",
    );
    await expect(page.getByPlaceholder("Password")).toHaveValue("12345678");

    await Promise.all([
      page.waitForURL(/\/chat(?:\?.*)?$/, { timeout: 20_000 }),
      page.getByRole("button", { name: "Login" }).click(),
    ]);

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