#!/usr/bin/env node
/**
 * Verify license enforcement wiring includes instance limits and edition gates.
 */
'use strict';

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

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

const checks = [
  [/canRegisterInstance/.test(engine), 'canRegisterInstance gate'],
  [/LIMIT_REACHED/.test(engine), 'LIMIT_REACHED status'],
  [/instancesUsed/.test(engine) && /InstancesAllowed/.test(engine), 'instance cap math'],
  [/grace|expired/i.test(engine), 'expiry/grace handling'],
  [/hasFeature/.test(feature), 'edition feature map'],
  [/enforceLicense/.test(gate), 'license middleware enforcement'],
];

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

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