import { useState, useCallback } from 'react';
import { getApiUrl } from '@/config/api';

export function useAIAnalysis() {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);
  const [analysis, setAnalysis] = useState(null);

  const fetchAnalysis = useCallback(async (flowData) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await fetch(getApiUrl('/api/options/ai-analysis'), {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ flowData })
      });

      const result = await response.json();

      if (result.success) {
        setAnalysis(result);
      } else {
        throw new Error(result.error || 'Failed to get AI analysis');
      }
    } catch (err) {
      setError(err.message);
      setAnalysis(null);
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    loading,
    error,
    analysis,
    fetchAnalysis
  };
}

