#!/usr/bin/env node
/**
 * Canonical reconciliation hash: IPSAS registry + journal matrix JSON (minor units / narratives unchanged).
 * Exit 0 always; stdout JSON with sha256 for audit trail anchoring.
 * Env: OMNL_IPSAS_GL_REGISTRY, OMNL_JOURNAL_MATRIX_PATH (optional overrides).
 */
import { readFileSync } from 'fs';
import { createHash } from 'crypto';
import { resolve } from 'path';
import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const regPath =
  process.env.OMNL_IPSAS_GL_REGISTRY ||
  resolve(__dirname, '../../../config/omnl-ipsas-gl-registry.json');
const matrixPath =
  process.env.OMNL_JOURNAL_MATRIX_PATH ||
  resolve(__dirname, '../../../config/omnl-journal-matrix.json');

const reg = readFileSync(regPath, 'utf8');
const matrix = readFileSync(matrixPath, 'utf8');
const canonical = JSON.stringify({
  registry: JSON.parse(reg),
  matrix: JSON.parse(matrix),
});
const sha256 = createHash('sha256').update(canonical).digest('hex');
const out = {
  ok: true,
  sha256,
  registryPath: regPath,
  matrixPath: matrixPath,
  generatedAt: new Date().toISOString(),
};
process.stdout.write(JSON.stringify(out, null, 2) + '\n');
