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

const TEST_USER = {
  email: 'admin@flatlogic.com',
  password: 'flatlogicAdmin123!',
};

async function authenticateViaPage(page: Page): Promise<void> {
  await page.goto('/');
  await page.getByPlaceholder('you@school.edu').fill(TEST_USER.email);
  await page.getByPlaceholder('Enter your password').fill(TEST_USER.password);
  await page.getByRole('button', { name: 'Sign In', exact: true }).click();
  await page.waitForURL('/', { timeout: 10000 });
}

async function checkAccessibility(page: Page, pageName: string): Promise<void> {
  const results = await new AxeBuilder({ page })
    .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa'])
    .analyze();

  const violations = results.violations.map((v) => ({
    id: v.id,
    impact: v.impact,
    description: v.description,
    nodes: v.nodes.length,
  }));

  expect(
    violations,
    `Accessibility violations on ${pageName}:\n${JSON.stringify(violations, null, 2)}`,
  ).toEqual([]);
}

test.describe('accessibility compliance', () => {
  test('login page has no critical accessibility violations', async ({ page }) => {
    await page.goto('/');
    await checkAccessibility(page, 'Login Page');
  });

  test('dashboard has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Dashboard');
  });

  test('classroom timer page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/classroom-timer');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Classroom Timer');
  });

  test('classroom support page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/classroom-support');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Classroom Support');
  });

  test('sign language page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/sign-language');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Sign Language');
  });

  test('zones of regulation page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/zones-of-regulation');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Zones of Regulation');
  });

  test('frame weekly page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/frame');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'F.R.A.M.E. Weekly');
  });

  test('behavior management page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/qbs-safety');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Behavior Management');
  });

  test('emotional intelligence page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/emotional-intelligence');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Emotional Intelligence');
  });

  test('attendance page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/attendance');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Attendance');
  });

  test('parent communication page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/parent-communication');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Parent Communication');
  });

  test('internal alerts page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/internal-alerts');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Internal Alerts');
  });

  test('safety protocols page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/safety-protocols');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Safety Protocols');
  });

  test('handbook policies page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/handbook-policies');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Handbook & Policies');
  });

  test('community partnerships page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/community-partnerships');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Community & Partnerships');
  });

  test('vocational opportunities page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/vocational-opportunities');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Vocational Opportunities');
  });

  test('esa funding page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/esa-funding');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'ESA Funding Info');
  });

  test('walkthrough page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/walkthrough');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Walk-Through Check-In');
  });

  test('director dashboard page has no critical accessibility violations', async ({ page }) => {
    await authenticateViaPage(page);
    await page.goto('/director-dashboard');
    await page.waitForLoadState('networkidle');
    await checkAccessibility(page, 'Director Dashboard');
  });
});
