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

afterEach(cleanup);

const makeDoc = (overrides: Record<string, unknown> = {}) => ({
	id: 'd1',
	title: 'Brief 1923',
	documentDate: '1923-04-15',
	textedBlockCount: 0,
	annotationCount: 10,
	contributors: [],
	hasMoreContributors: false,
	...overrides
});

describe('TranscriptionColumn', () => {
	it('renders the empty placeholder when docs is empty', async () => {
		render(TranscriptionColumn, { props: { docs: [], weeklyCount: 0 } });

		await expect.element(page.getByText(/Keine Dokumente warten/i)).toBeVisible();
	});

	it('renders the heading when docs has items', async () => {
		render(TranscriptionColumn, { props: { docs: [makeDoc()], weeklyCount: 0 } });

		await expect.element(page.getByRole('heading', { name: /text transkribieren/i })).toBeVisible();
	});

	it('renders the weekly pulse when weeklyCount > 0', async () => {
		render(TranscriptionColumn, { props: { docs: [makeDoc()], weeklyCount: 5 } });

		await expect.element(page.getByText(/diese Woche/i)).toBeVisible();
	});

	it('hides the weekly pulse when weeklyCount is 0', async () => {
		render(TranscriptionColumn, { props: { docs: [makeDoc()], weeklyCount: 0 } });

		await expect.element(page.getByText(/diese Woche/i)).not.toBeInTheDocument();
	});

	it('shows the block progress label when textedBlockCount > 0', async () => {
		render(TranscriptionColumn, {
			props: {
				docs: [makeDoc({ textedBlockCount: 3, annotationCount: 10 })],
				weeklyCount: 0
			}
		});

		await expect.element(page.getByText('3 / 10 Blöcke')).toBeVisible();
	});

	it('shows the em-dash placeholder when textedBlockCount is 0', async () => {
		render(TranscriptionColumn, { props: { docs: [makeDoc()], weeklyCount: 0 } });

		expect(document.body.textContent).toContain('—');
	});

	it('renders the document title as a link with task=transcribe query', async () => {
		render(TranscriptionColumn, { props: { docs: [makeDoc()], weeklyCount: 0 } });

		await expect
			.element(page.getByRole('link', { name: /brief 1923/i }))
			.toHaveAttribute('href', '/documents/d1?task=transcribe');
	});

	it('omits the date when documentDate is undefined', async () => {
		render(TranscriptionColumn, {
			props: { docs: [makeDoc({ documentDate: undefined })], weeklyCount: 0 }
		});

		// formatMCDate should not be called; just verify component renders
		await expect.element(page.getByRole('link', { name: /brief 1923/i })).toBeVisible();
	});
});
