'use client' import React, { useState, useEffect, useCallback } from 'react' import { useParams, useRouter } from 'next/navigation' import { useTheme } from '@/lib/ThemeContext' import { QuizQuestion } from '@/components/learn/QuizQuestion' interface MCQuestion { id: string question: string options: { id: string; text: string }[] correct_answer: string explanation?: string } function getBackendUrl() { if (typeof window === 'undefined') return 'http://localhost:8001' const { hostname, protocol } = window.location if (hostname === 'localhost') return 'http://localhost:8001' return `${protocol}//${hostname}:8001` } export default function QuizPage() { const { unitId } = useParams<{ unitId: string }>() const router = useRouter() const { isDark } = useTheme() const [questions, setQuestions] = useState([]) const [currentIndex, setCurrentIndex] = useState(0) const [isLoading, setIsLoading] = useState(true) const [error, setError] = useState(null) const [stats, setStats] = useState({ correct: 0, incorrect: 0 }) const [isComplete, setIsComplete] = useState(false) const glassCard = isDark ? 'bg-white/10 backdrop-blur-xl border border-white/10' : 'bg-white/80 backdrop-blur-xl border border-black/5' useEffect(() => { loadMC() }, [unitId]) const loadMC = async () => { setIsLoading(true) try { const resp = await fetch(`${getBackendUrl()}/api/learning-units/${unitId}/mc`) if (!resp.ok) throw new Error(`HTTP ${resp.status}`) const data = await resp.json() setQuestions(data.questions || []) } catch (err: any) { setError(err.message) } finally { setIsLoading(false) } } const handleAnswer = useCallback((correct: boolean) => { setStats((prev) => ({ correct: prev.correct + (correct ? 1 : 0), incorrect: prev.incorrect + (correct ? 0 : 1), })) if (currentIndex + 1 >= questions.length) { setIsComplete(true) } else { setCurrentIndex((i) => i + 1) } }, [currentIndex, questions.length]) if (isLoading) { return (
) } return (
{/* Header */}

Quiz

{questions.length} Fragen
{/* Content */}
{error ? (

{error}

) : isComplete ? (
{stats.correct === questions.length ? '🏆' : stats.correct > stats.incorrect ? '🎉' : '💪'}

{stats.correct === questions.length ? 'Perfekt!' : 'Geschafft!'}

{stats.correct} von {questions.length} richtig ({Math.round((stats.correct / questions.length) * 100)}%)

) : questions[currentIndex] ? ( ) : (

Keine Quiz-Fragen verfuegbar.

)}
) }