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

afterEach(cleanup);

// ─── Test data ────────────────────────────────────────────────────────────────

const baseData = {
	user: undefined,
	canWrite: true,
	canAnnotate: false,
	canBlogWrite: false,
	persons: [],
	initialSenderId: '',
	initialSenderName: '',
	initialReceivers: []
};

// ─── Prefill – sender ─────────────────────────────────────────────────────────

describe('New document page – sender prefill', () => {
	it('shows an empty sender input when no senderId is in the URL', async () => {
		render(Page, { data: baseData });
		const input = document.querySelector<HTMLInputElement>('#senderId-search');
		expect(input?.value).toBe('');
	});

	it('shows the sender name in the typeahead input when initialSenderName is set', async () => {
		render(Page, {
			data: { ...baseData, initialSenderId: 'p1', initialSenderName: 'Hans Müller' }
		});
		const input = document.querySelector<HTMLInputElement>('#senderId-search');
		expect(input?.value).toBe('Hans Müller');
	});

	it('sets the hidden senderId input to the prefilled ID', async () => {
		render(Page, {
			data: { ...baseData, initialSenderId: 'p1', initialSenderName: 'Hans Müller' }
		});
		const hidden = document.querySelector<HTMLInputElement>(
			'input[type="hidden"][name="senderId"]'
		);
		expect(hidden?.value).toBe('p1');
	});
});

// ─── Prefill – receiver ───────────────────────────────────────────────────────

describe('New document page – receiver prefill', () => {
	it('shows no receiver chips when initialReceivers is empty', async () => {
		render(Page, { data: baseData });
		await expect.element(page.getByText('Anna Schmidt')).not.toBeInTheDocument();
	});

	it('shows a receiver chip when initialReceivers has a person', async () => {
		const data = {
			...baseData,
			initialReceivers: [
				{ id: 'p2', firstName: 'Anna', lastName: 'Schmidt', displayName: 'Anna Schmidt' }
			]
		};
		render(Page, { data });
		await expect.element(page.getByText('Anna Schmidt')).toBeInTheDocument();
	});

	it('renders a hidden receiverIds input for the prefilled receiver', async () => {
		const data = {
			...baseData,
			initialReceivers: [
				{ id: 'p2', firstName: 'Anna', lastName: 'Schmidt', displayName: 'Anna Schmidt' }
			]
		};
		render(Page, { data });
		const hidden = document.querySelector<HTMLInputElement>('input[name="receiverIds"]');
		expect(hidden?.value).toBe('p2');
	});
});
