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

const PERSON_ID = '00000000-0000-0000-0000-000000000001';
const SPOUSE_ID = '00000000-0000-0000-0000-000000000002';
const PARENT_ID = '00000000-0000-0000-0000-000000000003';

describe('PersonRelationshipsCard', () => {
	it('hides an inferred relationship that is already a direct one', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: SPOUSE_ID,
					personDisplayName: 'Anna Müller',
					relatedPersonDisplayName: 'Bertha Müller',
					relationType: 'SPOUSE_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: [
				{
					person: {
						id: SPOUSE_ID,
						displayName: 'Bertha Müller',
						familyMember: true
					},
					label: 'SPOUSE',
					hops: 1
				}
			]
		});

		const matches = await page.getByText('Bertha Müller').all();
		expect(matches).toHaveLength(1);
	});

	it('still renders inferred relationships that are not direct', async () => {
		const COUSIN_ID = '00000000-0000-0000-0000-000000000003';
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [],
			inferredRelationships: [
				{
					person: { id: COUSIN_ID, displayName: 'Carla Cousine', familyMember: true },
					label: 'COUSIN',
					hops: 4
				}
			]
		});

		await expect.element(page.getByText('Carla Cousine')).toBeInTheDocument();
	});

	it('shows Elternteil-von chip when personId is the PARENT_OF subject', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: PARENT_ID,
					personDisplayName: 'Anna Müller',
					relatedPersonDisplayName: 'Kind Müller',
					relationType: 'PARENT_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: []
		});

		await expect.element(page.getByText('Elternteil von')).toBeInTheDocument();
	});

	it('chip labels use text-xs (≥12px) not text-[10px] — WCAG readable font size', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: SPOUSE_ID,
					personDisplayName: 'Anna',
					relatedPersonDisplayName: 'Bertha',
					relationType: 'SPOUSE_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: [
				{
					person: { id: PARENT_ID, displayName: 'Großmutter', familyMember: true },
					label: 'Großmutter',
					hops: 2
				}
			]
		});
		const chips = document.querySelectorAll<HTMLElement>('li span.rounded-full');
		expect(chips.length).toBeGreaterThan(0);
		chips.forEach((chip) => {
			expect(chip.classList.contains('text-xs')).toBe(true);
			expect(chip.classList.contains('text-[10px]')).toBe(false);
		});
	});

	it('shows Kind-von chip when personId is the PARENT_OF object', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r2',
					personId: PARENT_ID,
					relatedPersonId: PERSON_ID,
					personDisplayName: 'Eltern Müller',
					relatedPersonDisplayName: 'Anna Müller',
					relationType: 'PARENT_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: []
		});

		await expect.element(page.getByText('Kind von')).toBeInTheDocument();
	});

	it('renders the date range at its stored precision', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: SPOUSE_ID,
					personDisplayName: 'Anna Müller',
					relatedPersonDisplayName: 'Bertha Müller',
					relationType: 'SPOUSE_OF',
					fromDate: '1923-05-12',
					fromDatePrecision: 'DAY',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: []
		});

		await expect
			.element(page.getByTestId('relationship-date-range'))
			.toHaveTextContent('12. Mai 1923');
	});

	it('shows the notes line', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: SPOUSE_ID,
					personDisplayName: 'Anna Müller',
					relatedPersonDisplayName: 'Bertha Müller',
					relationType: 'SPOUSE_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN',
					notes: 'Hochzeit in Berlin'
				}
			],
			inferredRelationships: []
		});

		await expect
			.element(page.getByTestId('relationship-notes'))
			.toHaveTextContent('Hochzeit in Berlin');
	});

	it('renders no date line when the relationship has no dates', async () => {
		render(PersonRelationshipsCard, {
			personId: PERSON_ID,
			relationships: [
				{
					id: 'r1',
					personId: PERSON_ID,
					relatedPersonId: SPOUSE_ID,
					personDisplayName: 'Anna Müller',
					relatedPersonDisplayName: 'Bertha Müller',
					relationType: 'SPOUSE_OF',
					fromDatePrecision: 'UNKNOWN',
					toDatePrecision: 'UNKNOWN'
				}
			],
			inferredRelationships: []
		});

		await expect.element(page.getByText('Bertha Müller')).toBeInTheDocument();
		expect(document.querySelector('[data-testid="relationship-date-range"]')).toBeNull();
	});
});
