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-color-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyColor roundtrip check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-color-roundtrip' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
const target = [0.2, 0.4, 0.6, 0.8]
const approximatelyTarget = (value) => Array.isArray(value) && value.length === target.length && value.every((channel, index) => typeof channel === 'number' && Math.abs(channel - target[index]) <= 1e-7)
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::PropertyColor' || !isDeepStrictEqual(snapshot.object.propertyStatus, []) || !isDeepStrictEqual(snapshot.object.editorMode, []) || snapshot.object.hasShapeProperty !== false || !isDeepStrictEqual(snapshot.objectSet, [{ name: 'ColorProbe', typeId: 'App::FeatureTest' }])) fail(`${phase} native object semantics are incomplete`)
}
if (!approximatelyTarget(report.nativeAfter.reopened.object.value) || !approximatelyTarget(report.nativeAfter.resaved.object.value) || !isDeepStrictEqual(report.nativeAfter.reopened.object.value, report.nativeAfter.resaved.object.value)) fail('native reopen/resave values drifted')
if (report.web.before?.typeId !== 'App::PropertyColor' || report.web.before?.element !== 'PropertyColor' || report.web.before?.value !== '287454020' || report.web.after?.typeId !== 'App::PropertyColor' || report.web.after?.element !== 'PropertyColor' || report.web.after?.value !== '862362060' || 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 (!isDeepStrictEqual(report.classification?.requestedValue, target) || report.classification.webValue !== '862362060' || !approximatelyTarget(report.classification.reopenedValue) || !approximatelyTarget(report.classification.resavedValue) || report.classification.nativeOutputMatches !== true || report.classification.unknownSemanticDrift !== false || report.classification.zeroUnknownDrift !== true) fail('round-trip classification is not exact for PropertyColor')
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-color-roundtrip-pass', values: { nativeInitial: report.nativeInitial.object.value, webEditedPacked: 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))
