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-heatflux-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyHeatFlux roundtrip check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-heatflux-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: 'HeatFluxProbe', typeId: 'Fem::ConstraintHeatflux' }])) fail(`${phase} object set changed`)
  if (snapshot.heatFlux?.typeId !== 'Fem::ConstraintHeatflux' || snapshot.heatFlux.property?.name !== 'DFlux' || snapshot.heatFlux.property.typeId !== 'App::PropertyHeatFlux' || snapshot.heatFlux.property.group !== 'ConstraintHeatflux' || snapshot.heatFlux.property.status?.length !== 0 || snapshot.heatFlux.shape?.applicable !== false) fail(`${phase} writable HeatFlux semantics are incomplete`)
}
if (report.nativeInitial.heatFlux.property.value.numeric !== 0 || report.nativeAfter.reopened.heatFlux.property.value.numeric !== 500 || report.nativeAfter.resaved.heatFlux.property.value.numeric !== 500) fail('native HeatFlux values changed')
if (report.web.before?.typeId !== 'App::PropertyHeatFlux' || report.web.before.element !== 'Float' || Number(report.web.before.value) !== 0 || report.web.after?.typeId !== 'App::PropertyHeatFlux' || report.web.after.element !== 'Float' || Number(report.web.after.value) !== 500) fail('Web did not decode and encode the HeatFlux 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 !== 500 || report.classification.reopenedValue !== 500 || report.classification.resavedValue !== 500 || report.classification.nativeValueMatches !== 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-heatflux-roundtrip-pass', values: { nativeInitial: report.nativeInitial.heatFlux.property.value.numeric, webEdited: report.classification.webValue, nativeReopened: report.classification.reopenedValue, nativeResaved: report.classification.resavedValue }, opaquePathsPreserved: report.web.opaquePathsPreserved, zeroUnknownDrift: report.classification.zeroUnknownDrift }, null, 2))
