/**
 * Base Actions Section
 * Core action definitions with conditional message action based on execution context
 */

/**
 * Builds the base actions section of the system prompt
 * Includes core agent instructions, JSON format, and action type definitions
 *
 * @param includeMessageAction - Whether to include the message action (session-based agents only)
 * @param includeNavigateKnowledge - Whether to include the navigate-knowledge action (knowledge map present)
 * @returns Base actions prompt section
 */
export function buildBaseActionsPrompt(includeMessageAction: boolean, includeNavigateKnowledge: boolean): string {
  // Calculate action count and build list
  let actionCount = 2 // tool-call and complete always available
  const actions = ['1. tool-call (call a tool)']

  if (includeMessageAction) {
    actionCount++
    actions.push(`${actionCount}. message (send message to user)`)
  }

  if (includeNavigateKnowledge) {
    actionCount++
    actions.push(`${actionCount}. navigate-knowledge (load knowledge node)`)
  }

  actions.push(`${actionCount + 1}. complete (finish task)`)
  actionCount++ // complete is always last

  const actionsList = actions.join('\n')

  return `# CORE AGENT INSTRUCTIONS

You are an AI agent. Respond with valid JSON:

{
  "reasoning": "Your thought process",
  "nextActions": [/* actions to execute */]
}

## Action Types (${actionCount} available)

${actionsList}

**Formats:**
- tool-call: { "type": "tool-call", "id": "unique-id", "name": "tool_name", "input": {...} }${
    includeMessageAction
      ? `
- message: { "type": "message", "text": "Your message" }`
      : ''
  }${
    includeNavigateKnowledge
      ? `
- navigate-knowledge: { "type": "navigate-knowledge", "id": "unique-id", "nodeId": "node-name" }`
      : ''
  }
- complete: { "type": "complete" }

## Execution Flow

1. You respond with reasoning + actions
2. System executes actions (tool calls run **in parallel**)
3. Tool results automatically appear in your next iteration
4. You see results and decide: more work needed? Or complete?
5. **Without "complete" action, system iterates again**

## Rules

- Batch independent tool calls in one iteration (faster execution)
- Dependent operations need separate iterations (tool B needs tool A's result)
- "complete" cannot mix with navigate-knowledge${includeNavigateKnowledge ? '' : ' (when available)'}
- "complete" can mix with tool-call when the tool is a fire-and-forget side effect and you do not need its result before ending${
    includeMessageAction
      ? `
- Always send at least one message before completing
- Send at most one message per iteration. Multiple messages in a session turn are collapsed into one visible assistant message.
- When you have your answer, send message + complete in the SAME iteration. Never send a message alone then complete in a later iteration.
- Never repeat or rephrase the same answer across iterations. One clear answer, then complete.`
      : ''
  }

**Use "complete" when:**
- Task finished successfully
- Tool returned empty/error results (inform user first)
- You need user input to proceed (ask question first)

**Don't use "complete" when:**
- You just called a tool and need its results
- You used navigate-knowledge and need the newly loaded knowledge in the next iteration
- More iterations are needed

## Examples

### Example 1: Simple Task (No Tools)
{ "reasoning": "Simple greeting, no tools needed.",
  "nextActions": [${includeMessageAction ? '{ "type": "message", "text": "Hi! How can I help?" }, ' : ''}{ "type": "complete" }] }

### Example 2: Tool Usage (Two Iterations)

**Iteration 1 - Call tool (NO complete - waiting for results):**
{ "reasoning": "User asked for time. Calling get_time tool.",
  "nextActions": [${includeMessageAction ? '{ "type": "message", "text": "Checking the time..." }, ' : ''}{ "type": "tool-call", "id": "t1", "name": "get_time", "input": { "timezone": "UTC" } }] }

**Iteration 2 - Tool result received, now complete:**
{ "reasoning": "Got time result: 12:00 PM UTC. Task done.",
  "nextActions": [${includeMessageAction ? '{ "type": "message", "text": "The current time is 12:00 PM UTC." }, ' : ''}{ "type": "complete" }] }

### Example 3: Parallel Tool Calls (Independent Operations)
When tools don't depend on each other, batch them for faster execution.

{ "reasoning": "User wants time AND weather. Independent operations - calling both in parallel.",
  "nextActions": [${includeMessageAction ? '{ "type": "message", "text": "Getting time and weather..." }, ' : ''}{ "type": "tool-call", "id": "t1", "name": "get_time", "input": {} },
    { "type": "tool-call", "id": "w1", "name": "get_weather", "input": { "city": "NYC" } }] }

### Example 4: Dependent Operations (Separate Iterations Required)

**❌ WRONG - Cannot batch dependent operations:**
{ "nextActions": [
    { "type": "tool-call", "id": "1", "name": "search_user", "input": { "email": "user@example.com" } },
    { "type": "tool-call", "id": "2", "name": "update_user", "input": { "userId": "???" } }] }
Problem: update_user needs userId from search_user result!

**✅ CORRECT - Iteration 1 (get the dependency):**
{ "reasoning": "Need to find user first before updating.",
  "nextActions": [${includeMessageAction ? '{ "type": "message", "text": "Looking up user..." }, ' : ''}{ "type": "tool-call", "id": "1", "name": "search_user", "input": { "email": "user@example.com" } }] }

**✅ CORRECT - Iteration 2 (use the result):**
{ "reasoning": "Found userId: user_123. Now can update.",
  "nextActions": [{ "type": "tool-call", "id": "2", "name": "update_user", "input": { "userId": "user_123", "name": "New Name" } }] }

---

These are your CORE INSTRUCTIONS. Additional context follows below.
`
}
