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-color-success.json'), 'utf8'))
const fail = (message) => { throw new Error(`FreeCAD PropertyColor success check failed: ${message}`) }
if (report.schemaVersion !== 1 || report.status !== 'pass' || report.baselineId !== 'freecad-1.1.1-property-color-success' || report.freecadVersion !== '1.1.1' || report.gitCommit !== '0108fd4b4850cc46e625b60e53cea7a7bbe69f8d' || report.propertyType !== 'App::PropertyColor') fail('baseline is invalid')
if (report.caseCount !== 8 || !Array.isArray(report.cases) || report.cases.length !== 8 || new Set(report.cases.map(({ objectTypeId }) => objectTypeId)).size !== 8) fail('native success case inventory is incomplete')

const expectedHosts = [
  ['App::FeatureTest', 'Colour', [0, 0, 0, 1], 1],
  ['App::FeatureTestException', 'Colour', [0, 0, 0, 1], 1],
  ['App::Part', 'Color', [1, 1, 1, 0], 9],
  ['Assembly::AssemblyLink', 'Color', [1, 1, 1, 0], 9],
  ['Assembly::AssemblyObject', 'Color', [1, 1, 1, 0], 9],
  ['TechDraw::DrawViewAnnotation', 'TextColor', [0, 0, 0, 1], 1],
  ['TechDraw::DrawViewDraft', 'Color', [0, 0, 0, 1], 1],
  ['TechDraw::DrawViewSpreadsheet', 'TextColor', [0, 0, 0, 1], 2],
]
const normalized = {
  floatRgb: [0.25, 0.5, 0.75, 1],
  floatRgba: [0.100000001, 0.200000003, 0.300000012, 0.400000006],
  byteRgb: [0.003921569, 0.498039216, 1, 1],
  byteRgba: [1, 0.501960814, 0, 0.250980407],
  packedRgba: [0.06666667, 0.13333334, 0.200000003, 0.266666681],
  transparentBoundary: [0, 0, 0, 0],
}
const requested = {
  floatRgb: [0.25, 0.5, 0.75],
  floatRgba: [0.1, 0.2, 0.3, 0.4],
  byteRgb: [1, 127, 255],
  byteRgba: [255, 128, 0, 64],
  packedRgba: 0x11223344,
  transparentBoundary: [0, 0, 0, 0],
}
const byType = new Map(report.cases.map((entry) => [entry.objectTypeId, entry]))
for (const [typeId, propertyName, defaultValue, objectCount] of expectedHosts) {
  const entry = byType.get(typeId)
  if (entry?.propertyName !== propertyName || entry.mode !== 'direct-native-property-setter' || entry.propertyAssignmentsAccepted !== true || !isDeepStrictEqual(entry.defaultValue, defaultValue) || !isDeepStrictEqual(entry.requested, requested)) fail(`${typeId} setter contract is invalid`)
  if (typeId === 'TechDraw::DrawViewSpreadsheet') {
    if (entry.setup?.kind !== 'spreadsheet-source' || entry.setup.objectTypeId !== 'Spreadsheet::Sheet' || entry.setup.objectName !== 'ColorSource') fail('Spreadsheet host setup is incomplete')
  } else if (entry.setup?.kind !== 'none') fail(`${typeId} has unexpected host setup`)
  if (!isDeepStrictEqual(entry.phases?.default?.value, defaultValue) || !isDeepStrictEqual(entry.phases?.restored?.value, defaultValue)) fail(`${typeId} default or restored value changed`)
  for (const [phase, value] of Object.entries(normalized)) if (!isDeepStrictEqual(entry.phases?.[phase]?.value, value)) fail(`${typeId} ${phase} normalization changed`)
  const objectSet = entry.phases?.default?.objectSet
  if (!Array.isArray(objectSet) || objectSet.length !== objectCount || !objectSet.some(({ typeId: candidateType }) => candidateType === typeId)) fail(`${typeId} host object set is invalid`)
  for (const snapshot of Object.values(entry.phases ?? {})) {
    if (snapshot.propertyTypeId !== 'App::PropertyColor' || !isDeepStrictEqual(snapshot.propertyStatus, []) || !isDeepStrictEqual(snapshot.editorMode, []) || snapshot.shape?.applicable !== false || typeof snapshot.recomputeResult !== 'boolean' || !isDeepStrictEqual(snapshot.objectSet, objectSet)) fail(`${typeId} native metadata changed`)
  }
  if (typeId === 'App::FeatureTestException') {
    if (entry.hostExecution !== 'intrinsic-test-exception' || !isDeepStrictEqual(entry.phases.default.objectState, ['Up-to-date']) || entry.phases.default.statusString !== 'Valid') fail('FeatureTestException initial state changed')
    for (const [phase, snapshot] of Object.entries(entry.phases)) if (phase !== 'default' && (!isDeepStrictEqual(snapshot.objectState, ['Touched', 'Invalid']) || snapshot.statusString !== 'FeatureTestException::execute(): Testexception  ;-)')) fail(`FeatureTestException ${phase} intrinsic diagnostic changed`)
  } else {
    if (entry.hostExecution !== 'normal') fail(`${typeId} host execution classification changed`)
    for (const snapshot of Object.values(entry.phases)) if (!isDeepStrictEqual(snapshot.objectState, ['Up-to-date']) || snapshot.statusString !== 'Valid') fail(`${typeId} success state changed`)
  }
}
console.log(JSON.stringify({ status: 'freecad-property-color-success-pass', cases: report.caseCount, acceptedInputs: Object.keys(requested), normalized, normalHosts: 7, intrinsicHostDiagnostic: 'App::FeatureTestException' }, null, 2))
