import { afterEach, describe, expect, it } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import DescriptionSection from './DescriptionSection.svelte';
import { m } from '$lib/paraglide/messages.js';

afterEach(() => cleanup());

describe('DescriptionSection — onMount seeding (Felix B1/B2 fix regression fence)', () => {
	it('pre-fills the title input from initialTitle when currentTitle is empty', async () => {
		render(DescriptionSection, { initialTitle: 'Brief an Anna' });
		const titleInput = document.querySelector('input#title') as HTMLInputElement;
		expect(titleInput).not.toBeNull();
		expect(titleInput.value).toBe('Brief an Anna');
	});

	it('does not stomp a parent-bound currentTitle that is already non-empty', async () => {
		render(DescriptionSection, {
			currentTitle: 'Parent Title',
			initialTitle: 'Should Not Win'
		});
		const titleInput = document.querySelector('input#title') as HTMLInputElement;
		expect(titleInput.value).toBe('Parent Title');
	});

	it('always renders archiveBox + archiveFolder fields regardless of editMode', async () => {
		render(DescriptionSection, { editMode: false });
		expect(document.querySelector('[data-testid="description-archive-box"]')).not.toBeNull();
		expect(document.querySelector('[data-testid="description-archive-folder"]')).not.toBeNull();
	});

	it('renders the editMode-only archiveBox + archiveFolder fields when editMode=true', async () => {
		render(DescriptionSection, { editMode: true, hideTitle: true });
		expect(document.querySelector('[data-testid="description-archive-box"]')).not.toBeNull();
		expect(document.querySelector('[data-testid="description-archive-folder"]')).not.toBeNull();
	});

	it('pre-fills archiveBox from initialArchiveBox when archiveBox is empty', async () => {
		render(DescriptionSection, { initialArchiveBox: 'K-03', hideTitle: true });
		const input = document.querySelector('input#archiveBox') as HTMLInputElement;
		expect(input.value).toBe('K-03');
	});

	it('pre-fills archiveFolder from initialArchiveFolder when archiveFolder is empty', async () => {
		render(DescriptionSection, { initialArchiveFolder: 'Mappe B', hideTitle: true });
		const input = document.querySelector('input#archiveFolder') as HTMLInputElement;
		expect(input.value).toBe('Mappe B');
	});

	it('does not stomp a parent-bound archiveBox that is already non-empty', async () => {
		render(DescriptionSection, {
			archiveBox: 'Parent Value',
			initialArchiveBox: 'Should Not Win',
			hideTitle: true
		});
		const input = document.querySelector('input#archiveBox') as HTMLInputElement;
		expect(input.value).toBe('Parent Value');
	});
});

describe('DescriptionSection — auto-generated title helper (FR-TITLE-005)', () => {
	it('shows the helper with the localized text and wires aria-describedby when showTitleHelp is set', async () => {
		render(DescriptionSection, { showTitleHelp: true });
		const help = document.querySelector('#title-help') as HTMLElement;
		expect(help).not.toBeNull();
		expect(help.textContent?.trim()).toBe(m.form_helper_title_autogenerated());
		// ≥14px for the 60+ audience (FR-005 prefers a larger size than the 12px field hints).
		expect(help.classList.contains('text-sm')).toBe(true);
		const titleInput = document.querySelector('input#title') as HTMLInputElement;
		expect(titleInput.getAttribute('aria-describedby')).toBe('title-help');
	});

	it('omits the helper by default (e.g. the new-document form)', async () => {
		render(DescriptionSection, {});
		expect(document.querySelector('#title-help')).toBeNull();
		const titleInput = document.querySelector('input#title') as HTMLInputElement;
		expect(titleInput.getAttribute('aria-describedby')).toBeNull();
	});

	it('omits the helper when the title field is hidden (bulk edit)', async () => {
		render(DescriptionSection, { showTitleHelp: true, hideTitle: true });
		expect(document.querySelector('#title-help')).toBeNull();
	});
});
