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-property-force-mutation.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyForce mutation check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-force-mutation' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
if (report.object?.name !== 'ForceProbe' || report.object.typeId !== 'Fem::ConstraintForce' || report.property?.name !== 'Force' || report.property.typeId !== 'App::PropertyForce') fail('native object/property identity changed')
if (!(report.recompute?.initial > 0) || !(report.recompute.edit > 0) || !(report.recompute.restore > 0)) fail('initial, edit and restore were not recomputed')
for (const [phase, expected] of [['before', 0], ['edited', 500000], ['restored', 0]]) {
  const snapshot = report[phase]
  if (snapshot?.value?.numeric !== expected || typeof snapshot.value.unit !== 'string' || !snapshot.value.unit.includes('mm*kg/s^2') || JSON.stringify(snapshot.propertyStatus) !== '[]' || JSON.stringify(snapshot.editorMode) !== '[]' || snapshot.shape?.applicable !== false || !Array.isArray(snapshot.objectState)) fail(`${phase} native semantic state is invalid`)
  if (snapshot.reversed !== false || snapshot.enableAmplitude !== false || JSON.stringify(snapshot.amplitudeValues) !== '["0, 0","1, 1"]' || JSON.stringify(snapshot.directionVector) !== '[0,0,1]' || !snapshot.propertyNames?.includes('Force')) fail(`${phase} neighboring FEM properties changed`)
}
if (report.touchedAfterEdit?.value?.numeric !== 500000 || JSON.stringify(report.touchedAfterEdit.objectState) !== '["Touched"]' || report.touchedAfterEdit.mustExecute !== false) fail('edit did not preserve native touched state')
if (report.touchedAfterRestore?.value?.numeric !== 0 || JSON.stringify(report.touchedAfterRestore.objectState) !== '["Touched"]' || report.touchedAfterRestore.mustExecute !== false) fail('restore did not preserve native touched state')
if (report.neighbors?.preserved !== true || JSON.stringify(report.neighbors.before) !== JSON.stringify(report.neighbors.edited) || JSON.stringify(report.neighbors.before) !== JSON.stringify(report.neighbors.restored)) fail('neighbor properties drifted')
if (report.document?.objectsPreserved !== true || JSON.stringify(report.document.objectsBefore) !== JSON.stringify(report.document.objectsRestored) || report.document.objectsBefore?.length !== 1) fail('mutation polluted the document object set')
if (report.classification?.propertyChanged !== true || report.classification.propertyRestored !== true || report.classification.semanticStateRestored !== true || report.classification.neighborPropertiesPreserved !== true || report.classification.geometry !== 'not-applicable-no-shape-property') fail('mutation/restore classification is incomplete')
console.log(JSON.stringify({ status: 'freecad-property-force-mutation-pass', recompute: report.recompute, values: { before: report.before.value.numeric, edited: report.edited.value.numeric, restored: report.restored.value.numeric }, neighborsPreserved: report.neighbors.preserved, semanticStateRestored: report.classification.semanticStateRestored, geometry: report.classification.geometry }, null, 2))
