#!/usr/bin/env node
/**
 * Compiles policies.mjs into downstream artifacts.
 * Run: node scaffold/scripts/compile-policies.mjs
 *
 * Outputs:
 * 1. Validates policies are consistent
 * 2. Prints the generated FORBIDDEN table (for manual verification)
 * 3. Could be extended to auto-patch server-instructions/bodies
 */
import { generateForbiddenTable, generateScorerRules, POLICIES } from '../definitions/policies.mjs';

console.log('=== Policy Compilation ===');
console.log(`Total policies: ${POLICIES.length}`);
console.log(`Error-level: ${POLICIES.filter((policy) => policy.severity === 'error').length}`);
console.log(`Warning-level: ${POLICIES.filter((policy) => policy.severity === 'warning').length}`);
console.log('');
console.log('=== Generated FORBIDDEN Table ===');
console.log(generateForbiddenTable());
console.log('');
console.log('=== Generated Scorer Rules ===');
console.log(JSON.stringify(generateScorerRules(), null, 2));

const ids = POLICIES.map((policy) => policy.id);
const duplicateIds = ids.filter((id, index) => ids.indexOf(id) !== index);
if (duplicateIds.length > 0) {
  console.error(`\nERROR: Duplicate policy IDs: ${duplicateIds.join(', ')}`);
  process.exit(1);
}

console.log('\n✓ All policies valid');
