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

function buildAxe(page: Parameters<typeof AxeBuilder>[0]['page']) {
	return new AxeBuilder({ page }).withTags(['wcag2a', 'wcag2aa']);
}

test.describe('Richtlinien page — content', () => {
	test.beforeEach(async ({ page }) => {
		await page.goto('/hilfe/transkription');
	});

	test('renders h1 title, intro, five rules, four chips, closing card', async ({ page }) => {
		await expect(
			page.getByRole('heading', { level: 1, name: /Transkriptions-Richtlinien/ })
		).toBeVisible();
		await expect(page.getByText(/Damit alle Briefe einheitlich/)).toBeVisible();
		await expect(page.getByText('Nicht lesbare Wörter')).toBeVisible();
		await expect(page.getByText('Durchgestrichene Wörter')).toBeVisible();
		await expect(page.getByText(/Das lange s/)).toBeVisible();
		await expect(page.getByText('Unsichere Namen')).toBeVisible();
		await expect(page.getByText(/Dialekt/)).toBeVisible();
		await expect(page.getByText('Abkürzungen')).toBeVisible();
		await expect(page.getByText('Datumsformate')).toBeVisible();
		await expect(page.getByText(/Fehlt eine Regel/)).toBeVisible();
	});

	test('Wikipedia link opens in new tab with annotation', async ({ page }) => {
		const wikiLink = page.getByRole('link', { name: /Wikipedia/ });
		await expect(wikiLink).toHaveAttribute('target', '_blank');
		await expect(wikiLink).toHaveAttribute('rel', 'noopener noreferrer');
		await expect(wikiLink).toHaveAttribute('referrerpolicy', 'no-referrer');
		await expect(wikiLink).toContainText(/öffnet in neuem Tab/);
	});
});

test.describe('Richtlinien page — accessibility', () => {
	for (const viewport of [320, 768, 1440]) {
		test(`axe: light theme at ${viewport}px — no WCAG 2.1 AA violations`, async ({ page }) => {
			await page.setViewportSize({ width: viewport, height: 800 });
			await page.goto('/hilfe/transkription');
			const a11y = await buildAxe(page).analyze();
			expect(a11y.violations, JSON.stringify(a11y.violations, null, 2)).toHaveLength(0);
		});

		test(`axe: dark theme at ${viewport}px — no WCAG 2.1 AA violations`, async ({ page }) => {
			await page.setViewportSize({ width: viewport, height: 800 });
			await page.goto('/hilfe/transkription');
			await page.getByRole('button', { name: /Farbmodus|theme/i }).click();
			const a11y = await buildAxe(page).analyze();
			expect(a11y.violations, JSON.stringify(a11y.violations, null, 2)).toHaveLength(0);
		});
	}
});

test.describe('Richtlinien page — print media', () => {
	test('print snapshot hides nav, annotation chip, and new-tab spans', async ({ page }) => {
		await page.emulateMedia({ media: 'print' });
		await page.goto('/hilfe/transkription');

		const nav = page.locator('.app-nav');
		if ((await nav.count()) > 0) {
			await expect(nav).toBeHidden();
		}

		// .new-tab annotation spans must be hidden in print so "(öffnet in neuem Tab)"
		// text does not clutter the printed output (the print CSS declares display:none)
		for (const span of await page.locator('.new-tab').all()) {
			await expect(span).toBeHidden();
		}

		await page.screenshot({ path: 'test-results/e2e/richtlinien-print.png', fullPage: true });
	});
});
