import React, { useState, useEffect } from 'react'; import { RefreshCw, Trash2, CheckCircle, Loader, AlertCircle, HelpCircle, ArrowLeft, Eye, ChevronDown, ChevronUp } from 'lucide-react'; import { forceGenerateTopicQuestions, getTopicQuestionBank, deleteQuestion } from '../../lib/testService'; import * as db from '../../lib/db'; import Card from '../ui/Card'; import Button from '../ui/Button'; import Tag from '../ui/Tag'; import { motion, AnimatePresence } from 'framer-motion'; const TestManager = () => { const [topics, setTopics] = useState([]); const [questionCounts, setQuestionCounts] = useState({}); const [selectedTopic, setSelectedTopic] = useState(null); const [questions, setQuestions] = useState([]); const [loadingTopicId, setLoadingTopicId] = useState(null); const [error, setError] = useState(null); const loadData = async () => { const allTopics = await db.getTopics(); setTopics(allTopics); const counts = {}; await Promise.all(allTopics.map(async t => { const bank = await getTopicQuestionBank(t.id); counts[t.id] = bank.length; })); setQuestionCounts(counts); if (selectedTopic) { setQuestions(await getTopicQuestionBank(selectedTopic.id)); } }; useEffect(() => { loadData(); }, [selectedTopic]); const handleGenerate = async (topic, count = 10) => { setLoadingTopicId(topic.id); setError(null); try { await forceGenerateTopicQuestions(topic, count); loadData(); // refresh list } catch (e) { setError(e.message); } finally { setLoadingTopicId(null); } }; const handleDelete = (topicId, questionId) => { deleteQuestion(topicId, questionId); loadData(); }; // ── Detail view ── if (selectedTopic) { return (
{/* Header */}

{selectedTopic.label}

{questions.length} questions

{selectedTopic.description}

{error && (
{error}
)} {/* Questions List */}
{questions.length === 0 ? (

No questions generated for this topic yet.

) : ( questions.map((q, i) => (
{i + 1}

{q.question}

{q.options.map((opt, oi) => (
{oi === q.correctIndex && } {opt}
))}

{q.explanation}

)) )}
); } // ── List view ── return (
{topics.length === 0 && (

No topics available.

)} {error && !selectedTopic && (
{error}
)} {topics.map(topic => { const count = questionCounts[topic.id] || 0; const isLoading = loadingTopicId === topic.id; return (

{topic.label}

0 ? 'success' : 'dark'} className="text-xs"> {count} {count === 1 ? 'question' : 'questions'}

{topic.description}

{count === 0 ? ( ) : ( )}
); })}
); }; export default TestManager;