import { isDeepStrictEqual } from 'node:util'
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-boollist-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyBoolList roundtrip check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-boollist-roundtrip' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
const target = [true, false, true, false]
for (const [phase, snapshot] of [['nativeInitial', report.nativeInitial], ['nativeReopened', report.nativeAfter?.reopened], ['nativeResaved', report.nativeAfter?.resaved]]) {
  if (snapshot?.object?.typeId !== 'App::FeatureTest' || snapshot.object.propertyTypeId !== 'App::PropertyBoolList' || !isDeepStrictEqual(snapshot.object.propertyStatus, []) || !isDeepStrictEqual(snapshot.object.editorMode, []) || snapshot.object.hasShapeProperty !== false || !isDeepStrictEqual(snapshot.objectSet, [{ name: 'BoolListProbe', typeId: 'App::FeatureTest' }])) fail(`${phase} native object semantics are incomplete`)
}
if (!isDeepStrictEqual(report.nativeInitial.object.value, [false]) || !isDeepStrictEqual(report.nativeAfter.reopened.object.value, target) || !isDeepStrictEqual(report.nativeAfter.resaved.object.value, target)) fail('native initial/reopen/resave values drifted')
if (report.web.before?.typeId !== 'App::PropertyBoolList' || report.web.before?.element !== 'BoolList' || report.web.before?.value !== '[false]' || report.web.after?.typeId !== 'App::PropertyBoolList' || report.web.after?.element !== 'BoolList' || report.web.after?.value !== '[true,false,true,false]' || 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 !== '[true,false,true,false]' || !isDeepStrictEqual(report.classification.reopenedValue, target) || !isDeepStrictEqual(report.classification.resavedValue, target) || report.classification.nativeOutputMatches !== true || report.classification.unknownSemanticDrift !== false || report.classification.zeroUnknownDrift !== true) fail('round-trip classification is not exact for PropertyBoolList')
for (const archive of Object.values(report.archives ?? {})) if (!archive?.path || !Number.isSafeInteger(archive.bytes) || archive.bytes <= 0 || !/^[0-9a-f]{64}$/.test(archive.sha256)) fail('archive evidence is incomplete')
console.log(JSON.stringify({ status: 'freecad-property-boollist-roundtrip-pass', values: { nativeInitial: report.nativeInitial.object.value, webEdited: JSON.parse(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))
