import { isDeepStrictEqual } from 'node:util'
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-property-colorlist-success.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyColorList success check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-colorlist-success' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyColorList' || report.caseCount !== 2 || report.cases?.length !== 2) fail('baseline is invalid')
const expected = {
  default: [[0, 0, 0, 1]],
  empty: [],
  singletonFloat: [[0.25, 0.5, 0.75, 1]],
  mixedElements: [[0.100000001, 0.200000003, 0.300000012, 0.400000006], [1, 0.501960814, 0, 0.250980407], [0.06666667, 0.13333334, 0.200000003, 0.266666681]],
  singlePacked: [[0.06666667, 0.13333334, 0.200000003, 0.266666681]],
  iterable: [[0, 0.25, 0.5, 0.75], [1, 0.75, 0.5, 0.25]],
  indexedDictionary: [[0, 0, 0, 1], [0.06666667, 0.13333334, 0.200000003, 0.266666681], [0, 1, 0, 0.501960814]],
  restored: [[0, 0, 0, 1]],
}
for (const typeId of ['App::FeatureTest', 'App::FeatureTestException']) {
  const entry = report.cases.find(({ objectTypeId }) => objectTypeId === typeId)
  if (entry?.mode !== 'native-property-list-setter' || entry.propertyName !== 'ColourList' || entry.propertyAssignmentsAccepted !== true || !isDeepStrictEqual(entry.defaultValue, expected.default)) fail(`${typeId} setter contract is invalid`)
  for (const [phase, value] of Object.entries(expected)) if (!isDeepStrictEqual(entry.phases?.[phase]?.value, value)) fail(`${typeId} ${phase} value changed`)
  const large = entry.phases?.largeList?.value
  const largeColor = [0.003921569, 0.007843138, 0.011764706, 0.015686275]
  if (!Array.isArray(large) || large.length !== 257 || !large.every((color) => isDeepStrictEqual(color, largeColor))) fail(`${typeId} large-list boundary changed`)
  for (const snapshot of Object.values(entry.phases)) if (snapshot.propertyTypeId !== 'App::PropertyColorList' || !isDeepStrictEqual(snapshot.propertyStatus, []) || !isDeepStrictEqual(snapshot.editorMode, []) || snapshot.shape?.applicable !== false || snapshot.objectSet?.length !== 1 || snapshot.objectSet[0].typeId !== typeId) fail(`${typeId} native metadata is invalid`)
  if (typeId === 'App::FeatureTest') {
    for (const snapshot of Object.values(entry.phases)) if (!isDeepStrictEqual(snapshot.objectState, ['Up-to-date']) || snapshot.statusString !== 'Valid') fail(`${typeId} normal execution state changed`)
  } else {
    if (!isDeepStrictEqual(entry.phases.default.objectState, ['Up-to-date']) || entry.phases.default.statusString !== 'Valid' || entry.phases.default.recomputeResult !== false) fail('FeatureTestException default state changed')
    for (const [phase, snapshot] of Object.entries(entry.phases)) if (phase !== 'default' && (!snapshot.objectState.includes('Invalid') || !snapshot.statusString.includes('Testexception'))) fail(`FeatureTestException ${phase} intrinsic diagnostic changed`)
  }
}
console.log(JSON.stringify({ status: 'freecad-property-colorlist-success-pass', cases: report.caseCount, phases: Object.keys(report.cases[0].phases), largeListLength: report.cases[0].phases.largeList.value.length, normalHost: 'App::FeatureTest', intrinsicHostDiagnostic: 'App::FeatureTestException' }, null, 2))
