import a from './index5.mjs';
import { e as executionTracer } from '../shared/ccjk.DlTXS9rP.mjs';
import '../shared/ccjk.BAGoDD49.mjs';

async function traceCommand(options = {}) {
  if (options.list) {
    const traces = executionTracer.listTraces();
    if (traces.length === 0) {
      console.log(a.yellow("\n\u26A0\uFE0F  No execution traces found\n"));
      return;
    }
    console.log(a.cyan.bold("\n\u{1F4CA} Execution Traces\n"));
    for (const trace of traces.sort((a, b) => b.startTime - a.startTime)) {
      const duration = trace.endTime ? `${trace.endTime - trace.startTime}ms` : "running";
      console.log(a.white(`  ${trace.sessionId}`));
      console.log(a.gray(`    Started: ${new Date(trace.startTime).toLocaleString()}`));
      console.log(a.gray(`    Duration: ${duration}`));
      console.log(a.gray(`    Events: ${trace.metadata.totalEvents}`));
      console.log(a.gray(`    Agents: ${trace.metadata.agentCount}`));
      console.log(a.gray(`    Tool Calls: ${trace.metadata.toolCallCount}`));
      if (trace.metadata.errorCount > 0) {
        console.log(a.red(`    Errors: ${trace.metadata.errorCount}`));
      }
      console.log();
    }
    return;
  }
  if (options.cleanup) {
    executionTracer.cleanup(10);
    console.log(a.green("\n\u2705 Cleaned up old traces (kept last 10)\n"));
    return;
  }
  let sessionId = options.sessionId;
  if (!sessionId && options.last) {
    const traces = executionTracer.listTraces();
    if (traces.length === 0) {
      console.log(a.yellow("\n\u26A0\uFE0F  No execution traces found\n"));
      return;
    }
    const lastTrace = traces.sort((a, b) => b.startTime - a.startTime)[0];
    sessionId = lastTrace.sessionId;
  }
  if (!sessionId) {
    console.log(a.red("\n\u274C Please specify --session-id or use --last\n"));
    console.log(a.gray("Usage:"));
    console.log(a.gray("  ccjk trace --list              # List all traces"));
    console.log(a.gray("  ccjk trace --last              # Show last trace"));
    console.log(a.gray("  ccjk trace --session-id <id>   # Show specific trace"));
    console.log(a.gray("  ccjk trace --cleanup           # Clean up old traces"));
    console.log();
    return;
  }
  const visualization = executionTracer.visualizeTrace(sessionId);
  console.log(visualization);
}

export { traceCommand };
