/**
 * Accessibility tests — WCAG 2.2 AA compliance via axe-core (issue #191).
 *
 * Scans public pages for accessibility violations using @axe-core/playwright.
 * Zero violations at WCAG 2.2 AA level are required to pass CI.
 */

import { test, expect } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';

// Public pages that don't require authentication
const PUBLIC_PAGES = [
  { path: '/', name: 'Home' },
  { path: '/pricing', name: 'Pricing' },
  { path: '/contact', name: 'Contact' },
  { path: '/auth/login', name: 'Login' },
  { path: '/auth/register', name: 'Register' },
  { path: '/auth/forgot-password', name: 'Forgot Password' },
  { path: '/legal/privacy', name: 'Privacy Policy' },
  { path: '/legal/terms', name: 'Terms of Service' },
  { path: '/docs', name: 'Documentation' },
];

test.describe('accessibility (WCAG 2.2 AA)', () => {
  for (const { path, name } of PUBLIC_PAGES) {
    test(`${name} (${path}) has no accessibility violations`, async ({ page }) => {
      await page.goto(path, { waitUntil: 'networkidle' });

      const results = await new AxeBuilder({ page })
        .withTags(['wcag2a', 'wcag2aa', 'wcag22aa'])
        .analyze();

      // Log violations for debugging (visible in CI output)
      if (results.violations.length > 0) {
        const summary = results.violations.map((v) => ({
          id: v.id,
          impact: v.impact,
          description: v.description,
          nodes: v.nodes.length,
          help: v.helpUrl,
        }));
        console.log(`Accessibility violations on ${path}:`, JSON.stringify(summary, null, 2));
      }

      expect(
        results.violations,
        `${name} (${path}) has ${results.violations.length} accessibility violations`,
      ).toEqual([]);
    });
  }

  test('auth error page is accessible', async ({ page }) => {
    await page.goto('/auth/error?error=Default', { waitUntil: 'networkidle' });

    const results = await new AxeBuilder({ page })
      .withTags(['wcag2a', 'wcag2aa', 'wcag22aa'])
      .analyze();

    expect(results.violations).toEqual([]);
  });
});
