import { describe, it, expect } from 'vitest';
import { buildLineageIndex, highlightLineage } from './highlightLineage';
import type { components } from '$lib/generated/api';

type RelationshipDTO = components['schemas']['RelationshipDTO'];

// Fixture builders mirror buildLayout.test.ts. Ids are plain readable strings —
// the traversal is id-agnostic, so UUIDs add no value here.
function parentEdge(parentId: string, childId: string, id = parentId + childId): RelationshipDTO {
	return {
		id,
		personId: parentId,
		relatedPersonId: childId,
		personDisplayName: '',
		relatedPersonDisplayName: '',
		relationType: 'PARENT_OF',
		fromDatePrecision: 'UNKNOWN',
		toDatePrecision: 'UNKNOWN'
	};
}

function spouseEdge(a: string, b: string, id = a + b): RelationshipDTO {
	return {
		id,
		personId: a,
		relatedPersonId: b,
		personDisplayName: '',
		relatedPersonDisplayName: '',
		relationType: 'SPOUSE_OF',
		fromDatePrecision: 'UNKNOWN',
		toDatePrecision: 'UNKNOWN'
	};
}

function siblingEdge(a: string, b: string, id = a + b): RelationshipDTO {
	return {
		id,
		personId: a,
		relatedPersonId: b,
		personDisplayName: '',
		relatedPersonDisplayName: '',
		relationType: 'SIBLING_OF',
		fromDatePrecision: 'UNKNOWN',
		toDatePrecision: 'UNKNOWN'
	};
}

function lineage(edges: RelationshipDTO[], rootId: string) {
	return highlightLineage(buildLineageIndex(edges), rootId);
}

function activeIds(edges: RelationshipDTO[], rootId: string): string[] {
	return [...lineage(edges, rootId).active].sort();
}

describe('highlightLineage — active set (#703)', () => {
	it('an isolated person highlights only themselves', () => {
		expect(activeIds([], 'root')).toEqual(['root']);
	});

	it('walks the full pedigree upward (parents and grandparents)', () => {
		const edges = [
			parentEdge('father', 'root'),
			parentEdge('mother', 'root'),
			parentEdge('fatherFather', 'father'),
			parentEdge('fatherMother', 'father'),
			parentEdge('motherFather', 'mother'),
			parentEdge('motherMother', 'mother')
		];
		expect(activeIds(edges, 'root')).toEqual([
			'father',
			'fatherFather',
			'fatherMother',
			'mother',
			'motherFather',
			'motherMother',
			'root'
		]);
	});

	it('walks the full descendant tree downward (children and grandchildren)', () => {
		const edges = [
			parentEdge('root', 'child'),
			parentEdge('child', 'grandchild'),
			parentEdge('child', 'grandchild2')
		];
		expect(activeIds(edges, 'root')).toEqual(['child', 'grandchild', 'grandchild2', 'root']);
	});

	it('activates all spouses of a blood person, including remarriages', () => {
		// root's father remarried (mother + secondWife); root married twice (s1 + s2).
		const edges = [
			parentEdge('father', 'root'),
			parentEdge('mother', 'root'),
			spouseEdge('father', 'mother'),
			spouseEdge('father', 'secondWife'),
			spouseEdge('root', 's1'),
			spouseEdge('root', 's2')
		];
		expect(activeIds(edges, 'root')).toEqual([
			'father',
			'mother',
			'root',
			's1',
			's2',
			'secondWife'
		]);
	});

	it('stops at a married-in spouse — the in-law is active but their parents stay dimmed', () => {
		const edges = [spouseEdge('root', 'inLaw'), parentEdge('inLawParent', 'inLaw')];
		const { active } = lineage(edges, 'root');
		expect(active.has('inLaw')).toBe(true);
		expect(active.has('inLawParent')).toBe(false);
	});

	it('never pulls a collateral relative into the active set via a SIBLING_OF edge', () => {
		const edges = [
			parentEdge('parent', 'root'),
			parentEdge('parent', 'sibling'),
			siblingEdge('root', 'sibling')
		];
		const { active } = lineage(edges, 'root');
		expect(active.has('parent')).toBe(true); // ancestor
		expect(active.has('sibling')).toBe(false); // collateral — reached only down from an ancestor, never walked
	});
});

describe('highlightLineage — connector predicate (#703)', () => {
	it('is active only when both joined people are active', () => {
		// root—inLaw (spouse), inLaw—inLawParent (parent). Only root + inLaw are active.
		const edges = [
			parentEdge('parent', 'root'),
			spouseEdge('root', 'inLaw'),
			parentEdge('inLawParent', 'inLaw')
		];
		const { isConnectorActive } = lineage(edges, 'root');
		expect(isConnectorActive('root', 'parent')).toBe(true); // active ↔ active
		expect(isConnectorActive('root', 'inLaw')).toBe(true); // active ↔ active spouse
		expect(isConnectorActive('inLaw', 'inLawParent')).toBe(false); // active spouse ↔ dimmed in-law parent
	});
});

describe('highlightLineage — cyclic data (REQ-STAMMBAUM-04 / AC10)', () => {
	it('terminates on a PARENT_OF cycle and resolves the cycle members as active', () => {
		// A is parent of B and B is parent of A — a cycle. The visited guard stops the
		// walk re-visiting; both cycle members are reachable from root and stay active.
		const edges = [parentEdge('a', 'b'), parentEdge('b', 'a')];
		expect(activeIds(edges, 'a')).toEqual(['a', 'b']);
	});

	it('terminates on a self-referential PARENT_OF edge', () => {
		const edges = [parentEdge('x', 'x')];
		expect(activeIds(edges, 'x')).toEqual(['x']);
	});
});
