import { describe, it, expect } from 'vitest';
import { timelineMeta } from './timelineMeta';
import { makeEntry, makeYear, makeTimelineDTO } from './test-factories';

const letter = (id: string) => makeEntry({ kind: 'LETTER', documentId: id });
const event = (title: string) =>
	makeEntry({
		kind: 'EVENT',
		derived: true,
		derivedType: 'BIRTH',
		title,
		senderName: '',
		receiverName: '',
		documentId: undefined
	});

describe('timelineMeta', () => {
	it('counts letters and events across year bands and the undated bucket (REQ-002)', () => {
		const dto = makeTimelineDTO({
			years: [
				makeYear(1909, [letter('a'), event('Geburt'), letter('b')]),
				makeYear(1924, [event('Tod')])
			],
			undated: [letter('c'), event('Heirat')]
		});
		const meta = timelineMeta(dto);
		expect(meta.letterCount).toBe(3);
		expect(meta.eventCount).toBe(3);
	});

	it('reads the range from the first and last year band (REQ-002)', () => {
		const dto = makeTimelineDTO({
			years: [makeYear(1909, [letter('a')]), makeYear(1924, [letter('b')])]
		});
		const meta = timelineMeta(dto);
		expect(meta.firstYear).toBe(1909);
		expect(meta.lastYear).toBe(1924);
	});

	it('has a null range when there are no year bands, but still counts undated (REQ-002)', () => {
		const dto = makeTimelineDTO({ undated: [letter('a')] });
		const meta = timelineMeta(dto);
		expect(meta.firstYear).toBeNull();
		expect(meta.lastYear).toBeNull();
		expect(meta.letterCount).toBe(1);
	});

	it('reports zero counts and a null range for an empty timeline (REQ-002)', () => {
		expect(timelineMeta(makeTimelineDTO())).toEqual({
			firstYear: null,
			lastYear: null,
			letterCount: 0,
			eventCount: 0
		});
	});
});
