#!/usr/bin/env node
/*
  Ensures docs/security/EVENTS_SURFACE_AUTOGEN.md is up-to-date.

  Fails if regeneration changes the file.
*/

const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');

const ROOT = process.cwd();
const OUT_PATH = path.join(ROOT, 'docs', 'security', 'EVENTS_SURFACE_AUTOGEN.md');

(function main() {
  const normalize = (s) => String(s || '').replace(/\r\n/g, '\n');
  const before = fs.existsSync(OUT_PATH) ? fs.readFileSync(OUT_PATH, 'utf8') : '';
  execFileSync('node', [path.join('tools', 'generate_events_surface.js')], { stdio: 'inherit' });
  const after = fs.existsSync(OUT_PATH) ? fs.readFileSync(OUT_PATH, 'utf8') : '';

  if (normalize(before) !== normalize(after)) {
    console.error('Events surface doc is out of date.');
    console.error('Run: npm run gen:events-surface');
    process.exitCode = 1;
  }
})();
