import { describe, it, expect, afterEach } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import SegmentationColumn from './SegmentationColumn.svelte';
import type { components } from '$lib/generated/api';

type TranscriptionQueueItemDTO = components['schemas']['TranscriptionQueueItemDTO'];

afterEach(cleanup);

function makeDoc(overrides: Partial<TranscriptionQueueItemDTO> = {}): TranscriptionQueueItemDTO {
	return {
		id: 'doc-1',
		title: 'Test Dokument',
		annotationCount: 0,
		textedBlockCount: 0,
		reviewedBlockCount: 0,
		contributors: [],
		hasMoreContributors: false,
		...overrides
	};
}

describe('SegmentationColumn', () => {
	it('renders document list when docs are provided', async () => {
		const doc1 = makeDoc({ id: 'doc-1', title: 'Brief an Maria' });
		const doc2 = makeDoc({ id: 'doc-2', title: 'Postkarte 1923' });

		render(SegmentationColumn, { props: { docs: [doc1, doc2], weeklyCount: 0 } });

		await expect.element(page.getByText('Brief an Maria')).toBeInTheDocument();
		await expect.element(page.getByText('Postkarte 1923')).toBeInTheDocument();
	});

	it('renders dashed empty state when docs array is empty', async () => {
		render(SegmentationColumn, { props: { docs: [], weeklyCount: 0 } });

		await expect
			.element(page.getByText('Alle Dokumente haben bereits Segmentierungsblöcke.'))
			.toBeInTheDocument();
	});

	it('shows weekly pulse when weeklyCount > 0', async () => {
		const doc = makeDoc({ id: 'doc-1', title: 'Brief' });

		render(SegmentationColumn, { props: { docs: [doc], weeklyCount: 3 } });

		await expect.element(page.getByText(/\+3 diese Woche/)).toBeInTheDocument();
	});

	it('does not show weekly pulse when weeklyCount is 0', async () => {
		const doc = makeDoc({ id: 'doc-1', title: 'Brief' });

		render(SegmentationColumn, { props: { docs: [doc], weeklyCount: 0 } });

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

	it('links to /documents/{id}?task=transcribe', async () => {
		const doc = makeDoc({ id: 'abc-123', title: 'Verlinktes Dokument' });

		render(SegmentationColumn, { props: { docs: [doc], weeklyCount: 0 } });

		const link = page.getByRole('link', { name: /Verlinktes Dokument/ });
		await expect.element(link).toHaveAttribute('href', '/documents/abc-123?task=transcribe');
	});
});
