// Copyright (c) 2026 defconxt. All rights reserved.
// Licensed under AGPL-3.0 — see LICENSE file for details.
// NERF is a trademark of defconxt.

/**
 * commands.js — Command routing table for the NERF CLI.
 *
 * Maps all 28 CLI commands to one of three dispatch modes:
 *   - native:       Dispatched directly through Node.js handler functions 
 *   - passthrough:  Legacy mode — no commands use this as of v2.0
 *                   (full terminal access for Rich panels, Textual TUIs, long-running services)
 *   - bridge:       (Legacy) Dispatched via JSON-RPC through python-bridge.js — no commands
 *                   use this mode since v2.0, retained for backward compatibility
 *
 * @module commands
 */

/**
 * Dispatch mode for each CLI command.
 *
 * @type {Record<string, { mode: 'native' | 'bridge' | 'passthrough', description: string }>}
 */
export const COMMAND_MODES = {
  // ── Native commands (Node.js handler dispatch,  ──────────────
  query:           { mode: 'native',      description: 'Run a security query (smart routing + streaming)' },
  ingest:          { mode: 'native',      description: 'Ingest security data' },
  status:          { mode: 'native',      description: 'Show system status' },
  doctor:          { mode: 'native',      description: 'Diagnose installation health' },
  version:         { mode: 'native',      description: 'Print version information' },
  plugin:          { mode: 'native',      description: 'Manage plugins' },
  search:          { mode: 'native',      description: 'Search security data' },
  store:           { mode: 'native',      description: 'Manage data stores' },
  diff:            { mode: 'native',      description: 'Compare security states' },
  workflow:        { mode: 'native',      description: 'Manage workflows' },
  stats:           { mode: 'native',      description: 'Show statistics' },
  domains:         { mode: 'native',      description: 'Manage domains' },
  skills:          { mode: 'native',      description: 'Manage skills' },
  score:           { mode: 'native',      description: 'Show security score' },
  marketplace:     { mode: 'native',      description: 'Browse marketplace' },
  compliance:      { mode: 'native',      description: 'Run compliance checks' },
  leaderboard:     { mode: 'native',      description: 'Show leaderboard' },
  'memory-export': { mode: 'native',      description: 'Export memory' },
  'memory-import': { mode: 'native',      description: 'Import memory' },
  sarif:           { mode: 'native',      description: 'SARIF report tools' },
  osint:           { mode: 'native',      description: 'OSINT intelligence tools' },
  update:          { mode: 'native',      description: 'Update NERF to latest version' },

  // ── Passthrough commands (direct Python spawn, full terminal) ──────────
  // These were Python-dependent — now ported to Node.js.
  scan:            { mode: 'native', description: 'Run a security scan' },
  review:          { mode: 'native', description: 'Multi-layer code review (3 parallel analyzers)' },
  panel:           { mode: 'native', description: 'Expert panel security assessment (3 personas)' },
  shard:           { mode: 'native', description: 'Split large docs into semantic chunks' },
  guardrail:       { mode: 'native', description: 'Test input/output guardrails' },
  analyze:         { mode: 'native', description: 'Cross-artifact consistency analysis' },
  bot:             { mode: 'native', description: 'Manage bot integrations (long-running service)' },
  mcp:             { mode: 'native', description: 'MCP server tools (long-running service)' },
  api:             { mode: 'native', description: 'API management (long-running server)' },
  'setup-signal':  { mode: 'native', description: 'Configure Signal integration' },
  chain:           { mode: 'native', description: 'Run multi-mode agent chain (e.g. red,purple,blue)' },
  council:         { mode: 'native', description: 'Multi-model consensus evaluation' },
  resume:          { mode: 'native', description: 'Resume interrupted execution sessions' },
  benchmark:       { mode: 'native', description: 'Run benchmark suites (NYU CTF, PicoCTF, OverTheWire)' },
};

/**
 * Set of command names dispatched via passthrough (direct Python spawn).
 * @type {Set<string>}
 */
export const PASSTHROUGH_COMMANDS = new Set(
  Object.entries(COMMAND_MODES)
    .filter(([, v]) => v.mode === 'passthrough')
    .map(([k]) => k)
);

/**
 * Set of command names dispatched via Node.js native handlers.
 * @type {Set<string>}
 */
export const NATIVE_COMMANDS = new Set(
  Object.entries(COMMAND_MODES)
    .filter(([, v]) => v.mode === 'native')
    .map(([k]) => k)
);

/**
 * Set of command names dispatched via the JSON-RPC bridge.
 * Since v2.0, no commands use bridge mode — retained for backward compatibility.
 * @type {Set<string>}
 */
export const BRIDGE_COMMANDS = new Set(
  Object.entries(COMMAND_MODES)
    .filter(([, v]) => v.mode === 'bridge')
    .map(([k]) => k)
);

/**
 * Look up the dispatch mode for a command.
 *
 * @param {string} name — command name (e.g., 'scan', 'status')
 * @returns {'native' | 'bridge' | 'passthrough' | null} — mode string, or null if unknown
 */
export function getCommandMode(name) {
  const entry = COMMAND_MODES[name];
  return entry ? entry.mode : null;
}
