/**
 * Tools Section
 * Formats available tools with input schemas and usage instructions
 */

import type { ToolDefinition } from '../../../tools/types'

/**
 * Builds the tools section of the system prompt
 * Lists available tools with descriptions, input schemas, and usage examples
 *
 * @param tools - Array of tool definitions with schemas
 * @returns Tools prompt section, or empty string if no tools
 */
export function buildToolsPrompt(tools: ToolDefinition[]): string {
  if (tools.length === 0) {
    return ''
  }

  let section = '## Available Tools\n\n'
  section +=
    'You have access to the following tools. To use a tool, include a tool-call action in your nextActions array:\n\n'

  // List each tool with description and schema
  tools.forEach((tool) => {
    section += `### ${tool.name}\n`
    section += `${tool.description}\n`
    section += `Input schema: ${JSON.stringify(tool.inputSchema, null, 2)}\n\n`
  })

  // Add usage instructions
  section += 'To call a tool, return a tool-call action:\n'
  section +=
    '{\n  "type": "tool-call",\n  "id": "unique-id",\n  "name": "tool-name",\n  "input": { /* tool input matching schema */ }\n}\n\n'

  section += '**IMPORTANT RULES:**\n'
  section += '1. "complete" CANNOT mix with navigate-knowledge actions in the same response\n'
  section += '2. "complete" CAN mix with message - always pair your final message with complete in the same iteration\n'
  section += '3. "complete" CAN mix with fire-and-forget tool-call actions when you do not need their results\n'
  section += '4. To use tools and inspect their results, return ONLY tool-call actions, then wait for results in the next iteration\n'
  section += '5. After receiving tool results, you can either call more tools OR complete with final answer\n'
  section += '6. navigate-knowledge actions load new capabilities - tools become available in the next iteration\n'

  return section + '\n'
}
