/**
 * Account Settings E2E Tests
 *
 * Tests the account settings functionality including:
 * - Profile management (name, email updates)
 * - Certificate email configuration
 * - Password change with validation
 * - API key management (view, copy, regenerate)
 * - Accessibility compliance
 *
 * @see /projects/Charon/docs/plans/phase4-settings-plan.md - Section 3.6
 * @see /projects/Charon/frontend/src/pages/Account.tsx
 */

import { test, expect, loginUser, TEST_PASSWORD } from '../fixtures/auth-fixtures';
import {
  waitForLoadingComplete,
  waitForToast,
  waitForModal,
  waitForAPIResponse,
} from '../utils/wait-helpers';
import { getCertificateValidationMessage } from '../utils/ui-helpers';

test.describe('Account Settings', () => {
  test.beforeEach(async ({ page, adminUser }) => {
    await loginUser(page, adminUser);
    await waitForLoadingComplete(page);
    await page.goto('/settings/account');
    await waitForLoadingComplete(page);
  });

  /**
   * PR-3: Account Route Redirect (F8)
   *
   * Verifies that legacy account settings routes redirect to the
   * consolidated Users page at /settings/users.
   */
  test.describe('PR-3: Account Route Redirect (F8)', () => {
    // Outer beforeEach already handles login. These tests re-navigate to the legacy
    // routes to assert the React Router <Navigate> redirects them to /settings/users.
    test('should redirect /settings/account to /settings/users', async ({ page }) => {
      await page.goto('/settings/account');
      await page.waitForURL(/\/settings\/users/, { timeout: 15000 });
      await expect(page).toHaveURL(/\/settings\/users/);
    });

    test('should redirect /settings/account-management to /settings/users', async ({ page }) => {
      await page.goto('/settings/account-management');
      await page.waitForURL(/\/settings\/users/, { timeout: 15000 });
      await expect(page).toHaveURL(/\/settings\/users/);
    });
  });

  /**
   * PR-3: Self-Service Profile via Users Page (F10)
   *
   * Verifies that an admin can manage their own profile (name, email,
   * password, API key) through the UserDetailModal on /settings/users.
   * This replaces the deleted Account.tsx page.
   */
  test.describe('PR-3: Self-Service Profile via Users Page (F10)', () => {
    test.beforeEach(async ({ page }) => {
      // Outer beforeEach already handles login. Navigate to the users page
      // and wait for the user data to fully render before each test.
      await page.goto('/settings/users');
      await waitForLoadingComplete(page);
      // Wait for user data to load — the My Profile card's Edit User button
      // only appears after the API returns the current user's profile.
      await page.getByRole('button', { name: 'Edit User' }).first().waitFor({
        state: 'visible',
        timeout: 15000,
      });
    });

    test('should open My Profile modal from the My Profile card', async ({ page }) => {
      await test.step('Click Edit User in the My Profile card', async () => {
        // The My Profile card button is the first "Edit User" button in the DOM
        await page.getByRole('button', { name: 'Edit User' }).first().click();
        await expect(page.getByRole('dialog')).toBeVisible();
      });

      await test.step('Verify dialog is labelled "My Profile"', async () => {
        await expect(
          page.getByRole('dialog').getByRole('heading', { name: 'My Profile' })
        ).toBeVisible();
      });

      await test.step('Verify name and email fields are editable', async () => {
        const dialog = page.getByRole('dialog');
        await expect(dialog.locator('input').first()).toBeVisible();
        await expect(dialog.locator('input[type="email"]')).toBeVisible();
      });
    });

    test('should display Change Password toggle in My Profile modal (self-only)', async ({ page }) => {
      await page.getByRole('button', { name: 'Edit User' }).first().click();
      await expect(page.getByRole('dialog')).toBeVisible();

      const dialog = page.getByRole('dialog');
      await expect(dialog.getByRole('button', { name: 'Change Password' })).toBeVisible();
    });

    test('should reveal password fields after clicking Change Password toggle', async ({ page }) => {
      await page.getByRole('button', { name: 'Edit User' }).first().click();
      await expect(page.getByRole('dialog')).toBeVisible();

      const dialog = page.getByRole('dialog');

      await test.step('Password fields are hidden before toggling', async () => {
        await expect(dialog.locator('#current-password')).not.toBeVisible();
        await expect(dialog.locator('#new-password')).not.toBeVisible();
      });

      await test.step('Click Change Password to expand the section', async () => {
        await dialog.getByRole('button', { name: 'Change Password' }).click();
      });

      await test.step('Password fields are now visible', async () => {
        await expect(dialog.locator('#current-password')).toBeVisible();
        await expect(dialog.locator('#new-password')).toBeVisible();
        await expect(dialog.locator('#confirm-password')).toBeVisible();
      });
    });

    test('should display API Key section in My Profile modal (self-only)', async ({ page }) => {
      await page.getByRole('button', { name: 'Edit User' }).first().click();
      await expect(page.getByRole('dialog')).toBeVisible();

      const dialog = page.getByRole('dialog');
      await expect(dialog.getByText('API Key', { exact: true })).toBeVisible();
      await expect(dialog.getByRole('button', { name: 'Regenerate API Key' })).toBeVisible();
    });

    test('should have accessible structure in My Profile modal', async ({ page }) => {
      await page.getByRole('button', { name: 'Edit User' }).first().click();
      const dialog = page.getByRole('dialog');
      await expect(dialog).toBeVisible();

      await test.step('Dialog has accessible heading', async () => {
        await expect(dialog.getByRole('heading', { name: 'My Profile' })).toBeVisible();
      });

      await test.step('Close button has accessible label', async () => {
        await expect(dialog.getByRole('button', { name: /close/i })).toBeVisible();
      });
    });
  });

  // These tests reference the deleted Account.tsx page (removed in PR-2b). Equivalent
  // functionality is covered by the "PR-3: Self-Service Profile via Users Page (F10)" suite above.
  test.describe.skip('Profile Management', () => {
    /**
     * Test: Profile displays correctly
     * Verifies that user profile information is displayed on load.
     */
    test('should display user profile', async ({ page, adminUser }) => {
      await test.step('Verify profile section is visible', async () => {
        const profileSection = page.locator('form').filter({
          has: page.locator('#profile-name'),
        });
        await expect(profileSection).toBeVisible();
      });

      await test.step('Verify name field contains user name', async () => {
        const nameInput = page.locator('#profile-name');
        await expect(nameInput).toBeVisible();
        const nameValue = await nameInput.inputValue();
        expect(nameValue.length).toBeGreaterThan(0);
      });

      await test.step('Verify email field contains user email', async () => {
        const emailInput = page.locator('#profile-email');
        await expect(emailInput).toBeVisible();
        await expect(emailInput).toHaveValue(adminUser.email);
      });
    });

    /**
     * Test: Update profile name successfully
     * Verifies that the name can be updated.
     */
    test('should update profile name', async ({ page }) => {
      const newName = `Updated Name ${Date.now()}`;

      await test.step('Update the name field', async () => {
        const nameInput = page.locator('#profile-name');
        await nameInput.clear();
        await nameInput.fill(newName);
      });

      await test.step('Save profile changes', async () => {
        const saveButton = page.getByRole('button', { name: /save.*profile/i });
        await saveButton.click();
      });

      await test.step('Verify success toast', async () => {
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /updated|saved|success/i })).toBeVisible({ timeout: 10000 });
      });

      await test.step('Verify name persisted after page reload', async () => {
        await page.reload();
        await waitForLoadingComplete(page);
        const nameInput = page.locator('#profile-name');
        await expect(nameInput).toHaveValue(newName);
      });
    });

    /**
     * Test: Update profile email
     * Verifies that the email can be updated (triggers password confirmation).
     */
    test('should update profile email', async ({ page }) => {
      const newEmail = `updated-${Date.now()}@test.local`;

      await test.step('Update the email field', async () => {
        const emailInput = page.locator('#profile-email');
        await emailInput.clear();
        await emailInput.fill(newEmail);
      });

      await test.step('Click save to trigger password prompt', async () => {
        const saveButton = page.getByRole('button', { name: /save.*profile/i });
        await saveButton.click();
      });

      await test.step('Verify password confirmation prompt appears', async () => {
        const passwordPrompt = page.locator('#confirm-current-password');
        await expect(passwordPrompt).toBeVisible({ timeout: 5000 });
      });

      await test.step('Enter password and confirm', async () => {
        await page.locator('#confirm-current-password').fill(TEST_PASSWORD);
        const confirmButton = page.getByRole('button', { name: /confirm.*update/i });
        await confirmButton.click();
      });

      await test.step('Handle email confirmation modal if present', async () => {
        // The modal asks if user wants to update certificate email too
        const yesButton = page.getByRole('button', { name: /yes.*update/i });
        if (await yesButton.isVisible({ timeout: 3000 }).catch(() => false)) {
          await yesButton.click();
        }
      });

      await test.step('Verify success toast', async () => {
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /updated|saved|success/i })).toBeVisible({ timeout: 10000 });
      });
    });

    /**
     * Test: Password required for email change
     * Verifies that changing email requires current password.
     */
    test('should require password for email change', async ({ page }) => {
      const newEmail = `change-email-${Date.now()}@test.local`;

      await test.step('Update the email field', async () => {
        const emailInput = page.locator('#profile-email');
        await emailInput.clear();
        await emailInput.fill(newEmail);
      });

      await test.step('Click save button', async () => {
        const saveButton = page.getByRole('button', { name: /save.*profile/i });
        await saveButton.click();
      });

      await test.step('Verify password prompt modal appears', async () => {
        const modal = page.locator('[class*="fixed"]').filter({
          has: page.locator('#confirm-current-password'),
        });
        await expect(modal).toBeVisible();

        const passwordInput = page.locator('#confirm-current-password');
        await expect(passwordInput).toBeVisible();
        await expect(passwordInput).toBeFocused();
      });
    });

    /**
     * Test: Email change confirmation dialog
     * Verifies that changing email shows certificate email confirmation.
     */
    test('should show email change confirmation dialog', async ({ page }) => {
      const newEmail = `confirm-dialog-${Date.now()}@test.local`;

      await test.step('Update the email field', async () => {
        const emailInput = page.locator('#profile-email');
        await emailInput.clear();
        await emailInput.fill(newEmail);
      });

      await test.step('Submit with password', async () => {
        const saveButton = page.getByRole('button', { name: /save.*profile/i });
        await saveButton.click();

        // Wait for password prompt and fill it
        const passwordInput = page.locator('#confirm-current-password');
        await expect(passwordInput).toBeVisible({ timeout: 5000 });
        await passwordInput.fill(TEST_PASSWORD);

        const confirmButton = page.getByRole('button', { name: /confirm.*update/i });
        await confirmButton.click();
      });

      await test.step('Verify email confirmation modal appears', async () => {
        // Modal should ask about updating certificate email - use heading role to avoid strict mode violation
        const emailConfirmModal = page.getByRole('heading', { name: /update.*cert.*email|certificate.*email/i });
        await expect(emailConfirmModal.first()).toBeVisible({ timeout: 5000 });

        // Should have options to update or keep current
        const yesButton = page.getByRole('button', { name: /yes/i });
        const noButton = page.getByRole('button', { name: /no|keep/i });
        await expect(yesButton.first()).toBeVisible();
        await expect(noButton.first()).toBeVisible();
      });
    });
  });

  // These tests reference Certificate Email UI elements (#useUserEmail, #cert-email) from the deleted
  // Account.tsx page (removed in PR-2b). Certificate email settings are not present in UsersPage.tsx.
  test.describe.skip('Certificate Email', () => {
    /**
     * Test: Toggle use account email checkbox
     * Verifies the checkbox toggles custom email field visibility.
     */
    test('should toggle use account email', async ({ page }) => {
      let wasInitiallyChecked = false;

      await test.step('Check initial checkbox state', async () => {
        const checkbox = page.locator('#useUserEmail');
        await expect(checkbox).toBeVisible();
        // Get current state - may be checked or unchecked depending on prior tests
        wasInitiallyChecked = await checkbox.isChecked();
      });

      await test.step('Toggle checkbox to opposite state', async () => {
        // Use getByRole for more reliable checkbox interaction with Radix UI
        const checkbox = page.getByRole('checkbox', { name: /use.*account.*email|same.*email/i });
        await checkbox.click({ force: true });

        // Wait a moment for state to update
        await page.waitForTimeout(100);

        // Should now be opposite of initial
        if (wasInitiallyChecked) {
          await expect(checkbox).not.toBeChecked({ timeout: 5000 });
        } else {
          await expect(checkbox).toBeChecked({ timeout: 5000 });
        }
      });

      await test.step('Verify custom email field visibility toggles', async () => {
        const certEmailInput = page.locator('#cert-email');
        // When unchecked, custom email field should be visible
        if (wasInitiallyChecked) {
          // We just unchecked it, so field should now be visible
          await expect(certEmailInput).toBeVisible({ timeout: 5000 });
        } else {
          // We just checked it, so field should now be hidden
          await expect(certEmailInput).not.toBeVisible({ timeout: 5000 });
        }
      });

      await test.step('Toggle back to original state', async () => {
        const checkbox = page.getByRole('checkbox', { name: /use.*account.*email|same.*email/i });
        await checkbox.click({ force: true });
        await page.waitForTimeout(100);

        if (wasInitiallyChecked) {
          await expect(checkbox).toBeChecked({ timeout: 5000 });
        } else {
          await expect(checkbox).not.toBeChecked({ timeout: 5000 });
        }
      });
    });

    /**
     * Test: Enter custom certificate email
     * Verifies custom email can be entered when account email is unchecked.
     */
    test('should enter custom certificate email', async ({ page }) => {
      const customEmail = `cert-${Date.now()}@custom.local`;

      await test.step('Ensure use account email is unchecked', async () => {
        // force: true required for Radix UI checkbox
        const checkbox = page.getByRole('checkbox', { name: /use.*account.*email|same.*email/i });

        // Only click if currently checked - clicking an unchecked box would check it
        const isCurrentlyChecked = await checkbox.isChecked();
        if (isCurrentlyChecked) {
          await checkbox.click({ force: true });
          await page.waitForTimeout(100);
        }
        await expect(checkbox).not.toBeChecked({ timeout: 5000 });
      });

      await test.step('Enter custom email', async () => {
        const certEmailInput = page.locator('#cert-email');
        await expect(certEmailInput).toBeVisible();
        await certEmailInput.clear();
        await certEmailInput.fill(customEmail);
        await expect(certEmailInput).toHaveValue(customEmail);
      });
    });

    /**
     * Test: Validate certificate email format
     * Verifies invalid email shows validation error.
     */
    test('should validate certificate email format', async ({ page }) => {
      // Flaky test - validation error element timing issue. Email validation logic works correctly.

      await test.step('Ensure use account email is unchecked', async () => {
        const checkbox = page.locator('#useUserEmail');
        const isChecked = await checkbox.isChecked();
        if (isChecked) {
          await checkbox.click({ force: true });
        }
        await expect(checkbox).not.toBeChecked({ timeout: 5000 });
      });

      await test.step('Verify custom email field is visible', async () => {
        const certEmailInput = page.locator('#cert-email');
        await expect(certEmailInput).toBeVisible({ timeout: 5000 });
      });

      await test.step('Enter invalid email', async () => {
        const certEmailInput = page.locator('#cert-email');
        await certEmailInput.clear();
        await certEmailInput.fill('not-a-valid-email');
      });

      await test.step('Verify validation error appears', async () => {
        // Try multiple selectors to find validation message
        const errorMessage = page.locator('#cert-email-error')
          .or(page.locator('[id*="cert-email"][id*="error"]'))
          .or(page.locator('text=/invalid.*email|email.*invalid/i').first());

        await expect(errorMessage).toBeVisible({ timeout: 5000 });
      });

      await test.step('Verify save button is disabled', async () => {
        const saveButton = page.getByRole('button', { name: /save.*certificate/i });

        // Wait for form state to fully update before checking attributes
        await page.waitForTimeout(500);

        // Verify button has the correct data attributes
        await expect(saveButton).toHaveAttribute('data-use-user-email', 'false', { timeout: 5000 });
        await expect(saveButton).toHaveAttribute('data-cert-email-valid', /(false|null)/, { timeout: 5000 });

        // Now verify the button is actually disabled
        await expect(saveButton).toBeDisabled();
      });
    });

    /**
     * Test: Save certificate email successfully
     * Verifies custom certificate email can be saved.
     */
    test('should save certificate email', async ({ page }) => {
      const customEmail = `cert-save-${Date.now()}@test.local`;

      await test.step('Ensure use account email is unchecked and enter custom', async () => {
        const checkbox = page.locator('#useUserEmail');
        const isChecked = await checkbox.isChecked();
        if (isChecked) {
          await checkbox.click();
        }
        await expect(checkbox).not.toBeChecked();

        const certEmailInput = page.locator('#cert-email');
        await expect(certEmailInput).toBeVisible({ timeout: 5000 });
        await certEmailInput.clear();
        await certEmailInput.fill(customEmail);
      });

      await test.step('Save certificate email using Promise.all pattern', async () => {
        const saveButton = page.getByRole('button', { name: /save.*certificate/i });
        // Use Promise.all to avoid race condition between click and response
        await Promise.all([
          page.waitForResponse(
            (resp) => resp.url().includes('/api/v1/settings') && resp.request().method() === 'POST'
          ),
          saveButton.click(),
        ]);
      });

      await test.step('Verify success toast', async () => {
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /updated|saved|success/i })).toBeVisible({ timeout: 10000 });
      });

      await test.step('Verify email persisted after reload', async () => {
        await page.reload();
        await waitForLoadingComplete(page);

        const checkbox = page.locator('#useUserEmail');
        await expect(checkbox).not.toBeChecked();

        const certEmailInput = page.locator('#cert-email');
        await expect(certEmailInput).toHaveValue(customEmail);
      });
    });
  });

  // These tests reference password fields (#current-password, #new-password, #confirm-password)
  // from the deleted Account.tsx page. In UsersPage.tsx these fields are inside the UserDetailModal
  // (only visible after clicking Edit User → Change Password). Equivalent coverage is provided by
  // the 'PR-3: Self-Service Profile via Users Page (F10)' suite above.
  test.describe.skip('Password Change', () => {
    /**
     * Test: Change password with valid inputs
     * Verifies password can be changed successfully.
     *
     * Note: This test changes the password but the user fixture is per-test,
     * so it won't affect other tests.
     */
    test('should change password with valid inputs', async ({ page }) => {
      const newPassword = 'NewSecurePass456!';

      await test.step('Fill current password', async () => {
        const currentPasswordInput = page.locator('#current-password');
        await expect(currentPasswordInput).toBeVisible();
        await currentPasswordInput.fill(TEST_PASSWORD);
      });

      await test.step('Fill new password', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.fill(newPassword);
      });

      await test.step('Fill confirm password', async () => {
        const confirmPasswordInput = page.locator('#confirm-password');
        await confirmPasswordInput.fill(newPassword);
      });

      await test.step('Submit password change', async () => {
        const updateButton = page.getByRole('button', { name: /update.*password/i });
        await updateButton.click();
      });

      await test.step('Verify success toast', async () => {
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /updated|changed|success/i })).toBeVisible({ timeout: 10000 });
      });

      await test.step('Verify password fields are cleared', async () => {
        const currentPasswordInput = page.locator('#current-password');
        const newPasswordInput = page.locator('#new-password');
        const confirmPasswordInput = page.locator('#confirm-password');

        await expect(currentPasswordInput).toHaveValue('');
        await expect(newPasswordInput).toHaveValue('');
        await expect(confirmPasswordInput).toHaveValue('');
      });
    });

    /**
     * Test: Validate current password is required
     * Verifies that wrong current password shows error.
     */
    test('should validate current password', async ({ page }) => {
      await test.step('Fill incorrect current password', async () => {
        const currentPasswordInput = page.locator('#current-password');
        await currentPasswordInput.fill('WrongPassword123!');
      });

      await test.step('Fill valid new password', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.fill('NewPass789!');

        const confirmPasswordInput = page.locator('#confirm-password');
        await confirmPasswordInput.fill('NewPass789!');
      });

      await test.step('Submit and verify error', async () => {
        const updateButton = page.getByRole('button', { name: /update.*password/i });
        await updateButton.click();

        // Error toast uses role="alert" (with data-testid fallback)
        const errorToast = page.locator('[data-testid="toast-error"]')
          .or(page.getByRole('alert'))
          .filter({ hasText: /incorrect|invalid|wrong|failed/i });
        await expect(errorToast.first()).toBeVisible({ timeout: 10000 });
      });
    });

    /**
     * Test: Validate password strength requirements
     * Verifies weak passwords are rejected.
     */
    test('should validate password strength', async ({ page }) => {
      await test.step('Fill current password', async () => {
        const currentPasswordInput = page.locator('#current-password');
        await currentPasswordInput.fill(TEST_PASSWORD);
      });

      await test.step('Enter weak password', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.fill('weak');
      });

      await test.step('Verify strength meter shows weak', async () => {
        // Password strength meter component should be visible
        const strengthMeter = page.locator('[class*="strength"], [data-testid*="strength"]');
        if (await strengthMeter.isVisible()) {
          // Look for weak/poor indicator
          const weakIndicator = strengthMeter.getByText(/weak|poor|too short/i);
          await expect(weakIndicator).toBeVisible().catch(() => {
            // Some implementations use colors instead of text
          });
        }
      });
    });

    /**
     * Test: Validate password confirmation must match
     * Verifies mismatched passwords show error.
     */
    test('should validate password confirmation match', async ({ page }) => {
      await test.step('Fill current password', async () => {
        const currentPasswordInput = page.locator('#current-password');
        await currentPasswordInput.fill(TEST_PASSWORD);
      });

      await test.step('Enter mismatched passwords', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.fill('NewPassword123!');

        const confirmPasswordInput = page.locator('#confirm-password');
        await confirmPasswordInput.fill('DifferentPassword456!');
      });

      await test.step('Verify mismatch error appears', async () => {
        // Click elsewhere to trigger validation
        await page.locator('body').click();

        const errorMessage = page.getByText(/do.*not.*match|passwords.*match|mismatch/i);
        await expect(errorMessage).toBeVisible();
      });
    });

    /**
     * Test: Password strength meter is displayed
     * Verifies strength meter updates as password is typed.
     * Note: Skip if password strength meter component is not implemented.
     */
    test('should show password strength meter', async ({ page }) => {
      await test.step('Start typing new password', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.fill('a');
      });

      await test.step('Verify strength meter appears or skip if not implemented', async () => {
        // Look for password strength component - may not be implemented
        const strengthMeter = page.locator('[class*="strength"], [class*="meter"], [data-testid*="password-strength"]');
        const isVisible = await strengthMeter.isVisible({ timeout: 3000 }).catch(() => false);

        if (!isVisible) {
          // Password strength meter not implemented - return
          return;
        }

        await expect(strengthMeter).toBeVisible();
      });

      await test.step('Verify strength updates with stronger password', async () => {
        const newPasswordInput = page.locator('#new-password');
        await newPasswordInput.clear();
        await newPasswordInput.fill('VeryStr0ng!Pass#2024');

        // Strength meter should show stronger indication
        const strengthMeter = page.locator('[class*="strength"], [class*="meter"]');
        if (await strengthMeter.isVisible()) {
          // Check for strong/good indicator (text or aria-label)
          const text = await strengthMeter.textContent();
          const ariaLabel = await strengthMeter.getAttribute('aria-label');
          const hasStrongIndicator =
            text?.match(/strong|good|excellent/i) ||
            ariaLabel?.match(/strong|good|excellent/i);

          // Some implementations use colors, so we just verify the meter exists and updates
          expect(text?.length || ariaLabel?.length).toBeGreaterThan(0);
        }
      });
    });
  });

  // These tests reference API key elements from the deleted Account.tsx page. In UsersPage.tsx
  // the API key section is inside the UserDetailModal (only visible after clicking Edit User).
  // Equivalent coverage is provided by the 'PR-3: Self-Service Profile via Users Page (F10)' suite above.
  test.describe.skip('API Key Management', () => {
    /**
     * Test: API key is displayed
     * Verifies API key section shows the key value.
     */
    test('should display API key', async ({ page }) => {
      await test.step('Verify API key section is visible', async () => {
        // Find the API Key heading (h3) which is inside the Card component
        const apiKeyHeading = page.getByRole('heading', { name: /api.*key/i });
        await expect(apiKeyHeading).toBeVisible();
      });

      await test.step('Verify API key input exists and has value', async () => {
        // API key is in a readonly input with font-mono class
        const apiKeyInput = page.locator('input[readonly].font-mono');
        await expect(apiKeyInput).toBeVisible();
        const keyValue = await apiKeyInput.inputValue();
        expect(keyValue.length).toBeGreaterThan(0);
      });
    });

    /**
     * Test: Copy API key to clipboard
     * Verifies copy button copies key to clipboard.
     */
    test('should not expose API key copy action when key is masked', async ({ page }) => {
      await test.step('Verify API key input is masked and read-only', async () => {
        const apiKeyInput = page.locator('input[readonly].font-mono');
        await expect(apiKeyInput).toBeVisible();
        await expect(apiKeyInput).toHaveValue(/^\*+$/);
      });

      await test.step('Verify no copy-to-clipboard control is present in API key section', async () => {
        const apiKeyCard = page.locator('h3').filter({ hasText: /api.*key/i }).locator('..').locator('..');

        await expect(
          apiKeyCard
            .getByRole('button', { name: /copy/i })
            .or(apiKeyCard.getByTitle(/copy/i))
            .or(apiKeyCard.locator('button:has(svg.lucide-copy)'))
        ).toHaveCount(0);
      });
    });

    /**
     * Test: Regenerate API key
     * Verifies API key can be regenerated.
     */
    test('should regenerate API key', async ({ page }) => {
      let originalKey: string;

      await test.step('Get original API key', async () => {
        const apiKeyInput = page
          .locator('input[readonly]')
          .filter({ has: page.locator('[class*="mono"]') })
          .or(page.locator('input.font-mono'))
          .or(page.locator('input[readonly]').last());

        originalKey = await apiKeyInput.inputValue();
      });

      await test.step('Click regenerate button and wait for response', async () => {
        const regenerateButton = page
          .getByRole('button')
          .filter({ has: page.locator('svg.lucide-refresh-cw') })
          .or(page.getByRole('button', { name: /regenerate/i }))
          .or(page.getByTitle(/regenerate/i));

        // Use Promise.all to set up response listener BEFORE clicking
        await Promise.all([
          page.waitForResponse(
            (resp) => resp.url().includes('/api/v1/user/api-key') && resp.request().method() === 'POST'
          ),
          regenerateButton.click(),
        ]);
      });

      await test.step('Verify success toast', async () => {
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /regenerated|generated|new.*key/i })).toBeVisible({ timeout: 10000 });
      });

      await test.step('Verify API key rotation succeeded without revealing raw key', async () => {
        const apiKeyInput = page
          .locator('input[readonly]')
          .filter({ has: page.locator('[class*="mono"]') })
          .or(page.locator('input.font-mono'))
          .or(page.locator('input[readonly]').last());

        const newKey = await apiKeyInput.inputValue();
        expect(newKey).toBe('********');
        expect(newKey).toBe(originalKey);
        expect(newKey.length).toBeGreaterThan(0);
      });
    });

    /**
     * Test: Confirm API key regeneration
     * Verifies regeneration has proper feedback.
     */
    test('should confirm API key regeneration', async ({ page }) => {
      await test.step('Click regenerate button', async () => {
        const regenerateButton = page
          .getByRole('button')
          .filter({ has: page.locator('svg.lucide-refresh-cw') })
          .or(page.getByRole('button', { name: /regenerate/i }))
          .or(page.getByTitle(/regenerate/i));

        await regenerateButton.click();
      });

      await test.step('Verify regeneration feedback', async () => {
        // Wait for loading state on button
        const regenerateButton = page
          .getByRole('button')
          .filter({ has: page.locator('svg.lucide-refresh-cw') })
          .or(page.getByRole('button', { name: /regenerate/i }));

        // Button may show loading indicator or be disabled briefly
        // Then success toast should appear
        const toast = page.getByRole('status').or(page.getByRole('alert'));
        await expect(toast.filter({ hasText: /regenerated|generated|success/i })).toBeVisible({ timeout: 10000 });
      });
    });
  });

  // These tests reference form labels and IDs (#profile-name, #profile-email, #useUserEmail)
  // from the deleted Account.tsx page (removed in PR-2b). Accessibility of the replacement UI
  // is covered by the 'PR-3: Self-Service Profile via Users Page (F10)' suite above.
  test.describe.skip('Accessibility', () => {
    /**
     * Test: Keyboard navigation through account settings
     * Uses increased loop counts and waitForTimeout for CI reliability
     */
    test('should be keyboard navigable', async ({ page }) => {
      await test.step('Tab through profile section', async () => {
        // Start from first focusable element
        await page.keyboard.press('Tab');
        await page.waitForTimeout(150);

        // Tab to profile name
        const nameInput = page.locator('#profile-name');
        let foundName = false;

        for (let i = 0; i < 30; i++) {
          if (await nameInput.evaluate((el) => el === document.activeElement)) {
            foundName = true;
            break;
          }
          await page.keyboard.press('Tab');
          await page.waitForTimeout(150);
        }

        expect(foundName).toBeTruthy();
      });

      await test.step('Tab through password section', async () => {
        const currentPasswordInput = page.locator('#current-password');
        let foundPassword = false;

        for (let i = 0; i < 35; i++) {
          if (await currentPasswordInput.evaluate((el) => el === document.activeElement)) {
            foundPassword = true;
            break;
          }
          await page.keyboard.press('Tab');
          await page.waitForTimeout(150);
        }

        expect(foundPassword).toBeTruthy();
      });

      await test.step('Tab through API key section', async () => {
        // Should be able to reach copy/regenerate buttons
        let foundApiButton = false;

        for (let i = 0; i < 10; i++) {
          await page.keyboard.press('Tab');
          await page.waitForTimeout(100);
          const focused = page.locator(':focus');
          const role = await focused.getAttribute('role').catch(() => null);
          const tagName = await focused.evaluate((el) => el.tagName.toLowerCase()).catch(() => '');

          if (tagName === 'button' && await focused.locator('svg.lucide-copy, svg.lucide-refresh-cw').isVisible().catch(() => false)) {
            foundApiButton = true;
            break;
          }
        }

        // API key buttons should be reachable
        expect(foundApiButton || true).toBeTruthy(); // Non-blocking assertion
      });
    });

    /**
     * Test: Form labels are properly associated
     * Verifies all form inputs have proper labels.
     */
    test('should have proper form labels', async ({ page }) => {
      await test.step('Verify profile name has label', async () => {
        const nameLabel = page.locator('label[for="profile-name"]');
        await expect(nameLabel).toBeVisible();
      });

      await test.step('Verify profile email has label', async () => {
        const emailLabel = page.locator('label[for="profile-email"]');
        await expect(emailLabel).toBeVisible();
      });

      await test.step('Verify certificate email checkbox has label', async () => {
        const checkboxLabel = page.locator('label[for="useUserEmail"]');
        await expect(checkboxLabel).toBeVisible();
      });

      await test.step('Verify password fields have labels', async () => {
        const currentPasswordLabel = page.locator('label[for="current-password"]');
        const newPasswordLabel = page.locator('label[for="new-password"]');
        const confirmPasswordLabel = page.locator('label[for="confirm-password"]');

        await expect(currentPasswordLabel).toBeVisible();
        await expect(newPasswordLabel).toBeVisible();
        await expect(confirmPasswordLabel).toBeVisible();
      });

      await test.step('Verify required fields are indicated', async () => {
        // Required fields should have visual indicator (asterisk or aria-required)
        const requiredFields = page.locator('[aria-required="true"], label:has-text("*")');
        const count = await requiredFields.count();
        expect(count).toBeGreaterThan(0);
      });
    });
  });
});
