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

test.describe("auth business flow", () => {
  test("register then login and reach protected area", async ({ page }) => {
    test.skip(!process.env.DATABASE_URL, "requires DATABASE_URL for write-flow verification")

    const id = Date.now()
    const email = `e2e.user.${id}@example.com`
    const password = "e2e-pass-123456"

    await page.goto("/register")
    await page.getByLabel("Full Name").fill("E2E User")
    await page.getByLabel("Email").fill(email)
    await page.getByLabel("Password").fill(password)
    await page.getByRole("button", { name: "Create Account" }).click()
    await expect(page).toHaveURL(/\/login(?:$|[/?#])/)

    await page.getByLabel("Email").fill(email)
    await page.getByLabel("Password").fill(password)
    await page.getByRole("button", { name: "Sign In with Email" }).click()

    await expect(page).toHaveURL(/\/(dashboard|student\/dashboard)(?:$|[/?#])/)
    const profileResponse = await page.goto("/profile")
    expect(profileResponse?.status() ?? 200).toBeLessThan(500)
    await expect(page).not.toHaveURL(/\/login(?:$|[/?#])/)
  })
})
