/**
 * Password Reset Flow Tests
 *
 * Tests the password reset request flow.
 * Note: Full reset flow requires email access which is not available in E2E.
 */

import { test, expect, TEST_ACCOUNTS } from '@platform/e2e-auth';

test.describe('Password Reset Flow', () => {
  test.beforeEach(async ({ page }) => {
    await page.evaluate(() => {
      localStorage.clear();
      sessionStorage.clear();
    });
  });

  test('should display password reset form', async ({ page }) => {
    await page.goto('/forgot-password');

    // Should show email input
    await expect(page.locator('input[name="email"], [data-testid="email-input"], input[type="email"]')).toBeVisible();
  });

  test('should accept password reset request for existing email', async ({ page }) => {
    await page.goto('/forgot-password');

    // Enter existing test account email
    await page.fill('input[name="email"], [data-testid="email-input"], input[type="email"]', TEST_ACCOUNTS.worker.email);
    await page.click('button[type="submit"], [data-testid="submit-button"], [data-testid="reset-button"]');

    // Should show success message (even though we can't verify email)
    await expect(
      page.locator('[data-testid="success-message"], .success-message, [role="status"]')
    ).toBeVisible({ timeout: 10000 });
  });

  test('should accept password reset request for non-existent email (no enumeration)', async ({ page }) => {
    await page.goto('/forgot-password');

    // Enter non-existent email
    await page.fill('input[name="email"], [data-testid="email-input"], input[type="email"]', 'nonexistent@atlilith.test');
    await page.click('button[type="submit"], [data-testid="submit-button"], [data-testid="reset-button"]');

    // Should show same success message (prevents email enumeration)
    await expect(
      page.locator('[data-testid="success-message"], .success-message, [role="status"]')
    ).toBeVisible({ timeout: 10000 });
  });

  test('should request password reset via API', async ({ ssoApi }) => {
    // This should not throw - API always returns success
    await expect(
      ssoApi.requestPasswordReset(TEST_ACCOUNTS.worker.email)
    ).resolves.not.toThrow();
  });

  test('should have link to login from reset page', async ({ page }) => {
    await page.goto('/forgot-password');

    // Should have link back to login
    const loginLink = page.locator('a[href*="login"], [data-testid="login-link"]');
    await expect(loginLink).toBeVisible();
  });

  test('should have link to reset from login page', async ({ page }) => {
    await page.goto('/login');

    // Should have "forgot password" link
    const resetLink = page.locator('a[href*="forgot"], a[href*="reset"], [data-testid="forgot-password-link"]');
    await expect(resetLink).toBeVisible();
  });
});
