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-force-success.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyForce success check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-force-success' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d') fail('baseline is invalid')
if (report.object?.name !== 'ForceProbe' || report.object.typeId !== 'Fem::ConstraintForce' || report.property?.name !== 'Force' || report.property.typeId !== 'App::PropertyForce' || report.property.group !== '') fail('native object/property identity changed')
if (report.diagnostics?.exception !== null || report.diagnostics.documentObjectCount !== 2 || report.cases?.length !== 6 || report.outputProperties?.length !== 3) fail('native success cases are incomplete')
const expectedValues = new Map([
  ['default', 0],
  ['nominal-newton', 500000],
  ['zero-boundary', 0],
  ['negative-boundary', -5000],
  ['kilonewton', 2000000],
  ['restored', 0],
])
const byId = new Map(report.cases.map((entry) => [entry.id, entry]))
for (const [id, expected] of expectedValues) {
  const entry = byId.get(id)
  if (!entry || entry.objectTypeId !== 'Fem::ConstraintForce' || entry.propertyName !== 'Force' || entry.typeId !== 'App::PropertyForce' || entry.group !== '' || JSON.stringify(entry.status) !== '[]' || JSON.stringify(entry.editorMode) !== '[]') fail(`${id} native property metadata changed`)
  if (entry.value?.numeric !== expected || typeof entry.value.unit !== 'string' || !entry.value.unit.includes('mm*kg/s^2') || entry.shape?.applicable !== false || !Array.isArray(entry.objectState)) fail(`${id} force value or object state is invalid`)
}
const expectedOutputNames = ['ForceX', 'ForceY', 'ForceZ']
for (const [index, output] of report.outputProperties.entries()) {
  if (output.objectName !== 'RigidForceProbe' || output.objectTypeId !== 'Fem::ConstraintRigidBody' || output.propertyName !== expectedOutputNames[index] || output.typeId !== 'App::PropertyForce' || output.group !== 'ConstraintRigidBody' || JSON.stringify(output.status) !== '["27"]' || output.value?.numeric !== 0 || !String(output.value.unit).includes('mm*kg/s^2') || output.shape?.applicable !== false) fail(`${expectedOutputNames[index]} output boundary changed`)
}
console.log(JSON.stringify({ status: 'freecad-property-force-success-pass', cases: Object.fromEntries([...expectedValues].map(([id]) => [id, byId.get(id).value.numeric])), internalUnit: 'mm*kg/s^2', newtonScale: 1000, outputProperties: expectedOutputNames }, null, 2))
