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 sourcePath = resolve(root, '.cache/freecad/reference-desktop.json')
const reportPath = resolve(root, 'config/freecad-property-acceleration-inventory.json')
const fail = (message) => { throw new Error(`FreeCAD PropertyAcceleration inventory check failed: ${message}`) }
const [sourceContent, reportContent] = await Promise.all([readFile(sourcePath), readFile(reportPath)])
const source = JSON.parse(sourceContent)
const report = JSON.parse(reportContent)
const expectedSourceHash = createHash('sha256').update(sourceContent).digest('hex')
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.propertyType !== 'App::PropertyAcceleration' || report.classification !== 'opaque-fcstd-proxy') fail('report boundary is invalid')
if (report.baseline?.freecadVersion !== '1.1.1' || report.baseline?.commit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is not locked to FreeCAD 1.1.1')
if (report.source?.path !== '.cache/freecad/reference-desktop.json' || report.source.bytes !== sourceContent.length || report.source.sha256 !== expectedSourceHash) fail('source provenance is stale')
if (report.recordCount !== 1 || !Array.isArray(report.records) || report.records.length !== 1) fail('record count is not exactly one')
const [record] = report.records
if (record.objectTypeId !== 'Robot::TrajectoryDressUpObject' || record.objectAvailable !== true || record.probeStatus !== 'available' || record.propertyName !== 'Acceleration' || record.group !== 'TrajectoryDressUp' || JSON.stringify(record.status) !== '[]' || record.defaultRaw !== '1000.0 mm/s^2') fail('native property inventory changed')
if (record.valueModel?.kind !== 'quantity' || record.valueModel.dimension !== 'length/time^2' || record.valueModel.defaultValue !== 1000 || record.valueModel.defaultUnit !== 'mm/s^2') fail('quantity value model is invalid')
if (!Array.isArray(record.dependencies) || record.dependencies.length !== 0 || record.applicability?.requiredObjectTypeId !== record.objectTypeId) fail('dependencies or applicability are incomplete')
const nativeMatches = []
for (const objectType of source.runtimeObjects?.types ?? []) for (const property of objectType.properties ?? []) if (property.typeId === 'App::PropertyAcceleration') nativeMatches.push({ objectTypeId: objectType.typeId, propertyName: property.name, group: property.group, status: property.status, defaultRaw: property.default })
if (nativeMatches.length !== 1 || JSON.stringify(nativeMatches[0]) !== JSON.stringify({ objectTypeId: record.objectTypeId, propertyName: record.propertyName, group: record.group, status: record.status, defaultRaw: record.defaultRaw })) fail('report does not match the locked runtime oracle')
console.log(JSON.stringify({ status: 'freecad-property-acceleration-inventory-pass', propertyType: report.propertyType, recordCount: report.recordCount, objectTypeId: record.objectTypeId, defaultRaw: record.defaultRaw, classification: report.classification }, null, 2))
