import { describe, expect, it } from 'vitest';
import { m } from '$lib/paraglide/messages.js';
import { chipLabel, otherName } from './relationshipLabels';
import type { components } from '$lib/generated/api';

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

const ALICE_ID = 'alice-uuid';
const BOB_ID = 'bob-uuid';

function makeRel(
	relationType: RelationshipDTO['relationType'],
	override: Partial<RelationshipDTO> = {}
): RelationshipDTO {
	return {
		id: 'rel-1',
		personId: ALICE_ID,
		relatedPersonId: BOB_ID,
		personDisplayName: 'Alice',
		relatedPersonDisplayName: 'Bob',
		relationType,
		fromDatePrecision: 'UNKNOWN',
		toDatePrecision: 'UNKNOWN',
		...override
	};
}

describe('chipLabel', () => {
	it('returns parent_of when perspective is the subject of PARENT_OF', () => {
		const rel = makeRel('PARENT_OF');
		expect(chipLabel(rel, ALICE_ID)).toBe(m.relation_parent_of());
	});

	it('returns child_of when perspective is the object of PARENT_OF', () => {
		const rel = makeRel('PARENT_OF');
		expect(chipLabel(rel, BOB_ID)).toBe(m.relation_child_of());
	});

	it('returns spouse_of for SPOUSE_OF regardless of perspective', () => {
		const rel = makeRel('SPOUSE_OF');
		expect(chipLabel(rel, ALICE_ID)).toBe(m.relation_spouse_of());
		expect(chipLabel(rel, BOB_ID)).toBe(m.relation_spouse_of());
	});

	it('returns sibling_of for SIBLING_OF', () => {
		expect(chipLabel(makeRel('SIBLING_OF'), ALICE_ID)).toBe(m.relation_sibling_of());
	});

	it('returns friend for FRIEND', () => {
		expect(chipLabel(makeRel('FRIEND'), ALICE_ID)).toBe(m.relation_friend());
	});

	it('returns other for OTHER', () => {
		expect(chipLabel(makeRel('OTHER'), ALICE_ID)).toBe(m.relation_other());
	});
});

describe('otherName', () => {
	it('returns relatedPersonDisplayName when perspective is the subject', () => {
		const rel = makeRel('PARENT_OF');
		expect(otherName(rel, ALICE_ID)).toBe('Bob');
	});

	it('returns personDisplayName when perspective is the object', () => {
		const rel = makeRel('PARENT_OF');
		expect(otherName(rel, BOB_ID)).toBe('Alice');
	});
});
