import type { AgentRole } from "@paperclipai/shared";

export interface SlashCommand {
  command: string;
  description: string;
  routesTo: AgentRole | null;
  disabled?: boolean;
}

export const SLASH_COMMANDS: SlashCommand[] = [
  { command: "/brainstorm", description: "Route to Brainstormer", routesTo: "general" },
  { command: "/ask-pm", description: "Route to PM", routesTo: "pm" },
  { command: "/ask-engineer", description: "Route to Engineer", routesTo: "engineer" },
  { command: "/task", description: "Create a task", routesTo: "pm" },
  { command: "/search", description: "Search conversations", routesTo: null, disabled: true },
];

/**
 * Resolves which agent should receive a message based on slash command prefix or @mention.
 * Returns the agent ID to route to, or the active agent ID if no routing override found.
 */
export function resolveAgentFromContent(
  content: string,
  agents: Array<{ id: string; name: string; role: string }>,
  activeAgentId: string | null,
): string | null {
  // Slash command takes highest priority
  const slashMatch = content.match(/^(\/\S+)/);
  if (slashMatch) {
    const cmd = slashMatch[1];
    const slashCmd = SLASH_COMMANDS.find((c) => c.command === cmd);
    if (slashCmd?.routesTo) {
      const agent = agents.find((a) => a.role === slashCmd.routesTo);
      if (agent) return agent.id;
    }
  }
  // @mention takes second priority
  const mentionMatch = content.match(/@(\S+)/);
  if (mentionMatch) {
    const name = mentionMatch[1]!.toLowerCase();
    const agent = agents.find((a) => a.name.toLowerCase().startsWith(name));
    if (agent) return agent.id;
  }
  return activeAgentId;
}
