import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getLocale } from '$lib/paraglide/runtime.js';
import { formatDocumentDate } from '$lib/shared/utils/documentDate';
import { timelineDateLabel } from './dateLabel';

// The mocked path MUST include the `.js` suffix to match the import in
// dateLabel.ts — a specifier mismatch silently skips the mock and the helper
// would resolve the real locale. We partially mock via importOriginal so only
// getLocale is stubbed: the shared formatter still pulls real Paraglide message
// exports (season words, range prefixes) off the same runtime module.
vi.mock('$lib/paraglide/runtime.js', async (importOriginal) => ({
	...(await importOriginal<typeof import('$lib/paraglide/runtime.js')>()),
	getLocale: vi.fn(() => 'de')
}));

describe('timelineDateLabel', () => {
	beforeEach(() => {
		// Default every test to German; locale-specific tests override below.
		vi.mocked(getLocale).mockReturnValue('de');
	});

	it('renders a DAY date localized in German (REQ-001/REQ-002)', () => {
		const label = timelineDateLabel('1916-07-28', 'DAY');
		expect(label).toContain('28');
		expect(label).toContain('Juli');
	});

	it('renders a DAY date localized in English (REQ-001/REQ-002)', () => {
		vi.mocked(getLocale).mockReturnValue('en');
		const label = timelineDateLabel('1916-07-28', 'DAY');
		expect(label).toContain('July');
	});

	it('renders a SEASON date with the German season word (REQ-001/REQ-002)', () => {
		const label = timelineDateLabel('1916-04-01', 'SEASON');
		expect(label).toContain('Frühling');
	});

	it('returns null for UNKNOWN precision even with a date (REQ-003)', () => {
		expect(timelineDateLabel('1916-07-28', 'UNKNOWN')).toBeNull();
	});

	it('returns null for UNKNOWN precision without a date (REQ-003)', () => {
		expect(timelineDateLabel(null, 'UNKNOWN')).toBeNull();
	});

	it('returns null for APPROX with a null eventDate, without calling the formatter (REQ-004)', () => {
		expect(timelineDateLabel(null, 'APPROX')).toBeNull();
	});

	it('returns null for DAY with an empty-string eventDate (REQ-004)', () => {
		expect(timelineDateLabel('', 'DAY')).toBeNull();
	});

	it('delegates a same-year RANGE to formatDocumentDate (REQ-001/REQ-002)', () => {
		const expected = formatDocumentDate('1914-01-01', 'RANGE', '1914-11-11', null, 'de');
		expect(timelineDateLabel('1914-01-01', 'RANGE', '1914-11-11')).toBe(expected);
	});

	it('treats undefined eventDateEnd identically to null for RANGE (REQ-004)', () => {
		expect(timelineDateLabel('1914-01-01', 'RANGE', undefined)).toBe(
			timelineDateLabel('1914-01-01', 'RANGE', null)
		);
	});
});
