import React, { useState, useEffect, useRef } from 'react'; import { X, Send, MessageCircle } from 'lucide-react'; const OnboardingChat = ({ authToken, onClose, userName }) => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const [progress, setProgress] = useState(0); const [complete, setComplete] = useState(false); const endRef = useRef(null); useEffect(() => { startOnboarding(); }, []); useEffect(() => { endRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const startOnboarding = async () => { try { const resp = await fetch(`${process.env.REACT_APP_API_URL}/api/onboarding/start`, { headers: { 'Authorization': `Bearer ${authToken}` }, }); if (resp.ok) { const data = await resp.json(); setMessages(data.messages || [{ role: 'agent', text: data.reply }]); setProgress(data.progress); setComplete(data.complete || false); } } catch (e) { setMessages([{ role: 'agent', text: "Hi! What is your security role and what are you trying to accomplish right now?" }]); } }; const sendMessage = async () => { if (!input.trim() || loading) return; const userText = input; setInput(''); setMessages(prev => [...prev, { role: 'user', text: userText }]); setLoading(true); try { const resp = await fetch(`${process.env.REACT_APP_API_URL}/api/onboarding/chat`, { method: 'POST', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ user_input: userText }), }); if (resp.ok) { const data = await resp.json(); setMessages(prev => [...prev, { role: 'agent', text: data.reply }]); setProgress(data.progress); setComplete(data.complete); } } catch (e) { setMessages(prev => [...prev, { role: 'agent', text: "Sorry, I had trouble processing that. Try again?" }]); } finally { setLoading(false); } }; return (