/**
 * Helper for Prove-LicenseEnforcement.ps1 — expects SOVEREIGN_DB_CONNECTION
 * pointing at a throwaway DB already at instance limit.
 */
const path = require('path');
const repoRoot = path.join(__dirname, '..');
const apiRoot = path.join(repoRoot, 'src', 'api');
const presetConn = process.env.SOVEREIGN_DB_CONNECTION;
require(path.join(apiRoot, 'node_modules', 'dotenv')).config({ path: path.join(repoRoot, '.env') });
require(path.join(apiRoot, 'node_modules', 'dotenv')).config({ path: path.join(apiRoot, '.env') });
if (presetConn) process.env.SOVEREIGN_DB_CONNECTION = presetConn;

const { assertCanRegisterInstance } = require(path.join(apiRoot, 'lib', 'licenseEngine'));

(async () => {
  const result = await assertCanRegisterInstance();
  console.log(JSON.stringify({ ok: result.ok, code: result.code, message: result.message }));
  if (result.ok || result.code !== 'INSTANCE_LIMIT') {
    process.exit(1);
  }
  process.exit(0);
})().catch((e) => {
  console.error(e.message || e);
  process.exit(1);
});
