/**
 * Core Four CLI command — Context/Model/Prompt/Tools dashboard
 *
 * Usage:
 *   morph-spec core-four <feature>
 *   morph-spec core-four <feature> --json
 */

import chalk from 'chalk';
import { getCoreFour, formatCoreFour, initCoreFour } from '../../lib/context/core-four-tracker.js';
import { loadState } from '../../core/state/state-manager.js';

export async function coreFourCommand(featureName, options) {
  try {
    // Initialize with defaults if not already set
    initCoreFour({
      model: 'claude-sonnet-4-6',
      contextWindow: 200000,
      feature: featureName
    });

    // Try to populate context breakdown from feature state
    try {
      const state = loadState(false);
      if (state?.features?.[featureName]) {
        const feature = state.features[featureName];
        // Attempt to estimate context breakdown from known files
        const breakdown = {
          'CLAUDE.md': 23000,
          standards: feature.activeAgents ? feature.activeAgents.length * 500 : 0,
          spec: feature.outputs?.includes('spec') ? 8000 : 0,
          conversation: 5000,
          other: 2000
        };
        const { updateContextBreakdown } = await import('../../lib/context/core-four-tracker.js');
        updateContextBreakdown(breakdown);
      }
    } catch {
      // Best-effort
    }

    if (options.json) {
      const data = getCoreFour();
      console.log(JSON.stringify(data, null, 2));
      return;
    }

    console.log('\n' + formatCoreFour());
    console.log('');
  } catch (err) {
    console.error(chalk.red(`Error: ${err.message}`));
    process.exit(1);
  }
}
