/**
 * Knowledge Map Section
 * Formats knowledge map nodes (loaded vs unloaded) with comprehensive navigation instructions
 */

import { KnowledgeMap } from '../../knowledge-map/types'

/**
 * Builds the knowledge map section of the system prompt
 * Includes conceptual overview, navigation instructions, workflow examples,
 * and separates loaded nodes (with full prompts) from unloaded nodes (available to load)
 *
 * @param knowledgeMap - Knowledge map instance with nodes
 * @returns Knowledge map prompt section, or empty string if no nodes
 */
export function buildKnowledgeMapPrompt(knowledgeMap?: KnowledgeMap): string {
  // No knowledge map or no nodes - skip section
  if (!knowledgeMap || Object.keys(knowledgeMap.nodes).length === 0) {
    return ''
  }

  let section = '## Knowledge Map\n\n'

  // Conceptual overview
  section += 'Knowledge maps provide on-demand access to specialized capabilities. '
  section += 'Each node contains domain-specific instructions and tools.\n\n'

  // Critical instruction - most common mistake
  section += '**CRITICAL**: After navigating to a node, tools become available in the **NEXT iteration**. '
  section += 'Do NOT attempt to use tools in the same iteration as navigation.\n\n'

  // Separate loaded vs unloaded nodes
  const loadedNodes: (typeof knowledgeMap.nodes)[string][] = []
  const unloadedNodes: (typeof knowledgeMap.nodes)[string][] = []

  Object.values(knowledgeMap.nodes).forEach((node) => {
    if (node.loaded && node.prompt) {
      loadedNodes.push(node)
    } else {
      unloadedNodes.push(node)
    }
  })

  // Show loaded nodes with full prompts
  if (loadedNodes.length > 0) {
    section += '### Loaded Knowledge\n\n'
    section += 'These nodes are active - their tools are available now:\n\n'
    loadedNodes.forEach((node) => {
      section += `**${node.id}**\n${node.prompt}\n\n`
    })
  }

  // Show unloaded nodes (available to load)
  if (unloadedNodes.length > 0) {
    section += '### Available to Load\n\n'
    unloadedNodes.forEach((node) => {
      section += `- **${node.id}**: ${node.description}\n`
    })

    // Navigation format
    section += '\n### How to Navigate\n\n'
    section += '```json\n'
    section += '{ "type": "navigate-knowledge", "id": "unique-id", "nodeId": "node-id" }\n'
    section += '```\n\n'

    // Workflow example - shows the 2-iteration pattern
    section += '### Typical Workflow\n\n'
    section += '**Iteration 1 - Navigate to load knowledge:**\n'
    section += '```json\n'
    section += '{\n'
    section += '  "reasoning": "I need [domain] capabilities to accomplish this task.",\n'
    section += '  "nextActions": [\n'
    section += '    { "type": "navigate-knowledge", "id": "nav-1", "nodeId": "[node-id]" }\n'
    section += '  ]\n'
    section += '}\n'
    section += '```\n\n'
    section += '**Iteration 2 - Use newly available tools:**\n'
    section += '```json\n'
    section += '{\n'
    section += '  "reasoning": "Now I have [domain] tools. Using [tool_name] to [action].",\n'
    section += '  "nextActions": [\n'
    section += '    { "type": "tool-call", "id": "t1", "name": "[tool_name]", "input": {...} }\n'
    section += '  ]\n'
    section += '}\n'
    section += '```\n\n'

    // Cross-turn persistence note
    section += '**Note:** Loaded knowledge persists across conversation turns. '
    section += 'Previously loaded nodes remain available without re-navigation.\n'
  }

  return section + '\n'
}
