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

test("health endpoint returns status ok", async ({ request }) => {
  const res = await request.get("/api/health");
  expect(res.status()).toBe(200);
  const body = await res.json() as { status: string };
  expect(body.status).toBe("ok");
});

test("unauthenticated root redirects to signin", async ({ page }) => {
  await page.goto("/");
  await expect(page).toHaveURL(/\/auth\/signin/);
});

test("signin page renders credential inputs and submit button", async ({ page }) => {
  await page.goto("/auth/signin");
  await expect(page.locator('input[type="email"]')).toBeVisible();
  await expect(page.locator('input[type="password"]')).toBeVisible();
  await expect(page.locator('button[type="submit"]')).toBeVisible();
});

test("admin login succeeds and redirects away from signin", async ({ page }) => {
  await page.goto("/auth/signin");
  await page.fill('input[type="email"]', "admin@capakraken.dev");
  await page.fill('input[type="password"]', "admin123");
  await page.click('button[type="submit"]');
  await expect(page).not.toHaveURL(/\/auth\/signin/, { timeout: 15_000 });
});

test("authenticated user sees app shell nav", async ({ page }) => {
  await page.goto("/auth/signin");
  await page.fill('input[type="email"]', "admin@capakraken.dev");
  await page.fill('input[type="password"]', "admin123");
  await page.click('button[type="submit"]');
  await expect(page).not.toHaveURL(/\/auth\/signin/, { timeout: 15_000 });
  await expect(page.locator("nav").first()).toBeVisible({ timeout: 10_000 });
});
