import type { AgentPromptSurfaceKind } from "../plugins/types.js";
import { isAcpSessionKey, isSubagentSessionKey } from "../routing/session-key.js";

export type AgentPromptRenderContext = {
  surface: AgentPromptSurfaceKind;
  agentRuntimeId?: string;
  backendKind?: string;
  availableTools?: ReadonlySet<string>;
  sourceReplyDeliveryMode?: "automatic" | "message_tool_only";
  acpEnabled?: boolean;
  runtimeChannel?: string;
  runtimeCapabilities?: readonly string[];
};

export function buildOpenClawToolFallbackText(params: {
  surface: AgentPromptSurfaceKind;
  execToolName: string;
  processToolName: string;
}): string {
  if (params.surface === "pi_main") {
    return [
      "Pi lists the standard tools above. This runtime enables:",
      "- grep: search file contents for patterns",
      "- find: find files by glob pattern",
      "- ls: list directory contents",
      "- apply_patch: apply multi-file patches",
      `- ${params.execToolName}: run shell commands (supports background via yieldMs/background)`,
      `- ${params.processToolName}: manage background exec sessions`,
      "- browser: control OpenClaw's dedicated browser",
      "- canvas: present/eval/snapshot the Canvas",
      "- nodes: list/describe/notify/camera/screen on paired nodes",
      "- cron: manage cron jobs and wake events (use for reminders; when scheduling a reminder, write the systemEvent text as something that will read like a reminder when it fires, and mention that it is a reminder depending on the time gap between setting and firing; include recent context in reminder text if appropriate)",
      "- sessions_list: list sessions",
      "- sessions_history: fetch session history",
      "- sessions_send: send to another session",
      "- sessions_spawn: spawn an isolated sub-agent session",
      "- sessions_yield: end this turn and wait for sub-agent completion events",
      "- subagents: list active/recent sub-agent runs",
      '- session_status: show usage/time/model state and answer "what model are we using?"',
    ].join("\n");
  }

  return "No OpenClaw tool list is injected for this runtime prompt surface. Use only tools exposed directly by the active backend.";
}

export function shouldRenderOpenClawToolWorkflowHints(params: {
  surface: AgentPromptSurfaceKind;
  hasToolList: boolean;
}): boolean {
  return params.surface === "pi_main";
}

export function resolveAgentPromptSurfaceForSessionKey(
  sessionKey?: string,
): AgentPromptSurfaceKind {
  if (sessionKey && isAcpSessionKey(sessionKey)) {
    return "acp_backend";
  }
  return sessionKey && isSubagentSessionKey(sessionKey) ? "subagent" : "pi_main";
}
