#!/usr/bin/env node

import path from 'node:path';
import { ConfigLoader } from '@zyphon/config-loader';

async function main() {
  const configDir = process.env.ZYPHON_CONFIG_DIR || './config';
  const stateDir = process.env.ZYPHON_STATE_DIR || './data/state';

  console.log(`[Config Validator] Inspecting manifests in: ${path.resolve(configDir)}`);

  const loader = new ConfigLoader(configDir, stateDir);

  try {
    const clusterConfig = loader.loadClusterConfig();
    console.log('✓ Successfully validated all manifests!');
    console.log(`  - Applications: ${clusterConfig.applications.length} configured`);
    console.log(`  - Providers:    ${clusterConfig.providers.length} registered`);
    console.log(`  - Default channels in routing: ${Object.keys(clusterConfig.routing.default).join(', ')}`);
    console.log(`  - Circuit breaker threshold: ${clusterConfig.routing.circuit_breaker?.failure_threshold ?? 3}`);
    console.log(`  - Global policies: ${clusterConfig.policies.global.idempotency_window_minutes}m idempotency, ${clusterConfig.policies.global.daily_retention_days}d retention`);
    process.exit(0);
  } catch (err: any) {
    console.error('✗ Configuration validation failed:');
    console.error(err.message);
    if (err.errors) {
      console.error(JSON.stringify(err.errors, null, 2));
    }
    process.exit(1);
  }
}

main().catch((err) => {
  console.error('Fatal error:', err);
  process.exit(1);
});

