import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { loadGoldenManifest, validateGoldenOperation } from './freecad-golden-contract.mjs'

const root = resolve(new URL('..', import.meta.url).pathname)
const successManifest = await loadGoldenManifest(resolve(root, 'fixtures/freecad-golden/manifest.json'))
if (successManifest.scenarios.length !== 100) throw new Error(`FreeCAD golden fixtures: expected 100 success fixtures, found ${successManifest.scenarios.length}.`)
const validateFailureManifest = async (manifestFile, expectedCount) => {
  const failureManifest = JSON.parse(await readFile(manifestFile, 'utf8'))
  if (failureManifest.schemaVersion !== 1 || failureManifest.baselineId !== 'freecad-1.1.1' || !Array.isArray(failureManifest.failures) || failureManifest.failures.length !== expectedCount) throw new Error(`FreeCAD golden fixtures: expected ${expectedCount} failure fixtures at the locked baseline.`)
  const failureIds = new Set()
  for (const entry of failureManifest.failures) {
    if (!entry.id || failureIds.has(entry.id) || typeof entry.file !== 'string') throw new Error(`FreeCAD golden fixtures: invalid failure manifest entry ${entry.id || '<unknown>'}.`)
    failureIds.add(entry.id)
    const fixture = JSON.parse(await readFile(resolve(manifestFile, '..', entry.file), 'utf8'))
    if (fixture.id !== entry.id || typeof fixture.expectedError !== 'string' || !fixture.expectedError) throw new Error(`FreeCAD golden fixtures: incomplete failure fixture ${entry.id}.`)
    let rejected = false
    try { validateGoldenOperation(fixture.operation, fixture.id) } catch { rejected = true }
    if (!rejected) throw new Error(`FreeCAD golden fixtures: ${entry.id} is not rejected by the operation contract.`)
  }
  return failureManifest
}
const failureManifest = await validateFailureManifest(resolve(root, 'fixtures/freecad-golden/failures/manifest.json'), 51)
const supplementalManifest = await loadGoldenManifest(resolve(root, 'fixtures/freecad-golden/feature-families/manifest.json'))
if (supplementalManifest.scenarios.length !== 5) throw new Error(`FreeCAD golden fixtures: expected 5 supplemental feature-family fixtures, found ${supplementalManifest.scenarios.length}.`)
const supplementalFailureManifest = await validateFailureManifest(resolve(root, 'fixtures/freecad-golden/feature-families/failures/manifest.json'), 5)
console.log(JSON.stringify({ status: 'freecad-golden-fixtures-pass', successCount: successManifest.scenarios.length, failureCount: failureManifest.failures.length, supplementalSuccessCount: supplementalManifest.scenarios.length, supplementalFailureCount: supplementalFailureManifest.failures.length }, null, 2))
