import { useState, useRef, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Bot, Send, Sparkles, RefreshCw, Mic, X, ChevronRight } from 'lucide-react'; import { AI_SUGGESTED_PROMPTS } from '@/constants'; import type { ChatMessage } from '@/types'; const INITIAL_MESSAGES: ChatMessage[] = [ { id: '1', role: 'assistant', content: "šŸ‘‹ Hi there! I'm your **RIT AI Assistant**, powered by Google Gemini. I have comprehensive knowledge about Rajalakshmi Institute of Technology — from admission procedures to campus facilities.\n\nHow can I help you today?", timestamp: new Date().toISOString(), }, ]; function formatContent(text: string) { return text .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/\n/g, '
'); } export default function AIAssistant() { const [messages, setMessages] = useState(INITIAL_MESSAGES); const [input, setInput] = useState(''); const [isTyping, setIsTyping] = useState(false); const messagesEndRef = useRef(null); const inputRef = useRef(null); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages, isTyping]); const sendMessage = async (text: string) => { if (!text.trim()) return; const userMsg: ChatMessage = { id: Date.now().toString(), role: 'user', content: text, timestamp: new Date().toISOString(), }; setMessages((prev) => [...prev, userMsg]); setInput(''); setIsTyping(true); // Simulate AI response await new Promise((r) => setTimeout(r, 1500)); setIsTyping(false); const responses: Record = { hostel: "**RIT Hostel Facilities:**\n\n• Separate hostels for boys and girls\n• Wi-Fi connectivity in all rooms\n• 24/7 security and CCTV\n• Hygienic canteen with vegetarian & non-vegetarian options\n• Common rooms with TV and recreation\n• In-house medical facility\n\nFor hostel admission, contact the hostel office with your Aadhaar card, medical certificate, and filled hostel application form.", bus: "**RIT Bus Routes:**\n\nRIT operates **15+ bus routes** covering major areas of Chennai:\n\n• Route 01: Chennai Central → Koyambedu → Porur → RIT (7:00 AM)\n• Route 02: Tambaram → Chrompet → Pallavaram → RIT (7:15 AM)\n• Route 03: Anna Nagar → Vadapalani → RIT (7:20 AM)\n\nAll buses depart from respective stops by 7:30 AM. Return buses leave RIT at 4:30 PM and 6:00 PM.", library: "**RIT Library Information:**\n\n• **Timings:** Mon-Sat 8:00 AM – 8:00 PM, Sunday 10:00 AM – 5:00 PM\n• **Collection:** Over 50,000 books, 200+ journals\n• **Digital Access:** IEEE Xplore, ACM Digital Library, Scopus\n• **Services:** Book borrowing (4 books, 14 days), Reference, Photocopying\n• **Wi-Fi:** Available throughout the library\n\nYou'll need your student ID card to access the library.", }; const lower = text.toLowerCase(); let responseText = ''; if (lower.includes('hostel')) responseText = responses.hostel; else if (lower.includes('bus') || lower.includes('route')) responseText = responses.bus; else if (lower.includes('library')) responseText = responses.library; else { responseText = `Thank you for your question about **"${text}"**!\n\nI'm connected to RIT's knowledge base and can help you with:\n\n• šŸ“š Academic information & syllabus\n• šŸ  Hostel & accommodation\n• 🚌 Bus routes & timings\n• šŸ“‹ Admission procedures\n• šŸ›ļø Campus facilities\n• šŸ‘©ā€šŸ« Faculty information\n• šŸŽ‰ Events & clubs\n\nCould you be more specific about what you'd like to know? I'll give you the most accurate information!`; } const aiMsg: ChatMessage = { id: (Date.now() + 1).toString(), role: 'assistant', content: responseText, timestamp: new Date().toISOString(), }; setMessages((prev) => [...prev, aiMsg]); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(input); } }; const clearChat = () => { setMessages(INITIAL_MESSAGES); }; return (
{/* Header */}

RIT AI Assistant

Powered by Gemini AI Ā· Campus Knowledge Enabled
New Chat
{/* Features strip */} {[ { icon: Sparkles, label: 'Gemini AI' }, { icon: Bot, label: 'RAG Enabled' }, { icon: ChevronRight, label: 'Campus Knowledge' }, ].map((item, i) => (
{item.label}
))}
{/* Messages */}
{messages.map((msg) => ( {msg.role === 'assistant' && (
)}
))} {/* Typing */} {isTyping && (
{[0, 1, 2].map((i) => ( ))}
)}
{/* Suggested Prompts */}
{AI_SUGGESTED_PROMPTS.slice(0, 4).map((prompt, i) => ( sendMessage(prompt)} className="px-3.5 py-2 rounded-xl border border-[#E5E7EB] text-xs text-[#475569] bg-white transition-all hover:border-[#F97316] hover:text-[#F97316]" style={{ fontFamily: 'Inter, sans-serif' }} > {prompt} ))}
{/* Input */}
setInput(e.target.value)} onKeyDown={handleKeyDown} placeholder="Ask anything about RIT..." className="flex-1 text-sm text-[#1E293B] placeholder-[#94A3B8] focus:outline-none bg-transparent" style={{ fontFamily: 'Inter, sans-serif' }} /> {input && ( setInput('')} className="w-8 h-8 rounded-xl flex items-center justify-center text-[#94A3B8] hover:bg-gray-100 transition-all" > )} sendMessage(input)} disabled={!input.trim()} className="w-10 h-10 rounded-xl flex items-center justify-center text-white transition-all disabled:opacity-50" style={{ background: 'linear-gradient(135deg, #F97316, #FB923C)' }} >
); }