import a from './index5.mjs';
import { t as taskPersistence } from '../shared/ccjk.CNhnT6uQ.mjs';
import '../shared/ccjk.BAGoDD49.mjs';
import 'node:fs';
import 'better-sqlite3';
import '../shared/ccjk.bQ7Dh1g4.mjs';

async function sessionsCommand(options = {}) {
  if (options.list) {
    const sessions = taskPersistence.listSessions(20);
    if (sessions.length === 0) {
      console.log(a.yellow("\n\u26A0\uFE0F  No sessions found\n"));
      return;
    }
    console.log(a.cyan.bold("\n\u{1F4CB} Brain Sessions\n"));
    for (const session of sessions) {
      const date = new Date(session.createdAt).toLocaleString();
      console.log(a.white(`  ${session.id}`));
      console.log(a.gray(`    Created: ${date}`));
      if (session.metadata.rootTask) {
        console.log(a.gray(`    Task: ${session.metadata.rootTask}`));
      }
      console.log();
    }
    return;
  }
  if (options.show) {
    const context = taskPersistence.restoreContext(options.show);
    if (!context) {
      console.log(a.red(`
\u274C Session not found: ${options.show}
`));
      return;
    }
    console.log(a.cyan.bold(`
\u{1F4CA} Session: ${context.sessionId}
`));
    console.log(a.white(`Tasks: ${context.tasks.length}`));
    console.log();
    for (const task of context.tasks) {
      const statusIcon = {
        pending: "\u23F3",
        running: "\u{1F504}",
        completed: "\u2705",
        failed: "\u274C"
      }[task.status];
      console.log(`  ${statusIcon} ${task.name}`);
      console.log(a.gray(`     Status: ${task.status}`));
      console.log(a.gray(`     Priority: ${task.priority}`));
      if (task.dependencies.length > 0) {
        console.log(a.gray(`     Depends on: ${task.dependencies.join(", ")}`));
      }
      if (task.error) {
        const error = JSON.parse(task.error);
        console.log(a.red(`     Error: ${error.message}`));
      }
      console.log();
    }
    return;
  }
  if (options.restore) {
    const context = taskPersistence.restoreContext(options.restore);
    if (!context) {
      console.log(a.red(`
\u274C Session not found: ${options.restore}
`));
      return;
    }
    console.log(a.cyan.bold(`
\u{1F504} Restoring session: ${context.sessionId}
`));
    const incompleteTasks = context.tasks.filter(
      (t) => t.status === "pending" || t.status === "running"
    );
    if (incompleteTasks.length === 0) {
      console.log(a.green("\u2705 All tasks completed, nothing to restore\n"));
      return;
    }
    console.log(a.yellow(`Found ${incompleteTasks.length} incomplete tasks:
`));
    for (const task of incompleteTasks) {
      console.log(`  \u23F3 ${task.name}`);
    }
    console.log();
    console.log(a.gray("\u{1F4A1} To resume execution, use:"));
    console.log(a.cyan(`   ccjk brain execute --session ${context.sessionId}`));
    console.log();
    return;
  }
  if (options.cleanup) {
    const deleted = taskPersistence.cleanup(7);
    console.log(a.green(`
\u2705 Cleaned up ${deleted} old records (kept last 7 days)
`));
    return;
  }
  console.log(a.cyan.bold("\n\u{1F4CB} Brain Sessions Management\n"));
  console.log(a.white("Usage:"));
  console.log(a.gray("  ccjk sessions --list              # List all sessions"));
  console.log(a.gray("  ccjk sessions --show <id>         # Show session details"));
  console.log(a.gray("  ccjk sessions --restore <id>      # Restore incomplete session"));
  console.log(a.gray("  ccjk sessions --cleanup           # Clean up old sessions"));
  console.log();
}

export { sessionsCommand };
