"use client" import { useEffect, useState } from "react" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { CalendarClock, CheckCircle2, Circle, Clock, Loader2, PlayCircle, RefreshCw, XCircle } from "lucide-react" import { useActiveUser } from "@/hooks/useActiveUser" export function StudyPlanCard() { const [tasks, setTasks] = useState([]) const [loading, setLoading] = useState(true) const { userId, loading: userLoading } = useActiveUser(); useEffect(() => { if (userLoading || !userId) return; async function fetchPlan() { try { const res = await fetch(`/api/ai-coach/study-plan/today?user_id=${userId}`) if (!res.ok) { // If no plan is generated yet, fallback to mock silently throw new Error("silent_fallback") } const data = await res.json() setTasks(data.tasks || []) } catch (err: any) { if (err.message !== "silent_fallback") { console.error("Study plan fetch error:", err) } // Mock fallback if celery hasn't seeded yet setTasks([ { type: "Revision", description: "Revise Thermodynamics", duration_minutes: 15, status: "Pending" }, { type: "Mistake_Review", description: "Daily Mistake Review Session", duration_minutes: 10, status: "Pending" }, { type: "DPS", description: "Attempt Thermodynamics Practice Sheet", duration_minutes: 20, status: "Pending" } ]) } finally { setLoading(false) } } fetchPlan() }, [userId, userLoading]) const toggleTask = (idx: number, newStatus: string) => { setTasks(prev => { const copy = [...prev] copy[idx] = { ...copy[idx], status: newStatus } return copy }) } return (
Today's Plan {tasks.reduce((acc, curr) => acc + curr.duration_minutes, 0)} min total
AI-generated curriculum targeting your weak spots.
{loading ? (
Loading schedule...
) : (
{tasks.map((task, idx) => { const isDone = task.status === "Done" const isSkipped = task.status === "Skipped" const isPending = task.status === "Pending" return (
{task.type} {task.duration_minutes} min

{task.description}

{/* Controls */}
{isPending && ( <> )} {(isDone || isSkipped) && ( )}
) })}
)}
) }