import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { resolve } from 'node:path' import { AnatomicalSheetDocumentSchema, ReportV2Schema, type AnatomicalSheetDocument, type FindingCategory, type FindingNode, type ObservationV2, type ReportV2, } from '../../src' /** * The legacy catalog as the application's archive knew it: every leaf of the * generated baseline (`legacy-import/document.json`) becomes a Report v2 * finding, grouped in one observation per zone. Same recipe as the app and * anatomy helpers; migrated through the draft map it yields the unresolved * items (sphenoid / ssb leaves) a Phase 0 golden was frozen with. */ const BASELINE = resolve( fileURLToPath(import.meta.url), '../../../../../supabase/seed/anatomical-sheets/equine/legacy-import/document.json', ) export interface LegacyLeaf { zoneId: string path: string[] name: { fr: string; en: string } vet: boolean } export function loadLegacyBaseline(): AnatomicalSheetDocument { return AnatomicalSheetDocumentSchema.parse( JSON.parse(readFileSync(BASELINE, 'utf8')), ) } export function legacyLeaves(doc: AnatomicalSheetDocument): LegacyLeaf[] { const out: LegacyLeaf[] = [] const walk = ( zoneId: string, nodes: readonly (FindingCategory | FindingNode)[], prefix: string[], ) => { for (const node of nodes) { const path = [...prefix, node.id] if (node.children.length > 0) walk(zoneId, node.children, path) else { const vet = 'requires_vet_referral' in node && node.requires_vet_referral out.push({ zoneId, path, name: node.name, vet }) } } } for (const zone of doc.finding_zones) walk(zone.id, zone.categories, []) return out } export function leavesAsV2Report(leaves: LegacyLeaf[]): ReportV2 { const byZone = new Map() for (const leaf of leaves) { let obs = byZone.get(leaf.zoneId) if (!obs) { obs = { id: `obs-${leaf.zoneId}`, zones: [], findings: [], freeTextFindings: [], comment: '', } byZone.set(leaf.zoneId, obs) } obs.findings.push({ zoneId: leaf.zoneId, path: leaf.path, labelFrSnapshot: leaf.name.fr, labelEnSnapshot: leaf.name.en || leaf.name.fr, ...(leaf.vet ? { requiresVetReferral: true } : {}), }) } return ReportV2Schema.parse({ version: 2, id: 'all-leaves', createdAt: '2026-10-10T00:00:00Z', date: '2026-10-10', practitioner: { name: 'Synthetic' }, animal: { speciesId: 'equine', name: 'Synthetic' }, owner: { name: 'Synthetic' }, observations: [...byZone.values()], conclusion: '', recommendations: '', }) }