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-acceleration-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyAcceleration roundtrip check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-acceleration-roundtrip' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
for (const phase of ['nativeInitial', 'nativeAfter']) {
  const snapshot = phase === 'nativeInitial' ? report.nativeInitial : report.nativeAfter?.reopened
  if (snapshot?.object?.typeId !== 'Robot::TrajectoryDressUpObject' || snapshot.object.propertyTypeId !== 'App::PropertyAcceleration' || snapshot.object.source?.typeId !== 'Robot::TrajectoryObject' || snapshot.object.useAcceleration !== true || JSON.stringify(snapshot.object.propertyStatus) !== '[]' || JSON.stringify(snapshot.object.editorMode) !== '[]' || snapshot.object.trajectory?.waypointCount !== 2 || snapshot.sourceTrajectory?.waypointCount !== 2) fail(`${phase} native object semantics are incomplete`)
}
if (report.nativeInitial.object.value.numeric !== 1000 || report.nativeInitial.object.trajectory.waypoints.some((waypoint) => waypoint.acceleration !== 1000) || report.nativeAfter.reopened.object.value.numeric !== 500 || report.nativeAfter.resaved.object.value.numeric !== 500 || report.nativeAfter.reopened.object.trajectory.waypoints.some((waypoint) => waypoint.acceleration !== 500) || report.nativeAfter.resaved.object.trajectory.waypoints.some((waypoint) => waypoint.acceleration !== 500)) fail('native initial/reopen/resave values drifted')
if (report.nativeAfter.reopened.sourceTrajectory.contentSha256 !== report.nativeAfter.resaved.sourceTrajectory.contentSha256 || report.nativeAfter.reopened.object.trajectory.contentSha256 !== report.nativeAfter.resaved.object.trajectory.contentSha256) fail('FreeCAD resave did not preserve native trajectory semantics')
if (report.web.before?.typeId !== 'App::PropertyAcceleration' || report.web.before?.element !== 'Float' || Number(report.web.before?.value) !== 1000 || report.web.after?.typeId !== 'App::PropertyAcceleration' || report.web.after?.element !== 'Float' || report.web.after?.value !== '500' || report.web.objectSetPreserved !== true || report.web.semanticObjectsPreserved !== true || report.web.opaqueEntriesPreserved !== true || !Number.isSafeInteger(report.web.opaquePathsPreserved) || report.web.opaquePathsPreserved < 0) fail('Web edit did not preserve native archive semantics')
if (report.classification?.webValue !== '500' || report.classification.reopenedValue !== 500 || report.classification.resavedValue !== 500 || report.classification.nativeOutputMatches !== true || report.classification.unknownSemanticDrift !== false || report.classification.zeroUnknownDrift !== true) fail('round-trip classification is not exact for PropertyAcceleration')
console.log(JSON.stringify({ status: 'freecad-property-acceleration-roundtrip-pass', values: { nativeInitial: report.nativeInitial.object.value.numeric, webEdited: Number(report.classification.webValue), nativeReopened: report.classification.reopenedValue, nativeResaved: report.classification.resavedValue }, opaquePathsPreserved: report.web.opaquePathsPreserved, semanticObjectsPreserved: report.web.semanticObjectsPreserved, zeroUnknownDrift: report.classification.zeroUnknownDrift }, null, 2))
