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/chrome-cam-native-simulation-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || !/Chrome/i.test(report.browser?.product || '') || report.crossOriginIsolated !== true) throw new Error('Chrome native CAM simulation verification is not passing.')
if (report.capabilities?.openCamLib?.status !== 'available' || report.capabilities.openCamLib.backend !== 'upstream-opencamlib-wasm' || report.capabilities.camotics?.status !== 'native-host-available' || report.capabilities.camotics.backend !== 'camotics-native-host-qt5-tpl' || report.capabilities.camotics.workspaceNativeExecutable !== true || report.capabilities.camotics.nativeGuiPath !== 'CAMotics/camotics' || report.capabilities.camotics.nativeTplPath !== 'CAMotics/tplang' || report.capabilities.camotics.browserExecutable !== false || report.capabilities.camotics.browserWasmKernelExecutable !== true || report.capabilities.camotics.wasmKernel?.backend !== 'camotics-upstream-sweep-wasm' || report.capabilities.camotics.wasmKernel.fullCamoticsProgram !== false || report.capabilities.camotics.wasmKernel.gcodeParserIncluded !== false || report.capabilities.camotics.parserAuthority !== 'linuxcnc-wasm' || report.capabilities.camotics.camoticsParsesCanonicalGcode !== false) throw new Error('Native CAM capability evidence is incomplete.')
if (report.openCamLib?.backend !== 'upstream-opencamlib-wasm' || report.openCamLib.triangles !== 2 || report.openCamLib.points < 2 || Math.abs(report.openCamLib.minimumZ - 2) > 1e-9 || Math.abs(report.openCamLib.maximumZ - 2) > 1e-9 || report.openCamLib.deterministic !== true || !/^[0-9a-f]{64}$/.test(report.openCamLib.artifactSha256)) throw new Error('OpenCAMLib drop-cutter evidence is incomplete.')
if (report.camotics?.backend !== 'camotics-source-stage-contract' || report.camotics.nativeExecutable !== false || report.camotics.lineCount < 6 || report.camotics.gcodeBytes < 1 || !/^[0-9a-f]{64}$/.test(report.camotics.gcodeHash) || report.camotics.parserAuthority !== 'linuxcnc-wasm' || report.camotics.camoticsParsesCanonicalGcode !== false) throw new Error('CAMotics source-stage G-code evidence is incomplete.')
if (JSON.stringify(report.pipeline?.stages) !== JSON.stringify(['cad', 'opencamlib-wasm', 'camotics-source-stage', 'gcode', 'linuxcnc-wasm-parse-execute']) || report.pipeline.canonicalGcodeAuthority !== 'linuxcnc-wasm' || report.pipeline.camoticsSemanticParser !== false || report.pipeline.linuxcncWasmExecution !== 'handoff-required') throw new Error('CAMotics/LinuxCNC handoff order is invalid.')
if (report.solidRemoval?.backend !== 'bitbybit-occt-wasm' || report.solidRemoval.model !== 'sampled-flat-end-brep' || report.solidRemoval.sweepMode !== 'sampled-flat-end' || report.solidRemoval.nativeSolid !== true || report.solidRemoval.freeCadNativeEquivalent !== false || report.solidRemoval.sampleCount < 2 || report.solidRemoval.stockVolume <= report.solidRemoval.remainingVolume || report.solidRemoval.removedVolume <= 0 || Math.abs(report.solidRemoval.stockVolume - report.solidRemoval.removedVolume - report.solidRemoval.remainingVolume) > 1e-7 || report.solidRemoval.structuralValid !== true || report.solidRemoval.solids < 1 || report.solidRemoval.meshVertices < 1 || report.solidRemoval.meshTriangles < 1 || report.solidRemoval.brepBytes < 1 || !/^[0-9a-f]{64}$/.test(report.solidRemoval.brepHash)) throw new Error('OCCT native solid-removal evidence is incomplete.')
if (report.curvedSolidRemoval?.backend !== 'bitbybit-occt-wasm' || report.curvedSolidRemoval.model !== 'continuous-segment-sweep-brep' || report.curvedSolidRemoval.sweepMode !== 'continuous-segment' || report.curvedSolidRemoval.sampleCount <= 4 || report.curvedSolidRemoval.stockVolume <= report.curvedSolidRemoval.remainingVolume || report.curvedSolidRemoval.removedVolume <= 0 || report.curvedSolidRemoval.structuralValid !== true || report.curvedSolidRemoval.solids < 1 || report.curvedSolidRemoval.meshVertices < 1 || report.curvedSolidRemoval.meshTriangles < 1) throw new Error('OCCT arbitrary-curve sweep evidence is incomplete.')
if (report.afterRelease?.shapeCount !== 0 || report.afterRelease.kernelReferenceCount !== 0) throw new Error('OCCT native CAM ownership evidence is incomplete.')
console.log(JSON.stringify({ status: 'chrome-cam-native-simulation-pass', browser: report.browser.product, openCamLib: report.openCamLib, camotics: report.camotics, pipeline: report.pipeline, solidRemoval: report.solidRemoval, afterRelease: report.afterRelease }, null, 2))
