/**
 * Floating Response handlers
 * Controls the FloatingResponse component for agent UI responses
 */

import type { ToolConfig } from './index'

export interface ResponseControls {
  addMessage: (message: string, type?: 'info' | 'success' | 'warning' | 'error') => string
  removeMessage: (id: string) => void
  clearAll: () => void
  getMessages: () => Array<{ id: string; message: string; type: string; timestamp: number }>
}

// Global reference to response controls (set by App.vue)
let responseControls: ResponseControls | null = null

export function setResponseControls(controls: ResponseControls) {
  responseControls = controls
  ;(window as any).__responseControls = controls
}

export function getResponseControls(): ResponseControls | null {
  return responseControls
}

export function createResponseHandlers(): ToolConfig[] {
  return [
    {
      name: 'bubbleResponse',
      description: 'Responde al usuario mostrando un mensaje en la UI (terminal flotante) en lugar de en Claude Code',
      category: 'global',
      schema: {
        type: 'object',
        properties: {
          message: {
            type: 'string',
            description: 'El mensaje a mostrar al usuario en la UI'
          },
          type: {
            type: 'string',
            enum: ['info', 'success', 'warning', 'error'],
            description: 'Tipo de mensaje: info (default), success, warning, error'
          }
        },
        required: ['message']
      },
      handler: (args: { message: string; type?: 'info' | 'success' | 'warning' | 'error' }) => {
        if (!responseControls) return 'Error: Response controls not initialized'

        const type = args.type || 'info'
        const id = responseControls.addMessage(args.message, type)

        return `Mensaje mostrado en UI (id: ${id}, tipo: ${type})`
      }
    }
  ]
}
