'use client'

import { createContext, useContext, useState, useCallback, type ReactNode } from 'react'
import type { PrefillData } from '@/lib/api-docs/agent/types'

/** Current request payload from the playground */
export interface CurrentRequestPayload {
  method: string
  endpoint: string
  params: Array<{ key: string; value: string; active: boolean }>
  headers: Array<{ key: string; value: string; active: boolean }>
  body: string | null
  bodyContentType: string | null
}

interface PlaygroundContextValue {
  /** Pending prefill data to be applied */
  pendingPrefill: PrefillData | null
  /** Set prefill data - will be consumed by playground */
  setPrefill: (data: PrefillData) => void
  /** Clear pending prefill after it's been applied */
  clearPrefill: () => void
  /** Current request payload from the playground */
  currentRequestPayload: CurrentRequestPayload | null
  /** Set current request payload - called by playground when values change */
  setCurrentRequestPayload: (payload: CurrentRequestPayload | null) => void
  /** Trigger to send request from agent */
  triggerSend: boolean
  /** Call to trigger send from agent */
  requestSend: () => void
  /** Clear send trigger after playground handles it */
  clearSendTrigger: () => void
}

const PlaygroundContext = createContext<PlaygroundContextValue | null>(null)

export function PlaygroundProvider({ children }: { children: ReactNode }) {
  const [pendingPrefill, setPendingPrefill] = useState<PrefillData | null>(null)
  const [currentRequestPayload, setCurrentRequestPayloadState] = useState<CurrentRequestPayload | null>(null)
  const [triggerSend, setTriggerSend] = useState(false)
  
  const setPrefill = useCallback((data: PrefillData) => {
    setPendingPrefill(data)
  }, [])
  
  const clearPrefill = useCallback(() => {
    setPendingPrefill(null)
  }, [])
  
  const setCurrentRequestPayload = useCallback((payload: CurrentRequestPayload | null) => {
    setCurrentRequestPayloadState(payload)
  }, [])
  
  const requestSend = useCallback(() => {
    setTriggerSend(true)
  }, [])
  
  const clearSendTrigger = useCallback(() => {
    setTriggerSend(false)
  }, [])
  
  return (
    <PlaygroundContext.Provider value={{ 
      pendingPrefill, 
      setPrefill, 
      clearPrefill,
      currentRequestPayload,
      setCurrentRequestPayload,
      triggerSend,
      requestSend,
      clearSendTrigger,
    }}>
      {children}
    </PlaygroundContext.Provider>
  )
}

export function usePlaygroundPrefill() {
  const context = useContext(PlaygroundContext)
  if (!context) {
    throw new Error('usePlaygroundPrefill must be used within a PlaygroundProvider')
  }
  return context
}

export function useCurrentRequestPayload() {
  const context = useContext(PlaygroundContext)
  if (!context) {
    throw new Error('useCurrentRequestPayload must be used within a PlaygroundProvider')
  }
  return {
    currentRequestPayload: context.currentRequestPayload,
    setCurrentRequestPayload: context.setCurrentRequestPayload,
  }
}

export function useSendTrigger() {
  const context = useContext(PlaygroundContext)
  if (!context) {
    throw new Error('useSendTrigger must be used within a PlaygroundProvider')
  }
  return {
    triggerSend: context.triggerSend,
    requestSend: context.requestSend,
    clearSendTrigger: context.clearSendTrigger,
  }
}
