#!/usr/bin/env node
/**
 * Verify license + feature gate wiring is present (edition gating not cosmetic).
 */
'use strict';

const fs = require('fs');
const path = require('path');

const root = path.resolve(__dirname, '..');
const appSrc = fs.readFileSync(path.join(root, 'src/api/app.js'), 'utf8');
const licenseGate = fs.readFileSync(path.join(root, 'src/api/middleware/licenseGate.js'), 'utf8');
const featureGate = fs.readFileSync(path.join(root, 'src/api/middleware/featureGate.js'), 'utf8');
const licenseEngine = fs.readFileSync(path.join(root, 'src/api/lib/licenseEngine.js'), 'utf8');

let failed = false;

const checks = [
  [/app\.use\(licenseGate\)/.test(appSrc), 'licenseGate mounted'],
  [/featureGate\(/.test(appSrc), 'featureGate used on routes'],
  [/getLicenseSnapshot/.test(featureGate), 'featureGate reads license snapshot'],
  [/hasFeature/.test(featureGate), 'featureGate calls hasFeature'],
  [/enforceLicense/.test(licenseGate), 'licenseGate respects enforceLicense'],
  [/signatureValid|requireLicenseSignature/.test(licenseEngine), 'signed license support in engine'],
  [/readLicenseManifestFresh|signManifest/.test(fs.readFileSync(path.join(root, 'src/api/lib/licenseManifest.js'), 'utf8')), 'license manifest crypto'],
];

for (const [ok, label] of checks) {
  if (!ok) {
    console.error(`[verify:license-gating] FAIL — ${label}`);
    failed = true;
  } else {
    console.log(`[verify:license-gating] OK ${label}`);
  }
}

if (failed) process.exit(1);
console.log('[verify:license-gating] OK');
