import { expect, test } from "@playwright/test";
import { API_URL, fetchLatestToken, registerVerifyLogin, uniqueEmail } from "../helpers/api";

test.describe("§15.7 scenario 3: Password reset", () => {
  test("forgot password → reset → login with new password", async ({ request }) => {
    const email = uniqueEmail("e2e-reset");
    const oldPassword = "Valid1234";
    const newPassword = "ResetValid1";

    await registerVerifyLogin(request, email, oldPassword);

    const forgot = await request.post(`${API_URL}/api/v1/auth/forgot-password`, {
      data: { email }
    });
    expect(forgot.ok()).toBeTruthy();

    const token = await fetchLatestToken(request, email, "reset_password");
    const reset = await request.post(`${API_URL}/api/v1/auth/reset-password`, {
      data: { token, new_password: newPassword }
    });
    expect(reset.ok()).toBeTruthy();

    const oldLogin = await request.post(`${API_URL}/api/v1/auth/login`, {
      data: { email, password: oldPassword }
    });
    expect(oldLogin.status()).toBe(401);

    const newLogin = await request.post(`${API_URL}/api/v1/auth/login`, {
      data: { email, password: newPassword }
    });
    expect(newLogin.ok()).toBeTruthy();
  });
});
