// AI Concierge — recomendações personalizadas + agendamento via backend real // (POST /api/ai/chat). O catálogo, o system prompt e a execução das tools // (agendar, verificar_disponibilidade) agora vivem no servidor — batem direto // no Postgres em vez de localStorage. Ver backend/src/routes/ai.routes.ts. const { useState: aiUseState, useRef: aiUseRef, useEffect: aiUseEffect } = React; function AIConcierge({open, onClose, showToast}) { const [msgs, setMsgs] = aiUseState([ {role:'assistant', content:'Oi! Sou a **Bela**, concierge do Angella Barros Studio de Beleza. Me conta o que você procura — posso indicar o ritual ideal, sugerir produtos e já deixar seu horário reservado. ✧'} ]); const [input, setInput] = aiUseState(''); const [busy, setBusy] = aiUseState(false); const bodyRef = aiUseRef(null); aiUseEffect(() => { if (bodyRef.current) bodyRef.current.scrollTop = bodyRef.current.scrollHeight; }, [msgs, busy]); const send = async (text) => { const q = (text ?? input).trim(); if (!q || busy) return; const history = [...msgs, {role:'user', content:q}]; setMsgs(history); setInput(''); setBusy(true); try { const res = await fetch('/api/ai/chat', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ messages: history.map(m => ({role:m.role, content:m.content})) }), }); if (res.status === 501) { setMsgs(h => [...h, {role:'assistant', content:'Ainda estou sendo configurada por aqui — fale com o studio pelo WhatsApp enquanto isso. ✧'}]); return; } if (!res.ok) throw new Error('chat failed'); const data = await res.json(); setMsgs(h => [...h, {role:'assistant', content: data.reply}]); if (/reserva registrada/i.test(data.reply || '')) showToast && showToast('Reserva criada pela Bela'); } catch (e) { setMsgs(h => [...h, {role:'assistant', content:'Desculpe, tive um problema de conexão. Pode repetir? Se preferir, fale conosco pelo WhatsApp.'}]); } finally { setBusy(false); } }; const chips = ['Quero um ritual relaxante', 'Meu cabelo está ressecado', 'Tenho um evento no sábado', 'Indique um produto']; return ( <>