import { CLAW_MODE_SYSTEM_PROMPT, type Message } from './ChatComponentTypes'
import { buildPromptWithAttachments, type PlaygroundAttachment } from './playgroundFileAttachments'

export interface OutboundChatMessage {
  role: string
  content: string | null
  tool_calls?: Array<{
    id: string
    type: 'function'
    function: {
      name: string
      arguments: string
    }
  }>
  tool_call_id?: string
}

const RESPONSE_HEADER_KEYS = [
  // v0.4 keystone headers (#2203)
  'x-vsr-schema-version',
  'x-vsr-response-path',
  'x-vsr-selected-model',
  'x-vsr-selected-decision',
  'x-vsr-selected-modality',
  'x-vsr-replay-id',
  'x-vsr-cache-hit',
  'x-vsr-selected-reasoning',
  'x-vsr-learning-methods',
  'x-vsr-learning-actions',
  'x-vsr-learning-scopes',
  'x-vsr-learning-reasons',
  'x-vsr-fast-response',
  'x-vsr-response-warnings',
  'x-vsr-matched-keywords',
  'x-vsr-matched-embeddings',
  'x-vsr-matched-domains',
  'x-vsr-matched-fact-check',
  'x-vsr-matched-user-feedback',
  'x-vsr-matched-reask',
  'x-vsr-matched-preference',
  'x-vsr-matched-language',
  'x-vsr-matched-context',
  'x-vsr-matched-structure',
  'x-vsr-context-token-count',
  'x-vsr-matched-complexity',
  'x-vsr-matched-modality',
  'x-vsr-matched-authz',
  'x-vsr-matched-jailbreak',
  'x-vsr-matched-pii',
  'x-vsr-matched-kb',
  'x-vsr-matched-conversation',
  'x-vsr-matched-event',
  'x-vsr-matched-projections',
  'x-vsr-looper-model',
  'x-vsr-looper-models-used',
  'x-vsr-looper-iterations',
  'x-vsr-looper-algorithm',
  'x-vsr-retention-drop',
  'x-vsr-retention-ttl-turns',
  'x-vsr-retention-keep-current-model',
  'x-vsr-retention-prefer-prefix',
] as const

export const buildChatMessages = (
  messages: Message[],
  nextUserMessage: string,
  enableClawMode: boolean,
  nextUserAttachments: PlaygroundAttachment[] = [],
): OutboundChatMessage[] => {
  const chatMessages: OutboundChatMessage[] = []

  for (const message of messages) {
    if (message.role === 'user') {
      chatMessages.push({
        role: 'user',
        content: buildPromptWithAttachments(message.content, message.playgroundAttachments ?? []),
      })
      continue
    }

    if (message.role === 'assistant' && message.content) {
      chatMessages.push({ role: 'assistant', content: message.content })
    }
  }

  if (enableClawMode) {
    chatMessages.unshift({ role: 'system', content: CLAW_MODE_SYSTEM_PROMPT })
  }

  chatMessages.push({
    role: 'user',
    content: buildPromptWithAttachments(nextUserMessage, nextUserAttachments),
  })
  return chatMessages
}

export const buildChatRequestBody = (
  model: string,
  messages: OutboundChatMessage[],
  activeTools: unknown[],
): Record<string, unknown> => {
  const requestBody: Record<string, unknown> = {
    model,
    messages,
    stream: true,
  }

  if (activeTools.length > 0) {
    requestBody.tools = activeTools
    requestBody.tool_choice = 'auto'
  }

  return requestBody
}

export const collectResponseHeaders = (response: Response): Record<string, string> => {
  const headers: Record<string, string> = {}

  RESPONSE_HEADER_KEYS.forEach((key) => {
    const value = response.headers.get(key)
    if (value) {
      headers[key] = value
    }
  })

  return headers
}
