import { createHash } from 'node:crypto'
import { readFile, stat } 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-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyForce roundtrip check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-force-roundtrip' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
for (const [phase, snapshot] of [['nativeInitial', report.nativeInitial], ['nativeReopened', report.nativeAfter?.reopened], ['nativeResaved', report.nativeAfter?.resaved]]) {
  if (JSON.stringify(snapshot?.objectSet) !== JSON.stringify([{ name: 'ForceProbe', typeId: 'Fem::ConstraintForce' }, { name: 'RigidForceProbe', typeId: 'Fem::ConstraintRigidBody' }])) fail(`${phase} object set changed`)
  if (snapshot.force?.typeId !== 'Fem::ConstraintForce' || snapshot.force.property?.typeId !== 'App::PropertyForce' || snapshot.force.property.status?.length !== 0 || snapshot.force.shape?.applicable !== false) fail(`${phase} writable Force semantics are incomplete`)
  if (snapshot.rigid?.typeId !== 'Fem::ConstraintRigidBody' || snapshot.rigid.outputs?.map(({ name }) => name).join(',') !== 'ForceX,ForceY,ForceZ' || snapshot.rigid.outputs.some((output) => output.typeId !== 'App::PropertyForce' || JSON.stringify(output.status) !== '["27"]' || output.value?.numeric !== 0) || snapshot.rigid.shape?.applicable !== false) fail(`${phase} output Force semantics are incomplete`)
}
if (report.nativeInitial.force.property.value.numeric !== 0 || report.nativeAfter.reopened.force.property.value.numeric !== 500000 || report.nativeAfter.resaved.force.property.value.numeric !== 500000) fail('native Force values changed')
if (report.web.before?.typeId !== 'App::PropertyForce' || report.web.before.element !== 'Float' || Number(report.web.before.value) !== 0 || report.web.after?.typeId !== 'App::PropertyForce' || report.web.after.element !== 'Float' || Number(report.web.after.value) !== 500000) fail('Web did not decode and encode the Force Float payload')
if (report.web.objectSetPreserved !== true || report.web.semanticObjectsPreserved !== true || report.web.opaqueEntriesPreserved !== true || !Number.isSafeInteger(report.web.opaquePathsPreserved)) fail('Web edit did not preserve native archive semantics')
if (report.classification?.webValue !== 500000 || report.classification.reopenedValue !== 500000 || report.classification.resavedValue !== 500000 || report.classification.outputPropertiesPreserved !== true || report.classification.nativeOutputMatches !== true || report.classification.unknownSemanticDrift !== false || report.classification.zeroUnknownDrift !== true) fail('roundtrip classification is incomplete')
for (const archive of Object.values(report.archives ?? {})) {
  const path = resolve(root, archive.path ?? '')
  const content = await readFile(path)
  if ((await stat(path)).size !== archive.bytes || createHash('sha256').update(content).digest('hex') !== archive.sha256) fail(`archive is stale: ${archive.path}`)
}
console.log(JSON.stringify({ status: 'freecad-property-force-roundtrip-pass', values: { nativeInitial: report.nativeInitial.force.property.value.numeric, webEdited: report.classification.webValue, nativeReopened: report.classification.reopenedValue, nativeResaved: report.classification.resavedValue }, outputPropertiesPreserved: report.classification.outputPropertiesPreserved, opaquePathsPreserved: report.web.opaquePathsPreserved, zeroUnknownDrift: report.classification.zeroUnknownDrift }, null, 2))
