'use client';
import { useState, useEffect, useMemo } from 'react';
/**
 * Shared hook for fetching contextual suggestions
 * Used by both AgentChat and AgentPopupButton
 * 
 * Server-side caching with KV (1 week TTL) - no client-side cache needed
 */ export function useSuggestions({ currentEndpoint, endpointIndex = [], limit }) {
    const [suggestions, setSuggestions] = useState([]);
    const [isLoading, setIsLoading] = useState(false);
    // Stable key for request deduplication
    const requestKey = useMemo(()=>currentEndpoint?.id || `general:${endpointIndex.length}`, [
        currentEndpoint?.id,
        endpointIndex.length
    ]);
    // Fallback suggestions
    const fallbackSuggestions = useMemo(()=>{
        if (currentEndpoint) {
            return [
                {
                    title: 'What does this',
                    label: 'endpoint do?',
                    prompt: `What does ${currentEndpoint.name} do?`
                },
                {
                    title: 'What parameters',
                    label: 'are required?',
                    prompt: 'What parameters are required?'
                },
                {
                    title: 'Python example',
                    label: 'Show code',
                    prompt: 'Show me a Python example'
                }
            ];
        }
        return [
            {
                title: 'Find an endpoint',
                label: 'to create resources',
                prompt: 'Find endpoints for creating resources'
            },
            {
                title: 'How do I',
                label: 'authenticate?',
                prompt: 'How do I authenticate?'
            },
            {
                title: 'Overview',
                label: 'What can I do?',
                prompt: 'What can I do with this API?'
            }
        ];
    }, [
        currentEndpoint
    ]);
    // Fetch suggestions from API (server handles KV caching)
    useEffect(()=>{
        let cancelled = false;
        setIsLoading(true);
        fetch('/api/suggestions', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                endpointIndex,
                currentEndpointId: currentEndpoint?.id
            })
        }).then((res)=>res.json()).then((data)=>{
            if (cancelled) return;
            if (data.suggestions) {
                const allSuggestions = data.suggestions;
                setSuggestions(limit ? allSuggestions.slice(0, limit) : allSuggestions);
            }
        }).catch((err)=>{
            if (cancelled) return;
            console.warn('[useSuggestions] Failed to fetch suggestions:', err);
            setSuggestions(limit ? fallbackSuggestions.slice(0, limit) : fallbackSuggestions);
        }).finally(()=>{
            if (!cancelled) setIsLoading(false);
        });
        return ()=>{
            cancelled = true;
        };
    }, [
        requestKey,
        currentEndpoint,
        endpointIndex,
        limit,
        fallbackSuggestions
    ]);
    return {
        suggestions,
        isLoading
    };
}
