import { execFileSync } from 'node:child_process'
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const source = resolve(root, 'CAMotics')
if (!existsSync(source)) throw new Error('CAMotics source tree is required by the CAM pipeline.')
const revision = execFileSync('git', ['-C', source, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim()
if (revision !== 'e84665f2fa9d1151f03282ac7e01320bc65e015b') throw new Error(`CAMotics source revision drift: ${revision}`)
for (const file of ['src/gcodetool.cpp', 'src/camsim.cpp', 'src/gcode/parse/Parser.cpp']) {
  if (!existsSync(resolve(source, file))) throw new Error(`CAMotics source artifact missing: ${file}`)
}
for (const file of ['camsim', 'gcodetool', 'planner', 'build/camotics.so']) {
  if (!existsSync(resolve(source, file))) throw new Error(`CAMotics native artifact missing: ${file}`)
}
console.log(JSON.stringify({
  status: 'cam-pipeline-pass',
  order: ['CAD', 'OCL', 'CAMotics', 'GCODE', 'LinuxCNC WASM'],
  camotics: {
    sourcePath: 'CAMotics',
    sourceRevision: revision,
    generator: 'camotics-source-stage',
    simulator: 'camotics-camsim',
    workspaceNativeCli: 'verified-sidecar',
    browserNativeExecutable: false,
    parserAuthority: 'linuxcnc-wasm',
    camoticsParsesCanonicalGcode: false,
  },
  linuxcnc: { backend: 'linuxcnc-wasm', parserAuthority: 'linuxcnc-wasm' },
}, null, 2))
