import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const load = async (path) => JSON.parse(await readFile(resolve(root, path), 'utf8'))
const oracle = await load('config/freecad-partdesign-loft-oracle.json')
if (oracle.schemaVersion !== 1 || oracle.baselineId !== 'freecad-1.1.1-partdesign-loft-oracle' || oracle.freecadVersion !== '1.1.1' || oracle.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || oracle.status !== 'pass' || oracle.tolerance !== 1e-7 || oracle.summary?.cases !== 4 || oracle.summary?.passed !== 4) throw new Error('FreeCAD PartDesign loft oracle baseline is invalid.')
const expected = new Map([
  ['additive-loft', { typeId: 'PartDesign::AdditiveLoft', volume: 1 }],
  ['subtractive-loft', { typeId: 'PartDesign::SubtractiveLoft', volume: 1 }],
  ['additive-pipe', { typeId: 'PartDesign::AdditivePipe', volume: Math.PI }],
  ['subtractive-pipe', { typeId: 'PartDesign::SubtractivePipe', volume: 100 - Math.PI }],
])
for (const [id, expectation] of expected) {
  const fixture = oracle.cases?.find((entry) => entry.id === id)
  if (!fixture || fixture.typeId !== expectation.typeId || fixture.passed !== true || fixture.status !== 'Valid' || fixture.solids !== 1 || Math.abs(fixture.volume - expectation.volume) > oracle.tolerance) throw new Error(`FreeCAD PartDesign fixture ${id} is invalid.`)
  if (id.endsWith('-pipe') && fixture.transition !== 'Transformed') throw new Error(`FreeCAD PartDesign fixture ${id} did not use the locked Transformed transition.`)
}

const chrome = await load('config/chrome-partdesign-loft-verification.json')
const browserOrder = ['additive-loft', 'additive-pipe', 'subtractive-loft', 'subtractive-pipe']
if (chrome.status !== 'pass' || JSON.stringify(chrome.operations?.map((entry) => entry.command)) !== JSON.stringify(browserOrder) || chrome.operations.some((entry) => entry.recompute !== 'completed' || !entry.shapeId || !(entry.volume > 0)) || chrome.failureRecovery?.loft?.retainedShapeId !== chrome.failureRecovery?.loft?.previousShapeId || chrome.failureRecovery?.pipe?.retainedShapeId !== chrome.failureRecovery?.pipe?.previousShapeId || chrome.persistence?.reopenedTip !== chrome.operations[3].objectId || chrome.afterRelease?.shapeCount !== 0 || chrome.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome PartDesign loft/pipe lifecycle evidence is invalid.')

const nativeLoft = await load('config/chrome-native-loft-history-verification.json')
const nativePipe = await load('config/chrome-native-pipe-history-verification.json')
if (nativeLoft.status !== 'pass' || nativeLoft.nativeCapabilities?.operations?.includes('loft') !== true || nativeLoft.history?.recordCount < 1 || nativeLoft.afterRelease?.shapeCount !== 0 || nativeLoft.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome native Loft history evidence is invalid.')
if (nativePipe.status !== 'pass' || nativePipe.nativeCapabilities?.operations?.includes('pipe') !== true || nativePipe.history?.recordCount < 1 || nativePipe.afterRelease?.shapeCount !== 0 || nativePipe.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome native Pipe history evidence is invalid.')

console.log(JSON.stringify({ status: 'freecad-partdesign-loft-oracle-pass', baselineId: oracle.baselineId, summary: oracle.summary, chromeVolumes: Object.fromEntries(chrome.operations.map((entry) => [entry.command, entry.volume])), nativeHistory: { loft: nativeLoft.history.recordCount, pipe: nativePipe.history.recordCount } }, null, 2))
