import { createHash } from 'node:crypto'
import { readFile } 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-heatflux-inventory.json')
const fail = (message) => { throw new Error(`FreeCAD PropertyHeatFlux inventory check failed: ${message}`) }
const [sourceContent, reportContent] = await Promise.all([readFile(sourcePath), readFile(reportPath)])
const source = JSON.parse(sourceContent)
const report = JSON.parse(reportContent)

if (report.schemaVersion !== 1 || report.status !== 'pass' || report.propertyType !== 'App::PropertyHeatFlux' || 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 !== createHash('sha256').update(sourceContent).digest('hex')) fail('source provenance is stale')
if (report.recordCount !== 1 || report.writableRecordCount !== 1 || report.outputRecordCount !== 0 || report.records?.length !== 1) fail('record cardinality is invalid')
const record = report.records[0]
if (record.objectTypeId !== 'Fem::ConstraintHeatflux' || record.objectAvailable !== true || record.probeStatus !== 'available' || record.propertyName !== 'DFlux' || record.group !== 'ConstraintHeatflux' || JSON.stringify(record.status) !== '[]' || record.defaultRaw !== '0.0 kg/s^3') fail('Fem::ConstraintHeatflux.DFlux inventory changed')
const model = record.valueModel
if (model?.kind !== 'quantity' || model.dimension !== 'mass/time^3' || model.internalUnit !== 'kg/s^3' || model.displayUnit !== 'W/m^2' || model.wattsPerSquareMeterScale !== 1 || model.defaultValue !== 0 || model.writable !== true || model.derivedOutput !== false) fail('DFlux value model is invalid')
if (record.dependencies?.length !== 0 || record.applicability?.requiredObjectTypeId !== 'Fem::ConstraintHeatflux') fail('DFlux applicability is incomplete')
const nativeMatches = []
for (const objectType of source.runtimeObjects?.types ?? []) for (const property of objectType.properties ?? []) if (property.typeId === 'App::PropertyHeatFlux') nativeMatches.push({ objectTypeId: objectType.typeId, propertyName: property.name, group: property.group, status: property.status, defaultRaw: property.default })
const reportMatches = report.records.map(({ objectTypeId, propertyName, group, status, defaultRaw }) => ({ objectTypeId, propertyName, group, status, defaultRaw }))
if (JSON.stringify(nativeMatches) !== JSON.stringify(reportMatches)) fail('report does not match the locked runtime oracle')
console.log(JSON.stringify({ status: 'freecad-property-heatflux-inventory-pass', propertyType: report.propertyType, recordCount: report.recordCount, writableRecordCount: report.writableRecordCount, outputRecordCount: report.outputRecordCount, objectTypes: ['Fem::ConstraintHeatflux'], classification: report.classification }, null, 2))
