import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const report = JSON.parse(await readFile(resolve(root, 'config/freecad-core-parameter-mutation-oracle.json'), 'utf8'))
const expected = ['Part::Box', 'Part::Cylinder', 'Part::Sphere', 'Part::Ellipsoid', 'Part::Cone', 'Part::Torus', 'Part::Helix', 'Part::Prism', 'Part::Wedge', 'Part::Fuse', 'Part::Cut', 'Part::Common']
const fail = (message) => { throw new Error(`FreeCAD core parameter mutation oracle: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.summary?.families !== expected.length || report.summary?.passed !== expected.length || report.summary?.shapeChanged !== expected.length || report.summary?.restoredExactly !== expected.length) fail('baseline or summary is invalid.')
const seen = new Set()
for (const entry of report.cases ?? []) {
  if (!expected.includes(entry.familyId) || seen.has(entry.familyId)) fail(`unexpected or duplicate family ${entry.familyId}.`)
  seen.add(entry.familyId)
  if (entry.status !== 'pass' || entry.parameterChanged !== true || entry.shapeChanged !== true || entry.restoredExactly !== true || !entry.propertyPath) fail(`${entry.familyId} lacks a completed parameter transaction.`)
  if (!entry.before?.shape?.valid || !entry.edited?.shape?.valid || !entry.restored?.shape?.valid || !entry.before.shape.brepSha256 || entry.before.shape.brepSha256 !== entry.restored.shape.brepSha256) fail(`${entry.familyId} lacks valid/restored Shape evidence.`)
  if (JSON.stringify(entry.before.parameterSnapshot) === JSON.stringify(entry.edited.parameterSnapshot) || JSON.stringify(entry.before.parameterSnapshot) !== JSON.stringify(entry.restored.parameterSnapshot)) fail(`${entry.familyId} parameter snapshots are inconsistent.`)
}
if (seen.size !== expected.length) fail(`expected ${expected.length} families, found ${seen.size}.`)
console.log(JSON.stringify({ status: 'freecad-core-parameter-mutation-pass', families: seen.size, shapeChanged: report.summary.shapeChanged, restoredExactly: report.summary.restoredExactly }, null, 2))
