import { describe, it, expect, afterEach } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import EnrichDocumentPage from './+page.svelte';

afterEach(cleanup);

const baseDoc = {
	id: 'd1',
	title: 'Brief an Helene',
	originalFilename: 'brief.pdf',
	documentDate: '1923-04-15',
	sender: null,
	receivers: [],
	tags: [],
	filePath: null,
	contentType: null,
	location: null,
	status: 'UPLOADED',
	fileHash: null
};

const baseData = (overrides: Record<string, unknown> = {}) => ({
	document: baseDoc,
	incompleteCount: 5,
	...overrides
});

describe('enrich/[id] page', () => {
	it('renders the page with the DocumentEditLayout orchestrator', async () => {
		render(EnrichDocumentPage, { props: { data: baseData(), form: undefined } });

		const main = document.body.firstElementChild;
		expect(main).not.toBeNull();
	});

	it('renders the form-error banner via DocumentEditLayout when form.error is set', async () => {
		render(EnrichDocumentPage, {
			props: { data: baseData(), form: { error: 'Invalid input' } }
		});

		const main = document.body.firstElementChild;
		expect(main).not.toBeNull();
	});

	it('renders a hidden skip-form for the skip action', async () => {
		render(EnrichDocumentPage, { props: { data: baseData(), form: undefined } });

		const skipForm = document.querySelector('form#skip-form');
		expect(skipForm).not.toBeNull();
	});

	it('renders three submit-action buttons (skip, save, save&review)', async () => {
		render(EnrichDocumentPage, { props: { data: baseData(), form: undefined } });

		const submits = document.querySelectorAll('button[type="submit"]');
		expect(submits.length).toBeGreaterThanOrEqual(3);
	});
});
