import { expect, test } from "@playwright/test";
import {
  loginViaUi,
  registerUser,
  uniqueEmail,
  verifyEmail
} from "../helpers/api";

test.describe("§15.7 scenario 2: Auth full journey", () => {
  test("register → verify → login → profile edit → change password → logout", async ({
    page,
    request
  }) => {
    const email = uniqueEmail("e2e-flow");
    const password = "Valid1234";
    const newPassword = "NewValid1";

    await registerUser(request, email, password);
    await verifyEmail(request, email);

    await loginViaUi(page, email, password);
    await expect(page).toHaveURL(/\/profile$/);
    await expect(page.getByText(email)).toBeVisible();

    await page.getByLabel("Display name").fill("E2E User");
    await page.getByRole("button", { name: "Save name" }).click();
    await expect(page.getByText("Profile updated")).toBeVisible();

    await page.getByPlaceholder("Current password").fill(password);
    await page.getByPlaceholder("New password").fill(newPassword);
    await page.getByRole("button", { name: "Change password" }).click();
    await expect(page.getByText("Password changed")).toBeVisible();

    await page.getByRole("button", { name: "Logout" }).click();
    await expect(page).toHaveURL(/\/login$/);

    await loginViaUi(page, email, newPassword);
    await expect(page).toHaveURL(/\/profile$/);
  });
});
