"use client" import { fetchWithAuth } from "@/lib/apiClient"; import { useEffect, useState } from "react" import { Card, CardContent } from "@/components/ui/card" import { TrendingUp, Flame, RefreshCw } from "lucide-react" import Link from "next/link" import { useActiveUser, isDemoUser } from "@/hooks/useActiveUser" export function ReadinessGauge() { const [data, setData] = useState(null) const [loading, setLoading] = useState(true) const { userId, loading: userLoading } = useActiveUser(); const isDemo = isDemoUser(userId); useEffect(() => { if (userLoading || !userId) return; async function fetchReadiness() { try { const res = await fetchWithAuth(`/api/ai-coach/readiness/${userId}`) if (!res.ok) throw new Error("Failed to fetch") const json = await res.json() setData(json) } catch (err) { if (isDemo) { setData({ score: 72, breakdown: { accuracy: 30, speed: 15, consistency: 8, recovery: 5, coverage: 14 } }) } else { setData({ score: 0, breakdown: { accuracy: 0, speed: 0, consistency: 0, recovery: 0, coverage: 0 } }) } } finally { setLoading(false) } } fetchReadiness() }, [userId, userLoading, isDemo]) if (loading) { return ( ) } const score = data?.score || 0 const isZero = score === 0 && !isDemo const circumference = 2 * Math.PI * 45 // r=45 const strokeDashoffset = circumference - (score / 100) * circumference return (
{/* Background glow decoration */}
{isZero ? "Algorithmic Baseline" : "Updated daily • Today"} {isZero ? "Day 1 Baseline" : "+5 this week"}
{/* Circular Score */}
{score} SCORE
{/* Quick metric grid beside ring */}
{isZero ? "0%" : "88%"} {!isZero && ↑}
Accuracy
{isZero ? "-- s" : "45s"} {!isZero && ↓}
Speed/Q
{isZero ? "0%" : "High"} {!isZero && ↑}
Consistency
{isZero ? "0%" : "64%"} {!isZero && ↑}
Syllabus
{/* Subject Bars */}
{[ { name: "Physics", val: isZero ? 0 : 68, color: "var(--cyan-ai)" }, { name: "Chemistry", val: isZero ? 0 : 74, color: "#F59E0B" }, { name: "Biology / Maths", val: isZero ? 0 : 82, color: "#10B981" }, ].map(sub => (
{sub.name}
{sub.val}%
))}
{/* National Avg */}
National Avg 58 / 100 {isZero ? "Pending test data" : `You: +${score - 58} above avg`}
{isZero ? "Complete your first diagnostic test to establish your personalized readiness score." : "Your speed has dropped slightly. Focus on time-bound practice today."}
) }