import { CAMOTICS_WASM_ARTIFACT_SHA256, CAMOTICS_WASM_SOURCE_REVISION, loadCamoticsSweepKernel } from './facade/camoticsWasm'

type CamoticsWasmReport = {
  schemaVersion: 1
  status: 'pass' | 'failed'
  browserId: 'chrome'
  crossOriginIsolated: boolean
  kernel?: { backend: string; sourceRevision: string; artifactSha256: string; abiVersion: number; imports: number; fullCamoticsProgram: boolean; gcodeParserIncluded: boolean }
  sweep?: { conicInsideDepth: number; conicOutsideDepth: number; spheroidInsideDepth: number; longMoveBoundingBoxes: number; deterministic: boolean }
  parserAuthority?: 'linuxcnc-wasm'
  error?: string
}

declare global { interface Window { __bitbybitCamoticsWasmReport?: CamoticsWasmReport } }

const run = async (): Promise<CamoticsWasmReport> => {
  const report: CamoticsWasmReport = { schemaVersion: 1, status: 'failed', browserId: 'chrome', crossOriginIsolated: globalThis.crossOriginIsolated === true }
  try {
    if (!report.crossOriginIsolated) throw new Error('CAMotics WASM evidence requires a cross-origin isolated browser runtime.')
    const kernel = await loadCamoticsSweepKernel()
    const evaluate = () => ({
      conicInsideDepth: kernel.conicDepth({ length: 5, topRadius: 2, start: [0, 0, 0], end: [10, 0, 0], point: [5, 0, 0] }),
      conicOutsideDepth: kernel.conicDepth({ length: 5, topRadius: 2, start: [0, 0, 0], end: [10, 0, 0], point: [5, 4, 0] }),
      spheroidInsideDepth: kernel.spheroidDepth({ radius: 2, length: 4, start: [0, 0, 0], end: [10, 0, 0], point: [5, 0, 0] }),
      longMoveBoundingBoxes: kernel.conicBoundingBoxCount({ length: 5, topRadius: 2, start: [0, 0, 0], end: [100, 0, 0], tolerance: 0.01 }),
    })
    const first = evaluate()
    const second = evaluate()
    report.kernel = { backend: kernel.backend, sourceRevision: kernel.sourceRevision, artifactSha256: kernel.artifactSha256, abiVersion: kernel.abiVersion, imports: kernel.imports.length, fullCamoticsProgram: kernel.fullCamoticsProgram, gcodeParserIncluded: kernel.gcodeParserIncluded }
    report.sweep = { ...first, deterministic: JSON.stringify(first) === JSON.stringify(second) }
    report.parserAuthority = 'linuxcnc-wasm'
    report.status = report.kernel.backend === 'camotics-upstream-sweep-wasm' && report.kernel.sourceRevision === CAMOTICS_WASM_SOURCE_REVISION && report.kernel.artifactSha256 === CAMOTICS_WASM_ARTIFACT_SHA256 && report.kernel.abiVersion === 1 && report.kernel.imports === 0 && !report.kernel.fullCamoticsProgram && !report.kernel.gcodeParserIncluded && report.sweep.conicInsideDepth === 1 && report.sweep.conicOutsideDepth === -1 && report.sweep.spheroidInsideDepth === 1 && report.sweep.longMoveBoundingBoxes === 3 && report.sweep.deterministic ? 'pass' : 'failed'
  } catch (error) {
    report.error = error instanceof Error ? error.stack || error.message : String(error)
  }
  return report
}

void run().then((report) => {
  window.__bitbybitCamoticsWasmReport = report
  const output = document.querySelector('#result')
  if (output) output.textContent = JSON.stringify(report, null, 2)
})
