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/chrome-profile-validation-verification.json'), 'utf8'))
if (report.schemaVersion !== 1 || report.browserId !== 'chrome' || report.status !== 'pass' || report.crossOriginIsolated !== true) throw new Error('Chrome profile validation verification is not passing.')
const fixtures = report.fixtures ?? {}
const is = (fixture, status, ringCount, intersections = 0) => fixture?.status === status && fixture.ringCount === ringCount && fixture.selfIntersections === intersections
if (!is(fixtures.closed, 'closed', 1) || !is(fixtures.open, 'open', 1) || !is(fixtures.multiRing, 'multi-ring', 2) || !is(fixtures.selfIntersecting, 'self-intersecting', 1, 1) || !is(fixtures.invalidNesting, 'invalid-nesting', 2) || !is(fixtures.nonPlanar, 'non-planar', 1) || !is(fixtures.degenerate, 'degenerate', 1)) throw new Error('Chrome profile classification evidence is incomplete.')
const errors = report.validationErrors ?? {}
if (errors.closed !== null || errors.multiRing !== null || !/open/.test(errors.open ?? '') || !/self-intersecting/.test(errors.selfIntersecting ?? '') || !/contained directly/.test(errors.invalidNesting ?? '') || !/coplanar/.test(errors.nonPlanar ?? '') || !/collinear/.test(errors.degenerate ?? '')) throw new Error('Chrome profile validation error evidence is incomplete.')
const partDesign = report.partDesignProfiles ?? {}
if (partDesign.closed?.code !== null || partDesign.closed.regionCount !== 1 || partDesign.multiRing?.code !== null || partDesign.multiRing.outerPoints !== 4 || partDesign.multiRing.holeCount !== 1 || partDesign.multiRing.regionCount !== 1 || partDesign.open?.code !== 'PROFILE_OPEN' || partDesign.selfIntersecting?.code !== null || partDesign.selfIntersecting.regionCount !== 2 || JSON.stringify(partDesign.selfIntersecting.regionPoints) !== '[3,3]' || partDesign.invalidNesting?.code !== null || partDesign.invalidNesting.regionCount !== 2 || JSON.stringify(partDesign.invalidNesting.regionPoints) !== '[4,4]') throw new Error('Chrome PartDesign profile consumption evidence is incomplete.')
const geometry = report.partDesignGeometry ?? {}
const geometryMatches = (name, volume, solids, shapeType) => Math.abs(geometry[name]?.volume - volume) <= 1e-7 && geometry[name]?.solids === solids && geometry[name]?.shapeType === shapeType
if (!geometryMatches('closed', 360, 1, 'solid') || !geometryMatches('multiRing', 320, 1, 'solid') || !geometryMatches('selfIntersecting', 80, 2, 'compound') || !geometryMatches('invalidNesting', 370, 2, 'compound')) throw new Error('Chrome PartDesign OCCT profile geometry does not match FreeCAD.')
if (report.opfs?.markerSuite !== 'SK-11' || report.opfs.markerCases !== 12 || report.opfs.markerRemoved !== true) throw new Error('Chrome profile validation OPFS marker was not verified and removed.')
console.log(JSON.stringify({ status: 'chrome-profile-validation-pass', browser: report.browserId, fixtures: report.fixtures, validationErrors: report.validationErrors, partDesignProfiles: report.partDesignProfiles, partDesignGeometry: report.partDesignGeometry, opfs: report.opfs }, null, 2))
