import { test, expect } from '@playwright/test';

// Tests skipped until Playwright Chromium is installed in CI — see issue #363.
test.describe('Stammbaum — issue #358', () => {
	test.skip();

	test('/stammbaum renders the page heading', async ({ page }) => {
		await page.goto('/stammbaum');
		await expect(page.getByRole('heading', { name: 'Stammbaum' })).toBeVisible();
	});

	test('/stammbaum either shows an empty state or at least one node', async ({ page }) => {
		// Plan journey 3 (empty branch) and journey 1 (populated branch) covered jointly:
		// the test passes whenever the page renders one of the two coherent states.
		await page.goto('/stammbaum');
		const empty = page.getByRole('heading', { name: 'Noch keine Familienmitglieder' });
		const anyNode = page.locator('svg[role="img"][aria-label="Stammbaum"] g[role="button"]');
		await expect(async () => {
			const emptyVisible = await empty.isVisible().catch(() => false);
			const nodeCount = await anyNode.count();
			expect(emptyVisible || nodeCount > 0).toBe(true);
		}).toPass();

		if (await empty.isVisible().catch(() => false)) {
			await expect(page.getByRole('link', { name: /Zur Personenliste/ })).toBeVisible();
		}
	});

	test('person edit Stammbaum card surfaces the year-range error', async ({ page }) => {
		// Plan task 36: Bis < Von triggers the inline error and keeps the form unsubmitted.
		// We pick the first person, open the edit page, expand the add-rel form, and
		// inspect the validation message bound to the Bis field.
		await page.goto('/persons');
		const firstPerson = page.locator('a[href^="/persons/"]').first();
		await firstPerson.click();
		await expect(page).toHaveURL(/\/persons\/[^/]+/);
		await page.goto(page.url() + '/edit');

		// Open the add-rel form
		const addBtn = page.getByRole('button', { name: /Beziehung hinzufügen/i });
		await addBtn.click();

		// Enter Von 1935, Bis 1920 → expect the year-range error
		const fromInput = page.locator('input[name="fromYear"]');
		const toInput = page.locator('input[name="toYear"]');
		await fromInput.fill('1935');
		await toInput.fill('1920');

		await expect(page.locator('#add-rel-year-error')).toBeVisible();
		await expect(page.locator('#add-rel-year-error')).toContainText(/Bis.*Von|nicht vor/i);
	});
});
