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

const root = resolve(new URL('..', import.meta.url).pathname)
const report = JSON.parse(await readFile(resolve(root, 'config/freecad-partdesign-profile-oracle.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.baselineId !== 'freecad-1.1.1-partdesign-profile-oracle' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.status !== 'pass' || report.length !== 10 || report.tolerance !== 1e-7) throw new Error('FreeCAD PartDesign profile oracle baseline is invalid.')
if (report.summary?.cases !== 5 || report.summary?.passed !== 5 || report.summary?.accepted !== 4 || report.summary?.rejected !== 1) throw new Error('FreeCAD PartDesign profile oracle summary is incomplete.')
const expected = new Map([
  ['closed', 'accepted'],
  ['multi-ring', 'accepted'],
  ['open', 'rejected'],
  ['self-intersecting', 'accepted'],
  ['invalid-nesting', 'accepted'],
])
for (const [id, observation] of expected) {
  const fixture = report.cases?.find((entry) => entry.id === id)
  if (!fixture || fixture.expected !== observation || fixture.observed !== observation || fixture.passed !== true) throw new Error(`FreeCAD PartDesign profile fixture ${id} is invalid.`)
  if (observation === 'accepted' && (fixture.shapeNull !== false || fixture.solidCount < 1 || !Number.isFinite(fixture.volume) || fixture.volume <= 0)) throw new Error(`FreeCAD PartDesign accepted profile ${id} has no valid solid.`)
}
const closed = report.cases.find((entry) => entry.id === 'closed')
const multiRing = report.cases.find((entry) => entry.id === 'multi-ring')
const selfIntersecting = report.cases.find((entry) => entry.id === 'self-intersecting')
const invalidNesting = report.cases.find((entry) => entry.id === 'invalid-nesting')
if (Math.abs(closed.volume - 360) > report.tolerance || Math.abs(multiRing.volume - 320) > report.tolerance || Math.abs(selfIntersecting.volume - 80) > report.tolerance || Math.abs(invalidNesting.volume - 370) > report.tolerance || selfIntersecting.solidCount !== 2 || invalidNesting.solidCount !== 2) throw new Error('FreeCAD PartDesign accepted profile geometry does not match the locked oracle.')

const chrome = JSON.parse(await readFile(resolve(root, 'config/chrome-profile-validation-verification.json'), 'utf8'))
const profiles = chrome.partDesignProfiles ?? {}
if (chrome.status !== 'pass' || profiles.closed?.code !== null || profiles.multiRing?.code !== null || profiles.open?.code !== 'PROFILE_OPEN' || profiles.selfIntersecting?.code !== null || profiles.invalidNesting?.code !== null) throw new Error('Chrome PartDesign profile results do not match the FreeCAD acceptance matrix.')
const geometry = chrome.partDesignGeometry ?? {}
if (Math.abs(geometry.closed?.volume - closed.volume) > report.tolerance || geometry.closed?.solids !== closed.solidCount || Math.abs(geometry.multiRing?.volume - multiRing.volume) > report.tolerance || geometry.multiRing?.solids !== multiRing.solidCount || Math.abs(geometry.selfIntersecting?.volume - selfIntersecting.volume) > report.tolerance || geometry.selfIntersecting?.solids !== selfIntersecting.solidCount || Math.abs(geometry.invalidNesting?.volume - invalidNesting.volume) > report.tolerance || geometry.invalidNesting?.solids !== invalidNesting.solidCount) throw new Error('Chrome PartDesign profile geometry does not match the FreeCAD oracle.')
console.log(JSON.stringify({ status: 'freecad-partdesign-profile-oracle-pass', baselineId: report.baselineId, freecadVersion: report.freecadVersion, summary: report.summary, chrome: { profiles, geometry } }, null, 2))
