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

test.describe("Authentication", () => {
  test("login page loads", async ({ page }) => {
    await page.goto("/");
    await expect(page.getByText("SEC cyBERT")).toBeVisible();
    await expect(page.getByPlaceholder("Enter password")).toBeVisible();
  });

  test("rejects wrong password", async ({ page }) => {
    await page.goto("/");

    await page.selectOption("#annotator", "joey");
    await page.getByPlaceholder("Enter password").fill("wrongpassword");
    await page.getByRole("button", { name: "Sign In" }).click();

    await expect(page.getByText("Invalid credentials")).toBeVisible();
  });

  test("logs in with correct password and redirects to dashboard", async ({
    page,
  }) => {
    await page.goto("/");

    await page.selectOption("#annotator", "joey");
    await page.getByPlaceholder("Enter password").fill("SEC-cyBERT");
    await page.getByRole("button", { name: "Sign In" }).click();

    await page.waitForURL("/dashboard");
    await expect(page.getByText("Welcome, Joey")).toBeVisible();
  });

  test("redirects unauthenticated users from dashboard to login", async ({
    page,
  }) => {
    await page.goto("/dashboard");
    await page.waitForURL("/");
    await expect(page.getByText("SEC cyBERT")).toBeVisible();
  });

  test("logout clears session", async ({ page }) => {
    await page.goto("/");
    await page.selectOption("#annotator", "joey");
    await page.getByPlaceholder("Enter password").fill("SEC-cyBERT");
    await page.getByRole("button", { name: "Sign In" }).click();
    await page.waitForURL("/dashboard");

    await page.getByRole("button", { name: "Logout" }).click();
    await page.waitForURL("/");

    await page.goto("/dashboard");
    await page.waitForURL("/");
  });
});
