import { describe, it, expect, vi, afterEach } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';

import { createConfirmService, CONFIRM_KEY } from '$lib/shared/services/confirm.svelte.js';

const { default: DocumentEditPage } = await import('./+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,
	...overrides
});

describe('documents/[id]/edit page', () => {
	it('renders the page with the DocumentEditLayout', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: undefined }
		});

		// At minimum, the body has content
		const main = document.body.firstElementChild;
		expect(main).not.toBeNull();
	});

	it('renders both hidden submit-target forms', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: undefined }
		});

		const reviewForm = document.querySelector('form#mark-for-review-form');
		const deleteForm = document.querySelector('form#delete-form');
		expect(reviewForm).not.toBeNull();
		expect(deleteForm).not.toBeNull();
	});

	it('renders the action bar with delete, cancel, mark-for-review, and save buttons/links', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: undefined }
		});

		// Find delete button
		const deleteBtn = Array.from(document.querySelectorAll('button')).find((b) =>
			b.textContent?.includes('Löschen')
		);
		expect(deleteBtn).toBeDefined();
	});

	it('uses doc.title in the document title when set', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: undefined }
		});

		await vi.waitFor(() => expect(document.title).toContain('Brief an Helene'));
	});

	it('falls back to originalFilename when title is empty', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: {
				data: baseData({ document: { ...baseDoc, title: '', originalFilename: 'fallback.pdf' } }),
				form: undefined
			}
		});

		await vi.waitFor(() => expect(document.title).toContain('fallback.pdf'));
	});

	it('renders the cancel link to the document detail page', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: undefined }
		});

		const link = document.querySelector('a[href="/documents/d1"]');
		expect(link).not.toBeNull();
	});

	it('passes form.error to DocumentEditLayout when form is set', async () => {
		render(DocumentEditPage, {
			context: new Map([[CONFIRM_KEY, createConfirmService()]]),
			props: { data: baseData(), form: { error: 'Save failed' } }
		});

		// Layout shows the form error in a banner — at minimum, error text is in the DOM
		expect(document.body.textContent).toContain('Save failed');
	});
});
