"use client" import Link from "next/link" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import type { Lesson } from "@/lib/types" import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Loader2 } from "lucide-react" import ReactMarkdown from "react-markdown" import { api } from "@/lib/api" interface LessonViewerProps { courseId: string lessonId: string } export function LessonViewer({ courseId, lessonId }: LessonViewerProps) { const { user, firebaseUser, isLoadingAuth, authMethod, token } = useAuth() // Check token for access const router = useRouter() const [lesson, setLesson] = useState(null) const [isLoadingLesson, setIsLoadingLesson] = useState(true) const [isMarkingCompleted, setIsMarkingCompleted] = useState(false) const [error, setError] = useState(null) useEffect(() => { if (!isLoadingAuth && !user && !firebaseUser) { // Allow either MetaMask or Firebase user toast.error("Please login to view lessons.") router.push("/") return } const fetchLesson = async () => { setIsLoadingLesson(true) setError(null) try { const response = await api.get(`/api/courses/${courseId}/lessons/${lessonId}`) setLesson(response.data) } catch (err: any) { console.error("Failed to fetch lesson:", err) setError(err.response?.data?.message || "Failed to load lesson.") toast.error(err.response?.data?.message || "Failed to load lesson.") } finally { setIsLoadingLesson(false) } } if (user || firebaseUser) { // Only fetch if either user type is logged in fetchLesson() } }, [user, firebaseUser, isLoadingAuth, router, courseId, lessonId, token]) const markLessonCompleted = async () => { if (!lesson || lesson.completed || !token) { toast.error("Please connect your MetaMask wallet to mark lessons as completed.") return // Ensure token exists for this action } setIsMarkingCompleted(true) try { await api.post(`/api/courses/${courseId}/lessons/${lessonId}/complete`) setLesson((prev) => (prev ? { ...prev, completed: true } : null)) toast.success("Lesson marked as completed!") } catch (err: any) { console.error("Failed to mark lesson completed:", err) toast.error(err.response?.data?.message || "Failed to mark lesson completed.") } finally { setIsMarkingCompleted(false) } } if (isLoadingAuth || isLoadingLesson) { return (
Loading lesson...
) } if (error) { return (

{error}

) } if (!lesson) { return (

Lesson not found.

) } return ( {lesson.title} {authMethod === "firebase" && !token && (

Limited Access

You are logged in with email. Full functionality, including progress tracking and data persistence, requires connecting your MetaMask wallet.

)} {lesson.type === "video" && (

Video Player Placeholder: {lesson.content}

{/* In a real app, you'd embed a video player here */}
)} {lesson.type === "text" && (
{lesson.content}
)} {lesson.type === "code" && (
              {lesson.content}
            
)} {lesson.type === "quiz" && (

Quiz Link:

Start Quiz: {lesson.content}
)}
{!lesson.completed && ( )} {lesson.completed && ( )}
) }