'use client' import { useState, useRef, useEffect } from 'react' import { Send, Bot, User, Loader2, Sparkles } from 'lucide-react' const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000' interface Message { role: 'user' | 'assistant' content: string timestamp: Date } interface ChatBotProps { transactions: any[] metrics: any } const suggestedQuestions = [ "Why is my precision so low?", "How can I improve recall performance?", "Explain how the quantum ensemble works", "What's the optimal fraud threshold?", "Compare VQC, QAOA, and QNN performance" ] export default function ChatBot({ transactions, metrics }: ChatBotProps) { const [messages, setMessages] = useState([]) const [input, setInput] = useState('') const [isLoading, setIsLoading] = useState(false) const messagesEndRef = useRef(null) const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } useEffect(() => { scrollToBottom() }, [messages]) const sendMessage = async (messageText?: string) => { const text = messageText || input if (!text.trim() || isLoading) return const userMessage: Message = { role: 'user', content: text, timestamp: new Date() } setMessages(prev => [...prev, userMessage]) setInput('') setIsLoading(true) try { const response = await fetch(`${API_URL}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: text, history: transactions }) }) const data = await response.json() const assistantMessage: Message = { role: 'assistant', content: data.response || 'Sorry, I could not generate a response.', timestamp: new Date() } setMessages(prev => [...prev, assistantMessage]) } catch (error) { const errorMessage: Message = { role: 'assistant', content: '⚠️ Failed to connect to the AI service. Please check if the backend is running.', timestamp: new Date() } setMessages(prev => [...prev, errorMessage]) } finally { setIsLoading(false) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() sendMessage() } } return (
{/* Main chat area */}
{/* Header */}

AI Fraud Intelligence Assistant

Ask questions about fraud patterns, model optimization, or performance metrics

{/* Messages */}
{messages.length === 0 && (

Welcome to AI Assistant

I can help you understand fraud patterns, optimize your model, and explain the quantum-classical hybrid architecture.

{/* Suggested questions */}
{suggestedQuestions.slice(0, 3).map((q, i) => ( ))}
)} {messages.map((message, index) => (
{message.role === 'assistant' && (
)}

{message.content}

{message.timestamp.toLocaleTimeString()}

{message.role === 'user' && (
)}
))} {isLoading && (
Analyzing...
)}
{/* Input */}
setInput(e.target.value)} onKeyDown={handleKeyDown} placeholder="Ask about fraud patterns, model optimization..." className="flex-1 bg-dark-700 border border-dark-600 rounded-lg px-4 py-3 text-sm focus:border-primary-500 focus:outline-none" disabled={isLoading} />
{/* Sidebar */}
{/* Current metrics */}

📊 Current Metrics

{metrics ? (
Accuracy {(metrics.accuracy * 100).toFixed(1)}%
Precision {(metrics.precision * 100).toFixed(1)}%
Recall {(metrics.recall * 100).toFixed(1)}%
F1 Score {metrics.f1.toFixed(3)}
) : (

No data yet

)}
{/* Suggested questions */}

💡 Try asking

{suggestedQuestions.map((q, i) => ( ))}
{/* Model info */}

🧠 Model Architecture

Classical (80%)
VQC (8%)
QAOA (6%)
QNN (6%)
) }