"use client" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import api from "@/lib/api" import type { Question, Feedback, TestStartRequest, TestStartResponse, TestAnswerRequest, TestAnswerResponse, } from "@/lib/types" import { QuestionCard } from "./question-card" import { FeedbackPanel } from "./feedback-panel" import { ProgressTracker } from "./progress-tracker" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Label } from "@/components/ui/label" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" import { Loader2 } from "lucide-react" type TestState = "subject_selection" | "in_progress" | "showing_feedback" | "completed" export function TestingSession() { const { user, isLoadingAuth } = useAuth() const router = useRouter() const [testState, setTestState] = useState("subject_selection") const [selectedSubject, setSelectedSubject] = useState(null) const [sessionId, setSessionId] = useState(null) const [currentQuestion, setCurrentQuestion] = useState(null) const [questionNumber, setQuestionNumber] = useState(0) const [totalQuestions, setTotalQuestions] = useState(0) const [feedback, setFeedback] = useState(null) const [isSubmittingAnswer, setIsSubmittingAnswer] = useState(false) const [isStartingTest, setIsStartingTest] = useState(false) const availableSubjects = ["Math", "Science", "History", "Literature"] // Example subjects useEffect(() => { if (!isLoadingAuth && !user) { toast.error("Please connect your wallet to take a test.") router.push("/") // Redirect to home if not authenticated } }, [user, isLoadingAuth, router]) const startTest = async () => { if (!selectedSubject) { toast.error("Please select a subject to start the test.") return } setIsStartingTest(true) try { const response = await api.post("/api/test/start", { subject: selectedSubject, } as TestStartRequest) setSessionId(response.data.session_id) setCurrentQuestion(response.data.question) setQuestionNumber(response.data.question_number) setTotalQuestions(response.data.total_questions) setFeedback(null) setTestState("in_progress") toast.success(`Test started for ${selectedSubject}!`) } catch (error: any) { console.error("Error starting test:", error) toast.error(error.response?.data?.message || "Failed to start test.") } finally { setIsStartingTest(false) } } const handleAnswerSubmit = async (questionId: string, answerIndex: number) => { if (!sessionId) { toast.error("No active test session found.") return } setIsSubmittingAnswer(true) try { const response = await api.post("/api/test/answer", { session_id: sessionId, question_id: questionId, answer: answerIndex, } as TestAnswerRequest) setFeedback(response.data.feedback) setCurrentQuestion(response.data.next_question) setQuestionNumber((prev) => prev + 1) // Increment question number for display if (response.data.test_completed) { setTestState("completed") toast.success("Test completed!") } else { setTestState("showing_feedback") } } catch (error: any) { console.error("Error submitting answer:", error) toast.error(error.response?.data?.message || "Failed to submit answer.") } finally { setIsSubmittingAnswer(false) } } const handleContinue = () => { if (currentQuestion) { setTestState("in_progress") setFeedback(null) } else { // This case should ideally not happen if test_completed is true setTestState("completed") } } const handleStartNewTest = () => { setTestState("subject_selection") setSelectedSubject(null) setSessionId(null) setCurrentQuestion(null) setQuestionNumber(0) setTotalQuestions(0) setFeedback(null) } if (isLoadingAuth || !user) { return (
Loading authentication...
) } return (
{testState === "subject_selection" && ( Select a Subject {availableSubjects.map((subject) => (
))}
)} {(testState === "in_progress" || testState === "showing_feedback") && currentQuestion && (
{testState === "in_progress" && ( )} {testState === "showing_feedback" && feedback && ( )}
)} {testState === "completed" && ( Test Completed!

You have successfully completed the test.

)}
) }