import { describe, it, expect, afterEach, vi } from 'vitest';
import { cleanup, render } from 'vitest-browser-svelte';
import { page } from 'vitest/browser';
import StammbaumCard from './StammbaumCard.svelte';

vi.mock('$app/forms', () => ({ enhance: () => () => {} }));
vi.mock('$lib/person/relationship/RelationshipChip.svelte', () => ({ default: () => null }));
vi.mock('$lib/person/relationship/AddRelationshipForm.svelte', () => ({ default: () => null }));

afterEach(cleanup);

const baseProps = {
	personId: 'person-1',
	familyMember: false,
	relationships: [],
	inferredRelationships: [],
	canWrite: false
};

describe('StammbaumCard', () => {
	it('renders the section heading', async () => {
		render(StammbaumCard, baseProps);
		await expect.element(page.getByText('Stammbaum & Beziehungen')).toBeInTheDocument();
	});

	it('shows empty-relationships message when relationships list is empty', async () => {
		render(StammbaumCard, baseProps);
		await expect.element(page.getByText('Noch keine Beziehungen bekannt.')).toBeInTheDocument();
	});

	it('renders the family-member toggle when canWrite is true', async () => {
		render(StammbaumCard, { ...baseProps, canWrite: true });
		await expect.element(page.getByText('Als Familienmitglied')).toBeInTheDocument();
	});

	it('displays relationshipError text when provided', async () => {
		render(StammbaumCard, { ...baseProps, relationshipError: 'Test Fehler' });
		await expect.element(page.getByText('Test Fehler')).toBeInTheDocument();
	});

	it('toggle aria-label says "Zum Stammbaum hinzufügen" when not yet a family member', async () => {
		render(StammbaumCard, { ...baseProps, canWrite: true, familyMember: false });
		await expect
			.element(page.getByRole('switch', { name: 'Zum Stammbaum hinzufügen' }))
			.toBeInTheDocument();
	});

	it('toggle aria-label says "Aus Stammbaum entfernen" when already a family member', async () => {
		render(StammbaumCard, { ...baseProps, canWrite: true, familyMember: true });
		await expect
			.element(page.getByRole('switch', { name: 'Aus Stammbaum entfernen' }))
			.toBeInTheDocument();
	});
});
