"use client" import { useEffect, useState } from "react" import { useRouter, useParams } from "next/navigation" import { Loader2 } from "lucide-react" import { toast } from "react-hot-toast" import api from "@/lib/api" import { useAuth } from "@/context/auth-context" type Lesson = { id: string title: string description?: string video_url?: string } type Module = { id: string title: string lessons: Lesson[] } type Course = { id: string title: string description: string modules: Module[] embed_url?: string video_url?: string } export default function CoursePage() { const { user, firebaseUser, isLoading: authLoading } = useAuth() const params = useParams() const router = useRouter() const courseId = params?.courseId as string const [course, setCourse] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) // Sidebar state: current const [selectedModuleIdx, setSelectedModuleIdx] = useState(0) const [selectedLessonIdx, setSelectedLessonIdx] = useState(0) const [completed, setCompleted] = useState(false) useEffect(() => { if (!authLoading && !user && !firebaseUser) { toast.error("Please login to view courses.") router.replace("/") return } if ((user || firebaseUser) && courseId) { ;(async () => { setLoading(true) setError(null) try { const resp = await api.get(`/api/courses/${courseId}?t=${Date.now()}`) setCourse(resp.data) setSelectedModuleIdx(0) setSelectedLessonIdx(0) setCompleted(false) } catch { setError("Failed to load course data.") } finally { setLoading(false) } })() } }, [authLoading, user, firebaseUser, courseId, router]) // Helper: embed URL function getEmbedUrl(url?: string): string | undefined { if (!url) return undefined const regExp = /(?:youtu\.be\/|youtube\.com\/(?:watch\?v=|embed\/))([^#&?]{11})/ const match = url.match(regExp) if (match && match[1]) { return `https://www.youtube.com/embed/${match[1]}?rel=0&modestbranding=1` } // fallback (could already be an embed url or another provider) return url } const modules = course?.modules || [] // Pick first non-empty for fallback if nothing selected const selModIdx = modules.length > 0 ? selectedModuleIdx : 0 const lessons = modules.length > 0 ? modules[selModIdx]?.lessons : [] const selLesIdx = lessons.length > 0 ? selectedLessonIdx : 0 const currentLesson = lessons.length > 0 ? lessons[selLesIdx] : undefined // for navigation const isEnd = modules.length > 0 && selModIdx === modules.length - 1 && lessons.length > 0 && selLesIdx === lessons.length - 1 function prev() { if (selLesIdx > 0) setSelectedLessonIdx(selLesIdx - 1) else if (selModIdx > 0) { const prevLessons = modules[selModIdx - 1].lessons setSelectedModuleIdx(selModIdx - 1) setSelectedLessonIdx(Math.max(prevLessons.length - 1, 0)) } } function next() { if (lessons.length && selLesIdx < lessons.length - 1) setSelectedLessonIdx(selLesIdx + 1) else if (selModIdx < modules.length - 1) { setSelectedModuleIdx(selModIdx + 1) setSelectedLessonIdx(0) } } function markComplete() { setCompleted(true) toast.success("Course Completed!") } if (authLoading || loading) return (
Loading course...
) if (error) return (
{error}
) if (!course) return (
Course not found.
) return (
{/* Sidebar: Always show all modules and lessons */} {/* Main: show lesson or course video/desc/mark as read */}
{modules.length > 0 && lessons.length > 0 && currentLesson ? ( <>

{currentLesson.title}

{currentLesson.video_url && (