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

afterEach(cleanup);

describe('CoCorrespondentsList', () => {
	it('renders nothing when the coCorrespondents list is empty', async () => {
		render(CoCorrespondentsList, {
			props: { coCorrespondents: [], personId: 'p-1', personName: 'Anna Schmidt' }
		});

		await expect
			.element(page.getByRole('heading', { name: /häufige korrespondenten/i }))
			.not.toBeInTheDocument();
	});

	it('renders the heading and search hint when there is at least one co-correspondent', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: 'Max Mustermann', count: 3 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		await expect
			.element(page.getByRole('heading', { name: /häufige korrespondenten/i }))
			.toBeVisible();
		await expect.element(page.getByText(m.person_correspondents_search_hint())).toBeVisible();
	});

	it('renders one chip per co-correspondent with name and count', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [
					{ id: 'c-1', name: 'Max Mustermann', count: 3 },
					{ id: 'c-2', name: 'Erika Beispiel', count: 1 }
				],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		await expect.element(page.getByText('Max Mustermann')).toBeVisible();
		await expect.element(page.getByText('Erika Beispiel')).toBeVisible();
		await expect.element(page.getByText('×3')).toBeVisible();
		await expect.element(page.getByText('×1')).toBeVisible();
	});

	it('points each chip to the document search pre-filtered by sender and receiver', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: 'Max Mustermann', count: 3 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		await expect
			.element(page.getByRole('link', { name: /max mustermann/i }))
			.toHaveAttribute('href', '/documents?senderId=p-1&receiverId=c-1');
	});

	it('labels the link as a search action naming both persons', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: 'Max Mustermann', count: 3 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		// Asserted against the de message (names both persons) — not retyped copy.
		const link = page.getByRole('link', { name: /max mustermann/i });
		await expect
			.element(link)
			.toHaveAttribute(
				'title',
				m.person_correspondents_search_title({ A: 'Anna Schmidt', B: 'Max Mustermann' })
			);
	});

	it('builds initials from up to two name parts', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: 'Max Mustermann Beispiel', count: 1 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		await expect.element(page.getByText('MM')).toBeVisible();
	});

	it('handles a single-word name (initials use just one letter)', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: 'Cher', count: 2 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		// "Cher" → initials "C"
		const initialsBadge = document.querySelector('span.flex.h-4.w-4');
		expect(initialsBadge?.textContent?.trim()).toBe('C');
	});

	it('handles a name part starting with empty string gracefully', async () => {
		render(CoCorrespondentsList, {
			props: {
				coCorrespondents: [{ id: 'c-1', name: '  Max', count: 1 }],
				personId: 'p-1',
				personName: 'Anna Schmidt'
			}
		});

		// Leading space → name.split(' ') yields ['', '', 'Max']; first chars are '', '', 'M' → 'M'
		const initialsBadge = document.querySelector('span.flex.h-4.w-4');
		expect(initialsBadge?.textContent?.trim()).toContain('M');
	});
});
