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

const root = resolve(new URL('..', import.meta.url).pathname)
const load = async (name) => JSON.parse(await readFile(resolve(root, `config/${name}`), 'utf8'))
const [techdraw, spreadsheet, draft, plot, production] = await Promise.all([
  load('chrome-techdraw-verification.json'),
  load('chrome-spreadsheet-verification.json'),
  load('chrome-draft-verification.json'),
  load('chrome-plot-verification.json'),
  load('chrome-production-document-verification.json'),
])

for (const [name, report] of Object.entries({ techdraw, spreadsheet, draft, plot, production })) {
  if (report.schemaVersion !== 1 || report.status !== 'pass' || report.browserId !== 'chrome' || report.crossOriginIsolated !== true || !/Chrome/i.test(report.browser?.product || '')) throw new Error(`P07 ${name} Chrome evidence is not passing.`)
  if (report.afterRelease?.shapeCount !== 0 || report.afterRelease?.kernelReferenceCount !== 0) throw new Error(`P07 ${name} leaked ShapeHandles.`)
}
if (techdraw.page?.views !== 2 || techdraw.page.sections !== 1 || techdraw.page.dimensions !== 1 || techdraw.page.tolerances !== 1 || techdraw.pdf?.validHeader !== true || techdraw.pdf.validXref !== true || techdraw.pdf.deterministic !== true) throw new Error('TD-ALL page, GD&T or deterministic PDF evidence is incomplete.')
if (spreadsheet.largeSheet?.cells !== 10000 || spreadsheet.largeSheet.evaluationErrors !== 0 || spreadsheet.largeSheet.durationMs > spreadsheet.largeSheet.budgetMs || spreadsheet.cycle?.detected !== true || spreadsheet.binding?.redoValue !== 16) throw new Error('SS-ALL formula, binding or large-sheet evidence is incomplete.')
if (draft.objects?.sourceDependencies !== 5 || draft.operations?.trimmedLength !== 4 || draft.operations.offsetLength !== 8 || draft.persistence?.roundTrip !== true || draft.persistence.released !== true) throw new Error('DRAFT-BASE/DRAFT-OPS parametric operation evidence is incomplete.')
if (plot.series?.updatedValue !== 16 || plot.png?.validSignature !== true || plot.png.deterministic !== true || plot.png.width !== 640 || plot.png.height !== 360 || !plot.resources?.some((resource) => resource.mediaType === 'image/png' && resource.roundTrip && resource.released)) throw new Error('PLOT-ALL binding or real PNG evidence is incomplete.')
if (production.closure?.complete !== true || production.closure.documentVersion !== 8 || production.closure.reopenedComplete !== true || production.workflow?.edited.techdrawDimension !== 5 || production.workflow.exports.techdrawPdfValid !== true || production.persistence?.roundTrip !== true || production.persistence.released !== true) throw new Error('DOC-CLOSURE real create/edit/save/load/export evidence is incomplete.')

console.log(JSON.stringify({ status: 'document-closure-pass', browser: production.browser.product, tasks: ['TD-ALL', 'SS-ALL', 'DRAFT-BASE', 'DRAFT-OPS', 'PLOT-ALL', 'DOC-CLOSURE'], techdraw: { views: techdraw.page.views, pdfBytes: techdraw.pdf.bytes, tolerances: techdraw.page.tolerances }, spreadsheet: spreadsheet.largeSheet, draft: draft.operations, plot: plot.png, production: { closure: production.closure, workflow: production.workflow } }, null, 2))
