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-font-roundtrip.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyFont roundtrip check failed: ${message}`) }
const initial = 'osifont'
const target = 'Noto Sans CJK SC'
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-font-roundtrip' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyFont') fail('baseline is invalid')
const objectSet = [{ name: 'FontProbe', typeId: 'TechDraw::DrawViewAnnotation' }]
for (const [phase, snapshot] of [['nativeInitial', report.nativeInitial], ['nativeReopened', report.nativeAfter?.reopened], ['nativeResaved', report.nativeAfter?.resaved]]) {
  if (snapshot?.object?.name !== 'FontProbe' || snapshot.object.typeId !== 'TechDraw::DrawViewAnnotation' || snapshot.object.propertyTypeId !== 'App::PropertyFont' || !isDeepStrictEqual(snapshot.object.propertyStatus, []) || !isDeepStrictEqual(snapshot.object.editorMode, []) || !isDeepStrictEqual(snapshot.object.state, ['Up-to-date']) || snapshot.object.statusString !== 'Valid' || snapshot.object.hasShapeProperty !== false || !isDeepStrictEqual(snapshot.objectSet, objectSet)) fail(`${phase} native object semantics are incomplete`)
}
if (report.nativeInitial.object.value !== initial || report.nativeAfter.reopened.object.value !== target || report.nativeAfter.resaved.object.value !== target) fail('native PropertyFont values drifted')
if (report.web.before?.typeId !== 'App::PropertyFont' || report.web.before.element !== 'String' || report.web.before.value !== initial || report.web.after?.typeId !== 'App::PropertyFont' || report.web.after.element !== 'String' || report.web.after.value !== target || report.web.targetMarkedTouched !== true || report.web.webOutputMatches !== 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?.requestedValue !== target || report.classification.webValue !== target || report.classification.reopenedValue !== target || report.classification.resavedValue !== target || report.classification.nativeOutputMatches !== true || report.classification.objectSetPreserved !== true || report.classification.nativeStateStable !== true || report.classification.fontResourceBoundary !== 'font family name persists in FCStd; font files and availability remain presentation-only' || report.classification.unknownSemanticDrift !== false || report.classification.zeroUnknownDrift !== true) fail('round-trip classification is not exact for PropertyFont')
for (const artifact of Object.values(report.archives ?? {})) if (!artifact?.path || !Number.isSafeInteger(artifact.bytes) || artifact.bytes <= 0 || !/^[0-9a-f]{64}$/.test(artifact.sha256)) fail('archive evidence is incomplete')
console.log(JSON.stringify({ status: 'freecad-property-font-roundtrip-pass', values: { nativeInitial: report.nativeInitial.object.value, webEdited: report.classification.webValue, nativeReopened: report.classification.reopenedValue, nativeResaved: report.classification.resavedValue }, opaquePathsPreserved: report.web.opaquePathsPreserved, fontResourceBoundary: report.classification.fontResourceBoundary, zeroUnknownDrift: report.classification.zeroUnknownDrift }, null, 2))
