import { spawnSync } from 'node:child_process'
import { createHash } from 'node:crypto'
import { existsSync } from 'node:fs'
import { readFile, stat, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { buildProductionDriftSnapshot, productionDriftDecisions, same, validateProductionDriftSnapshot } from './freecad-production-drift-classification.mjs'

const root = resolve(new URL('..', import.meta.url).pathname)
const executable = process.env.FREECAD_CMD || resolve(root, '.cache/freecad/install-desktop/bin/FreeCADCmd')
const sysroot = resolve(root, '.cache/freecad/sysroot')
const harnessPath = resolve(root, 'scripts/freecad-tsn-stage-correlation-oracle.py')
const oraclePath = resolve(root, 'config/freecad-tsn-stage-correlation-oracle.json')
const outputPath = resolve(root, 'config/freecad-production-drift-classification.json')
const fail = (message) => { throw new Error(`FreeCAD production drift classification: ${message}`) }
const collectDifferences = (reference, replay, path = '$', differences = []) => {
  if (same(reference, replay)) return differences
  if (Array.isArray(reference) && Array.isArray(replay)) {
    for (let index = 0; index < Math.max(reference.length, replay.length); index += 1) collectDifferences(reference[index], replay[index], `${path}[${index}]`, differences)
    return differences
  }
  if (reference && replay && typeof reference === 'object' && typeof replay === 'object') {
    for (const key of new Set([...Object.keys(reference), ...Object.keys(replay)])) collectDifferences(reference[key], replay[key], `${path}.${key}`, differences)
    return differences
  }
  differences.push({ path, reference, replay })
  return differences
}
if (!existsSync(executable)) fail(`native executable is missing: ${executable}`)

const [reference, harnessContent, harnessBytes] = await Promise.all([
  readFile(oraclePath, 'utf8').then(JSON.parse),
  readFile(harnessPath),
  stat(harnessPath).then(({ size }) => size),
])
const execution = spawnSync(executable, ['--python-path', resolve(sysroot, 'usr/lib/python3/dist-packages'), harnessPath], {
  cwd: root,
  encoding: 'utf8',
  timeout: 180_000,
  maxBuffer: 80 * 1024 * 1024,
  env: {
    ...process.env,
    PYTHONPATH: `${resolve(sysroot, 'usr/lib/python3/dist-packages')}${process.env.PYTHONPATH ? `:${process.env.PYTHONPATH}` : ''}`,
    LD_LIBRARY_PATH: `${resolve(sysroot, 'usr/lib/x86_64-linux-gnu')}${process.env.LD_LIBRARY_PATH ? `:${process.env.LD_LIBRARY_PATH}` : ''}`,
    MATPLOTLIBRC: resolve(sysroot, 'usr/share/matplotlib/mpl-data/matplotlibrc'),
    MPLBACKEND: 'Agg',
  },
})
const output = `${execution.stdout || ''}\n${execution.stderr || ''}`
const marker = 'FREECAD_TSN_STAGE_CORRELATION_RESULT='
const markerIndex = output.indexOf(marker)
const payload = markerIndex >= 0 ? output.slice(markerIndex + marker.length).match(/\{.*\}/s)?.[0] : undefined
if (execution.error || execution.status !== 0 || !payload) fail(`native replay exited with ${execution.status}: ${execution.error?.message || output.trim()}`)
const replay = JSON.parse(payload)
if (reference.freecadVersion !== '1.1.1' || reference.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || replay.freecadVersion !== reference.freecadVersion || replay.gitCommit !== reference.gitCommit || replay.status !== 'pass') fail('native baseline or replay status is invalid.')

const classifications = productionDriftDecisions.map((decision) => {
  const referenceSnapshot = buildProductionDriftSnapshot(reference, decision.operation)
  const replaySnapshot = buildProductionDriftSnapshot(replay, decision.operation)
  validateProductionDriftSnapshot(referenceSnapshot)
  validateProductionDriftSnapshot(replaySnapshot)
  if (!same(referenceSnapshot, replaySnapshot)) fail(`${decision.operation} fingerprints differ between the locked reference and independent native replay: ${JSON.stringify(collectDifferences(referenceSnapshot, replaySnapshot))}`)
  return {
    ...decision,
    reference: referenceSnapshot,
    replay: replaySnapshot,
    replayStable: true,
    implementationDefect: decision.classification === 'implementation_defect',
  }
})

const driftCases = reference.driftReplays.length
const countClassification = (classification) => classifications.filter((entry) => entry.classification === classification).length
const report = {
  schemaVersion: 1,
  baseline: {
    freecadVersion: reference.freecadVersion,
    commit: reference.gitCommit,
    oracleId: reference.baselineId,
  },
  generatedBy: './npmw run probe:freecad-production-drift-classification',
  checkedBy: './npmw run check:freecad-production-drift-classification',
  nativeProbe: {
    path: 'scripts/freecad-tsn-stage-correlation-oracle.py',
    bytes: harnessBytes,
    sha256: createHash('sha256').update(harnessContent).digest('hex'),
    independentRuns: 2,
  },
  classifications,
  summary: {
    driftCases,
    classifiedCases: classifications.length,
    classifiedStages: classifications.reduce((sum, entry) => sum + entry.reference.driftOrdinals.length, 0),
    stableSemantics: countClassification('stable_semantics'),
    allowedEvolution: countClassification('allowed_evolution'),
    implementationDefects: countClassification('implementation_defect'),
    unknown: driftCases - classifications.length,
  },
}
await writeFile(outputPath, `${JSON.stringify(report, null, 2)}\n`)
console.log(JSON.stringify({
  status: 'freecad-production-drift-classification-generated',
  cases: report.classifications.map(({ taskId, operation, classification, reference: snapshot }) => ({ taskId, operation, classification, driftOrdinals: snapshot.driftOrdinals })),
  independentRuns: report.nativeProbe.independentRuns,
  output: 'config/freecad-production-drift-classification.json',
}, null, 2))
