import React from 'react'; import { useNavigate } from 'react-router-dom'; import { supabase } from '../../lib/supabase'; import { useSession } from '../../hooks/useSession'; import { useStoryCategories } from '../../hooks/useStoryCategories'; import { Wand2, ArrowLeft, ArrowRight } from 'lucide-react'; import { useStudentTracking } from '../../hooks/useStudentTracking'; interface Category { id: string; slug: string; title: string; description: string; icon: string; } interface StoryStep { title: string; key?: keyof StoryChoices; items?: Category[]; isContextStep?: boolean; } interface StoryChoices { theme_id: string | null; subject_id: string | null; character_id: string | null; setting_id: string | null; context?: string; } export function StoryGenerator() { const navigate = useNavigate(); const { session } = useSession(); const { themes, subjects, characters, settings, isLoading } = useStoryCategories(); const [step, setStep] = React.useState(1); const [choices, setChoices] = React.useState({ theme_id: null, subject_id: null, character_id: null, setting_id: null, context: '' }); const [isGenerating, setIsGenerating] = React.useState(false); const [error, setError] = React.useState(null); const [generationStatus, setGenerationStatus] = React.useState< 'idle' | 'creating' | 'generating-images' | 'saving' >('idle'); const { trackStoryGenerated } = useStudentTracking(); const startTime = React.useRef(Date.now()); const steps: StoryStep[] = [ { title: 'Escolha o Tema', items: themes || [], key: 'theme_id' }, { title: 'Escolha a Disciplina', items: subjects || [], key: 'subject_id' }, { title: 'Escolha o Personagem', items: characters || [], key: 'character_id' }, { title: 'Escolha o Cenário', items: settings || [], key: 'setting_id' }, { title: 'Adicione um Contexto (Opcional)', isContextStep: true } ]; const currentStep = steps[step - 1]; const isLastStep = step === steps.length; const handleSelect = (key: keyof StoryChoices, value: string) => { setChoices(prev => ({ ...prev, [key]: value })); if (step < steps.length) { setStep(prev => prev + 1); } }; const handleContextChange = (event: React.ChangeEvent) => { setChoices(prev => ({ ...prev, context: event.target.value })); }; const handleBack = () => { if (step > 1) { setStep(prev => prev - 1); } }; const handleGenerate = async () => { if (!session?.user?.id) return; if (!choices.theme_id || !choices.subject_id || !choices.character_id || !choices.setting_id) { setError('Por favor, preencha todas as escolhas antes de continuar.'); return; } try { setIsGenerating(true); setError(null); setGenerationStatus('creating'); const { data: story, error: storyError } = await supabase .from('stories') .insert({ student_id: session.user.id, title: 'Gerando...', theme_id: choices.theme_id, subject_id: choices.subject_id, character_id: choices.character_id, setting_id: choices.setting_id, context: choices.context || null, status: 'draft', content: { prompt: choices, pages: [] } }) .select() .single(); if (storyError) throw storyError; // Tracking da criação da história const selectedTheme = themes?.find(t => t.id === choices.theme_id)?.title || ''; const selectedSubject = subjects?.find(s => s.id === choices.subject_id)?.title || ''; const selectedCharacter = characters?.find(c => c.id === choices.character_id)?.title || ''; const selectedSetting = settings?.find(s => s.id === choices.setting_id)?.title || ''; trackStoryGenerated({ story_id: story.id, theme: selectedTheme, subject: selectedSubject, character: selectedCharacter, setting: selectedSetting, context: choices.context, generation_time: Date.now() - startTime.current, word_count: 0, // será atualizado após a geração student_id: session.user.id, school_id: session.user.user_metadata?.school_id, class_id: session.user.user_metadata?.class_id }); setGenerationStatus('generating-images'); console.log('Chamando Edge Function com:', story); const { data: functionData, error: functionError } = await supabase.functions .invoke('generate-story', { body: { record: story } }); console.log('Resposta da Edge Function:', functionData); if (functionError) { throw new Error(`Erro na Edge Function: ${functionError.message}`); } setGenerationStatus('saving'); const { data: updatedStory, error: updateError } = await supabase .from('stories') .select('*') .eq('id', story.id) .single(); if (updateError) throw updateError; // Atualizar a contagem de palavras após a geração const wordCount = updatedStory.content.pages.reduce((acc: number, page: { text: string }) => acc + page.text.split(/\s+/).length, 0); await supabase.from('story_metrics').insert({ story_id: story.id, word_count: wordCount, generation_time: Date.now() - startTime.current }); navigate(`/aluno/historias/${story.id}`); } catch (err) { console.error('Erro ao gerar história:', err); setError('Não foi possível criar sua história. Tente novamente.'); } finally { setIsGenerating(false); setGenerationStatus('idle'); } }; const getGenerationStatusText = () => { switch (generationStatus) { case 'creating': return 'Iniciando criação...'; case 'generating-images': return 'Gerando história e imagens...'; case 'saving': return 'Finalizando...'; default: return 'Criar História Mágica'; } }; if (isLoading) { return (
{[...Array(4)].map((_, i) => (
))}
); } return (
{/* Progress Bar */}
{steps.map((s, i) => (
))}

{currentStep.title}

{currentStep.isContextStep ? (