/**
 * WebSocket event handlers for agent communication
 */

import type { AgentEvent } from "../agents/bandit-state"

export type TerminalLine = {
  type: "input" | "output" | "error" | "system"
  content: string
  timestamp: Date
  level?: number
  command?: string
}

export type ChatMessage = {
  type: "user" | "agent" | "typing" | "thinking" | "tool_call"
  content: string
  timestamp: Date
  level?: number
  metadata?: {
    modelName?: string
    tokenCount?: number
    executionTime?: number
  }
}

/**
 * Handle incoming agent events and update UI state
 */
export function handleAgentEvent(
  event: AgentEvent,
  updateTerminal: (updater: (prev: TerminalLine[]) => TerminalLine[]) => void,
  updateChat: (updater: (prev: ChatMessage[]) => ChatMessage[]) => void
) {
  console.log('🎯 handleAgentEvent called:', event.type, event.data)
  const timestamp = new Date(event.timestamp)

  switch (event.type) {
    case 'terminal_output':
      console.log('💻 Adding terminal line:', event.data.content)
      updateTerminal(prev => [
        ...prev,
        {
          type: event.data.command ? 'input' : 'output',
          content: event.data.content,
          timestamp,
          level: event.data.level,
          command: event.data.command,
        },
      ])
      break

    case 'agent_message':
      console.log('💬 Adding chat message:', event.data.content)
      updateChat(prev => [
        ...prev,
        {
          type: 'agent',
          content: event.data.content,
          timestamp,
          level: event.data.level,
          metadata: event.data.metadata,
        },
      ])
      break

    case 'thinking':
      console.log('🧠 Adding thinking message:', event.data.content)
      updateChat(prev => [
        ...prev,
        {
          type: 'thinking',
          content: event.data.content,
          timestamp,
          level: event.data.level,
        },
      ])
      break

    case 'tool_call':
      updateTerminal(prev => [
        ...prev,
        {
          type: 'system',
          content: `[TOOL] ${event.data.content}`,
          timestamp,
          level: event.data.level,
        },
      ])
      break

    case 'level_complete':
      updateTerminal(prev => [
        ...prev,
        {
          type: 'system',
          content: `✓ Level ${event.data.level} complete!`,
          timestamp,
          level: event.data.level,
        },
      ])
      updateChat(prev => [
        ...prev,
        {
          type: 'agent',
          content: `Level ${event.data.level} completed successfully. ${event.data.content}`,
          timestamp,
          level: event.data.level,
        },
      ])
      break

    case 'run_complete':
      updateTerminal(prev => [
        ...prev,
        {
          type: 'system',
          content: '✓ Run completed successfully!',
          timestamp,
        },
      ])
      updateChat(prev => [
        ...prev,
        {
          type: 'agent',
          content: event.data.content,
          timestamp,
        },
      ])
      break

    case 'error':
      updateTerminal(prev => [
        ...prev,
        {
          type: 'error',
          content: `ERROR: ${event.data.content}`,
          timestamp,
          level: event.data.level,
        },
      ])
      updateChat(prev => [
        ...prev,
        {
          type: 'agent',
          content: `Error: ${event.data.content}`,
          timestamp,
          level: event.data.level,
        },
      ])
      break
  }
}

/**
 * Create WebSocket message for sending to agent
 */
export function createAgentMessage(type: string, data: any): string {
  return JSON.stringify({
    type,
    data,
    timestamp: new Date().toISOString(),
  })
}

