import React, { useState, useEffect } from "react"; import { User } from "@/entities/User"; import { InvokeLLM } from "@/integrations/Core"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { BookOpen, FileText, Lightbulb, RefreshCw, Download, Sparkles, GraduationCap, Target, Brain, CheckCircle } from "lucide-react"; const TOPICS = [ { value: "organic_chemistry", label: "Organic Chemistry", icon: "๐Ÿงช", color: "bg-green-100 text-green-800" }, { value: "inorganic_chemistry", label: "Inorganic Chemistry", icon: "โš›๏ธ", color: "bg-blue-100 text-blue-800" }, { value: "physical_chemistry", label: "Physical Chemistry", icon: "๐Ÿ”ฌ", color: "bg-purple-100 text-purple-800" }, { value: "analytical_chemistry", label: "Analytical Chemistry", icon: "๐Ÿ“Š", color: "bg-orange-100 text-orange-800" }, { value: "biochemistry", label: "Biochemistry", icon: "๐Ÿงฌ", color: "bg-pink-100 text-pink-800" }, { value: "quantum_chemistry", label: "Quantum Chemistry", icon: "โšก", color: "bg-indigo-100 text-indigo-800" }, { value: "materials_science", label: "Materials Science", icon: "๐Ÿ—๏ธ", color: "bg-cyan-100 text-cyan-800" } ]; const MATERIAL_TYPES = [ { value: "study_guide", label: "Study Guide", icon: BookOpen, description: "Comprehensive topic overview" }, { value: "flashcards", label: "Flashcards", icon: Brain, description: "Key terms and definitions" }, { value: "practice_problems", label: "Practice Problems", icon: Target, description: "Problems with solutions" }, { value: "concept_map", label: "Concept Map", icon: Lightbulb, description: "Visual concept connections" } ]; export default function StudyMaterials() { const [user, setUser] = useState(null); const [selectedTopic, setSelectedTopic] = useState(""); const [selectedType, setSelectedType] = useState(""); const [generatedMaterial, setGeneratedMaterial] = useState(null); const [isGenerating, setIsGenerating] = useState(false); useEffect(() => { loadUser(); }, []); const loadUser = async () => { try { const userData = await User.me(); setUser(userData); } catch (error) { await User.login(); } }; const generateMaterial = async () => { if (!selectedTopic || !selectedType) return; setIsGenerating(true); setGeneratedMaterial(null); try { const topic = TOPICS.find(t => t.value === selectedTopic); const materialType = MATERIAL_TYPES.find(t => t.value === selectedType); let prompt = ""; let schema = {}; switch (selectedType) { case "study_guide": prompt = `Create a comprehensive study guide for ${topic.label} suitable for a ${user?.learning_level || 'beginner'} level student. Include: - Overview and importance - Key concepts with clear explanations - Important formulas or reactions - Real-world applications - Common misconceptions - Study tips and memory aids`; schema = { type: "object", properties: { title: { type: "string" }, overview: { type: "string" }, key_concepts: { type: "array", items: { type: "object", properties: { concept: { type: "string" }, explanation: { type: "string" } } } }, formulas: { type: "array", items: { type: "string" } }, applications: { type: "array", items: { type: "string" } }, misconceptions: { type: "array", items: { type: "string" } }, study_tips: { type: "array", items: { type: "string" } } } }; break; case "flashcards": prompt = `Create 15-20 flashcards for ${topic.label} at ${user?.learning_level || 'beginner'} level. Each flashcard should have: - A clear, concise question or term - A comprehensive answer or definition - Include key formulas, reactions, or concepts`; schema = { type: "object", properties: { title: { type: "string" }, cards: { type: "array", items: { type: "object", properties: { front: { type: "string" }, back: { type: "string" } } } } } }; break; case "practice_problems": prompt = `Create 8-10 practice problems for ${topic.label} at ${user?.learning_level || 'beginner'} level. Include: - Varied difficulty within the level - Step-by-step solutions - Brief explanations of key concepts used`; schema = { type: "object", properties: { title: { type: "string" }, problems: { type: "array", items: { type: "object", properties: { question: { type: "string" }, solution: { type: "string" }, concepts_used: { type: "array", items: { type: "string" } } } } } } }; break; case "concept_map": prompt = `Create a concept map structure for ${topic.label} at ${user?.learning_level || 'beginner'} level showing: - Main concepts and their relationships - Hierarchical organization - Key connections between ideas - Brief descriptions for each concept`; schema = { type: "object", properties: { title: { type: "string" }, main_concept: { type: "string" }, concepts: { type: "array", items: { type: "object", properties: { name: { type: "string" }, description: { type: "string" }, level: { type: "number" }, connections: { type: "array", items: { type: "string" } } } } } } }; break; } const result = await InvokeLLM({ prompt, response_json_schema: schema }); setGeneratedMaterial({ type: selectedType, topic: selectedTopic, data: result }); } catch (error) { console.error("Error generating material:", error); } setIsGenerating(false); }; const downloadMaterial = () => { if (!generatedMaterial) return; let content = ""; const { data } = generatedMaterial; switch (generatedMaterial.type) { case "study_guide": content = `${data.title}\n\n`; content += `Overview:\n${data.overview}\n\n`; if (data.key_concepts) { content += "Key Concepts:\n"; data.key_concepts.forEach(concept => { content += `- ${concept.concept}: ${concept.explanation}\n`; }); content += "\n"; } if (data.formulas && data.formulas.length > 0) { content += "Important Formulas/Reactions:\n"; data.formulas.forEach(formula => { content += `- ${formula}\n`; }); content += "\n"; } if (data.applications && data.applications.length > 0) { content += "Real-world Applications:\n"; data.applications.forEach(app => { content += `- ${app}\n`; }); content += "\n"; } if (data.misconceptions && data.misconceptions.length > 0) { content += "Common Misconceptions:\n"; data.misconceptions.forEach(misconception => { content += `- ${misconception}\n`; }); content += "\n"; } if (data.study_tips && data.study_tips.length > 0) { content += "Study Tips:\n"; data.study_tips.forEach(tip => { content += `- ${tip}\n`; }); content += "\n"; } break; case "flashcards": content = `${data.title}\n\nFlashcards:\n\n`; data.cards?.forEach((card, index) => { content += `Card ${index + 1}:\nQ: ${card.front}\nA: ${card.back}\n\n`; }); break; case "practice_problems": content = `${data.title}\n\nPractice Problems:\n\n`; data.problems?.forEach((problem, index) => { content += `Problem ${index + 1}:\nQuestion: ${problem.question}\nSolution: ${problem.solution}\nConcepts Used: ${problem.concepts_used?.join(', ')}\n\n`; }); break; case "concept_map": content = `${data.title}\n\nMain Concept: ${data.main_concept}\n\n`; content += "Concepts:\n"; data.concepts?.forEach(concept => { content += `- Name: ${concept.name}\n Description: ${concept.description}\n Level: ${concept.level}\n Connections: ${concept.connections?.join(', ')}\n\n`; }); break; } const blob = new Blob([content], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${data.title.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_.]/g, '')}.txt`; // Sanitize filename document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; const renderMaterial = () => { if (!generatedMaterial) return null; const { data, type } = generatedMaterial; switch (type) { case "study_guide": return (
{data.title}

{data.overview}

{data.key_concepts && data.key_concepts.length > 0 && ( Key Concepts
{data.key_concepts.map((concept, index) => (

{concept.concept}

{concept.explanation}

))}
)} {data.formulas && data.formulas.length > 0 && ( Important Formulas/Reactions
    {data.formulas.map((formula, index) => (
  • {formula}
  • ))}
)} {data.applications && data.applications.length > 0 && ( Real-world Applications
    {data.applications.map((app, index) => (
  • {app}
  • ))}
)} {data.misconceptions && data.misconceptions.length > 0 && ( Common Misconceptions
    {data.misconceptions.map((misconception, index) => (
  • {misconception}
  • ))}
)} {data.study_tips && data.study_tips.length > 0 && ( Study Tips
    {data.study_tips.map((tip, index) => (
  • {tip}
  • ))}
)}
); case "flashcards": return (
{data.title}

{data.cards?.length} flashcards

{data.cards?.map((card, index) => (
Card {index + 1}

Question:

{card.front}

Answer:

{card.back}

))}
); case "practice_problems": return (
{data.title}

{data.problems?.length} practice problems

{data.problems?.map((problem, index) => (

Problem {index + 1}:

{problem.question}

Solution:

{problem.solution}

{problem.concepts_used && problem.concepts_used.length > 0 && (

Concepts Used:

{problem.concepts_used.map((concept, idx) => ( {concept} ))}
)}
))}
); case "concept_map": return (
{data.title}

Main Concept: {data.main_concept}

{data.concepts && data.concepts.length > 0 && ( Related Concepts
{data.concepts.map((concept, index) => (

{concept.name}

{concept.description}

{concept.connections && concept.connections.length > 0 && (
Connections:
{concept.connections.map((conn, idx) => ( {conn} ))}
)}
))}
)}
); default: return ( Material type not yet implemented for display. ); } }; return (

AI Study Materials

Generate personalized learning resources with AI

{/* Material Generator */} Generate Study Material
{TOPICS.map((topic) => ( ))}
{MATERIAL_TYPES.map((type) => ( ))}
{/* Generated Material */} {generatedMaterial && (
Generated Material
t.value === generatedMaterial.topic)?.color}> {TOPICS.find(t => t.value === generatedMaterial.topic)?.label} {MATERIAL_TYPES.find(t => t.value === generatedMaterial.type)?.label}
{renderMaterial()}
)} {/* Empty State */} {!generatedMaterial && !isGenerating && (

Ready to Study?

Select a chemistry topic and material type above to generate personalized study materials!

)}
); }