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-dressup-oracle.json')
if (oracle.schemaVersion !== 1 || oracle.baselineId !== 'freecad-1.1.1-partdesign-dressup-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 dress-up oracle baseline is invalid.')
const expectedTypes = new Map([
  ['fillet-selected-edge', { typeId: 'PartDesign::Fillet', volume: 997.8539816339743 }],
  ['chamfer-selected-edge', { typeId: 'PartDesign::Chamfer', volume: 995 }],
  ['draft-selected-face', { typeId: 'PartDesign::Draft', volume: 1500 }],
  ['thickness-selected-face', { typeId: 'PartDesign::Thickness', volume: 424 }],
])
for (const [id, expectation] of expectedTypes) {
  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 || fixture.selection?.length !== 1) throw new Error(`FreeCAD PartDesign dress-up fixture ${id} is invalid.`)
}
const thickness = oracle.cases.find((entry) => entry.id === 'thickness-selected-face')
if (thickness.faces !== 11 || thickness.elementMapSize !== 51 || thickness.reversed !== true) throw new Error('FreeCAD PartDesign Thickness topology evidence is invalid.')

const chrome = await load('config/chrome-partdesign-transform-verification.json')
const dressupOperations = chrome.operations?.slice(1, 5)
if (chrome.status !== 'pass' || JSON.stringify(dressupOperations?.map((entry) => entry.command)) !== JSON.stringify(['fillet', 'chamfer', 'draft', 'thickness']) || dressupOperations.some((entry) => entry.recompute !== 'completed' || !entry.shapeId || !(entry.volume > 0)) || chrome.topologyMigration?.wrongBindings !== 0 || chrome.topologyMigration?.stable < 1 || chrome.topologyMigration?.ambiguous < 1 || chrome.topologyMigration?.ambiguityDiagnostics !== chrome.topologyMigration?.ambiguous || chrome.ambiguityRecovery?.code !== 'DRESSUP_EDGE_REFERENCE_UNRESOLVED' || chrome.ambiguityRecovery?.retainedShapeId !== chrome.ambiguityRecovery?.previousShapeId || chrome.afterRelease?.shapeCount !== 0 || chrome.afterRelease?.kernelReferenceCount !== 0) throw new Error('Chrome PartDesign dress-up topology lifecycle evidence is invalid.')

const nativePaths = new Map([
  ['fillet', 'config/chrome-native-fillet-history-verification.json'],
  ['chamfer', 'config/chrome-native-chamfer-history-verification.json'],
  ['draft', 'config/chrome-native-draft-history-verification.json'],
  ['thickness', 'config/chrome-native-thickness-history-verification.json'],
])
const nativeHistory = {}
for (const [operation, path] of nativePaths) {
  const report = await load(path)
  if (report.status !== 'pass' || report.nativeCapabilities?.operations?.includes(operation) !== true || report.history?.recordCount < 1 || report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error(`Chrome native ${operation} history evidence is invalid.`)
  nativeHistory[operation] = report.history.recordCount
}
console.log(JSON.stringify({ status: 'freecad-partdesign-dressup-oracle-pass', baselineId: oracle.baselineId, summary: oracle.summary, topologyMigration: chrome.topologyMigration, nativeHistory }, null, 2))
