const { test, expect } = require('@playwright/test');
const AxeBuilder = require('@axe-core/playwright').default;

test.use({ viewport: { width: 1920, height: 1080 } });

test.describe('single-project', () => {
  test('project detail renders hero, sidebar, no axe violations', async ({ page }, testInfo) => {
    // Navigate to the projects archive and pick the first card. The test
    // fails (not skips) when no projects exist — silently skipping masks
    // regression of the single-project template itself.
    const response = await page.goto('/projects/');
    expect(response && response.status()).toBeLessThan(400);

    const firstLink = page.locator('.archive-projects__grid a.project-card__title-link').first();
    const linkCount = await firstLink.count();
    expect(linkCount, 'archive must contain at least one project card to exercise the single-project template').toBeGreaterThan(0);

    await Promise.all([
      page.waitForLoadState('networkidle'),
      firstLink.click(),
    ]);
    await page.screenshot({ path: 'test-results/single.png', fullPage: true });

    await expect(page.locator('.single-project__title')).toBeVisible();
    await expect(page.locator('.single-project__sidebar')).toBeVisible();

    const results = await new AxeBuilder({ page })
      .withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa', 'wcag22a', 'wcag22aa'])
      .analyze();
    await testInfo.attach('axe', {
      body: JSON.stringify(results, null, 2),
      contentType: 'application/json',
    });
    expect(results.violations).toEqual([]);
  });
});
