import { isDextoAuthEnabled } from "@dexto/agent-management";
import {
  isCommandDefinitionSupported,
  isCommandSupported,
  supportsPrompts
} from "../agent-backend.js";
import { generalCommands, createHelpCommand } from "./general-commands.js";
import { searchCommand, resumeCommand, renameCommand, forkCommand } from "./session/index.js";
import { exportCommand } from "./export/index.js";
import { modelCommands } from "./model/index.js";
import { mcpCommands } from "./mcp/index.js";
import { pluginCommands } from "./plugin/index.js";
import { systemCommands } from "./system/index.js";
import { toolCommands } from "./tool-commands.js";
import { promptCommands } from "./prompt-commands.js";
import { skillCommands } from "./skill-commands.js";
import { documentationCommands } from "./documentation-commands.js";
import { connectCommand, loginCommand, logoutCommand } from "./auth/index.js";
const CLI_COMMANDS = [];
const baseCommands = [
  // General commands (without help)
  ...generalCommands,
  // Session management
  searchCommand,
  // /search - opens search overlay
  resumeCommand,
  // /resume - opens session selector overlay
  renameCommand,
  // /rename <title> - rename current session
  forkCommand,
  // /fork - creates a forked session from current session
  exportCommand,
  // /export - opens export wizard overlay
  // Model management
  modelCommands,
  // /model - opens model selector overlay
  // MCP server management
  mcpCommands,
  // /mcp - opens MCP server list overlay
  // Plugin management
  pluginCommands,
  // /plugin - manage Claude Code compatible plugins
  // Tool management commands
  ...toolCommands,
  // Prompt management commands
  ...promptCommands,
  // Skill management commands
  ...skillCommands,
  // System commands
  ...systemCommands,
  // Documentation commands
  ...documentationCommands,
  // Auth commands (feature-flagged)
  ...isDextoAuthEnabled() ? [loginCommand, logoutCommand] : [],
  // Model provider auth
  connectCommand
];
CLI_COMMANDS.push(createHelpCommand((agent) => getAvailableCommands(agent)));
CLI_COMMANDS.push(...baseCommands);
async function executeCommand(command, args, agent, sessionId, configFilePath) {
  const ctx = { sessionId: sessionId ?? null, configFilePath: configFilePath ?? null };
  const cmd = CLI_COMMANDS.find(
    (c) => c.name === command || c.aliases && c.aliases.includes(command)
  );
  if (cmd) {
    if (!isCommandSupported(agent, command, cmd)) {
      return `\u26A0\uFE0F  Command /${command} is not available for this chat target.`;
    }
    try {
      const result = await cmd.handler(args, agent, ctx);
      return result;
    } catch (error) {
      const errorMsg2 = `\u274C Error executing command /${command}:
${error instanceof Error ? error.message : String(error)}`;
      agent.logger.error(
        `Error executing command /${command}: ${error instanceof Error ? error.message : String(error)}`
      );
      return errorMsg2;
    }
  }
  try {
    if (supportsPrompts(agent)) {
      const { getDynamicPromptCommands } = await import("./prompt-commands.js");
      const dynamicCommands = await getDynamicPromptCommands(agent);
      const promptCmd = dynamicCommands.find((c) => c.name === command);
      if (promptCmd) {
        try {
          const result = await promptCmd.handler(args, agent, ctx);
          return result;
        } catch (error) {
          const errorMsg2 = `\u274C Error executing prompt /${command}:
${error instanceof Error ? error.message : String(error)}`;
          agent.logger.error(
            `Error executing prompt /${command}: ${error instanceof Error ? error.message : String(error)}`
          );
          return errorMsg2;
        }
      }
    }
  } catch (error) {
    agent.logger.debug(
      `Failed to check dynamic commands for ${command}: ${error instanceof Error ? error.message : String(error)}`
    );
  }
  const errorMsg = `\u274C Unknown command: /${command}
Type / to see available commands, /prompts to add new ones`;
  return errorMsg;
}
function getAllCommands() {
  return CLI_COMMANDS;
}
function getAvailableCommands(agent) {
  return CLI_COMMANDS.filter((command) => isCommandDefinitionSupported(agent, command));
}
export {
  CLI_COMMANDS,
  executeCommand,
  getAllCommands,
  getAvailableCommands
};
