"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Activity, Clock, PlayCircle, Search } from "lucide-react"; interface TestData { id: string; title: string; type: string; exam_pattern: string; total_time: number; difficulty: string; } export default function CBTPage() { const [exams, setExams] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(""); const [targetExam, setTargetExam] = useState(""); const [attempts, setAttempts] = useState([]); const userId = "b0e17378-864d-439e-8806-708dc48a891b"; useEffect(() => { async function loadCBTExams() { try { // 1. Fetch user target exam const userRes = await fetch(`http://localhost:8000/api/users/${userId}`); if (!userRes.ok) throw new Error("Failed to load user target exam"); const userData = await userRes.json(); const pattern = userData.target_exam; setTargetExam(pattern === "NEET" ? "NEET" : "JEE Main"); // 2. Fetch tests filtered by exam const testRes = await fetch(`http://localhost:8000/api/tests?exam_pattern=${pattern}`); if (!testRes.ok) throw new Error("Failed to fetch tests"); const testsData = await testRes.json(); setExams(testsData); // 3. Fetch user attempts const attemptsRes = await fetch(`http://localhost:8000/api/users/${userId}/attempts`); if (attemptsRes.ok) { const attemptsData = await attemptsRes.json(); setAttempts(attemptsData); } } catch (err) { console.error("Error loading exams:", err); } finally { setLoading(false); } } loadCBTExams(); }, []); const filteredExams = exams.filter(exam => exam.title.toLowerCase().includes(search.toLowerCase()) ); const formatDate = (dateStr: string) => { return new Date(dateStr).toLocaleDateString("en-US", { year: 'numeric', month: 'short', day: 'numeric' }); } // Get the highest score for each test if attempted multiple times const attemptsByTest = attempts.reduce((acc, curr) => { if (!acc[curr.source_id] || acc[curr.source_id].score < curr.score) { acc[curr.source_id] = curr; } return acc; }, {} as Record); return (

Computer Based Tests (CBT)

Real exam environment simulations to conquer {targetExam || "NEET/JEE"}.

setSearch(e.target.value)} className="w-full pl-10 pr-4 py-2 bg-background border border-border rounded-xl text-sm focus:ring-2 focus:ring-[#00F0FF] outline-none" />

Upcoming & Active Exams

{loading ? (
Loading test catalog...
) : filteredExams.length === 0 ? (
No active exams found for {targetExam || "your target exam"}. Try switching it in settings!
) : (
{filteredExams.map((exam) => { const attempt = attemptsByTest[exam.id]; const isAttempted = !!attempt; return (
{exam.type} Test {isAttempted && ( Attempted )}

{exam.title}

Available Now
{exam.total_time} Min • {exam.difficulty} Difficulty
)})}
)}

Past Attempts

{attempts.length === 0 ? (
You haven't attempted any CBT exams yet.
) : attempts.map((attempt, i) => { const exam = exams.find(e => e.id === attempt.source_id); const scoreCalc = (attempt.score * 4) - ((attempt.total_questions - attempt.score) * 1); // rough calc for display return (

{exam?.title || 'Unknown Test'}

{formatDate(attempt.submitted_at)}

Score
{scoreCalc} /{attempt.total_questions * 4}
Accuracy
{Math.round(attempt.accuracy)}%
)})}
); }