'use client' import { useState } from 'react' import { GraduationCap, BookOpen, Wrench, Sparkles, Loader2, X, ArrowRight, ArrowLeft, Check, FileText } from 'lucide-react' import { useLanguage } from '@/lib/i18n' import { cn } from '@/lib/utils' import { toast } from 'sonner' type WizardProfile = 'student' | 'teacher' | 'engineer' interface ProfileOption { id: WizardProfile icon: typeof GraduationCap titleKey: string descKey: string placeholderKey: string } const PROFILES: ProfileOption[] = [ { id: 'student', icon: GraduationCap, titleKey: 'wizard.profileStudent', descKey: 'wizard.profileStudentDesc', placeholderKey: 'wizard.topicStudentPlaceholder', }, { id: 'teacher', icon: BookOpen, titleKey: 'wizard.profileTeacher', descKey: 'wizard.profileTeacherDesc', placeholderKey: 'wizard.topicTeacherPlaceholder', }, { id: 'engineer', icon: Wrench, titleKey: 'wizard.profileEngineer', descKey: 'wizard.profileEngineerDesc', placeholderKey: 'wizard.topicEngineerPlaceholder', }, ] const LEVELS = ['wizard.levelBeginner', 'wizard.levelIntermediate', 'wizard.levelAdvanced', 'wizard.levelExpert'] export function AiNotebookWizard({ onClose, onComplete }: { onClose: () => void; onComplete: (notebookId: string) => void }) { const { t, language } = useLanguage() const [step, setStep] = useState<0 | 1 | 2>(0) const [profile, setProfile] = useState(null) const [topic, setTopic] = useState('') const [level, setLevel] = useState(1) const [count, setCount] = useState(6) const [loading, setLoading] = useState(false) const [progressMsg, setProgressMsg] = useState('') const [success, setSuccess] = useState<{ notebookId: string; notebookName: string; noteTitles: string[] } | null>(null) const handleSubmit = async () => { if (!profile || !topic.trim()) return setLoading(true) setProgressMsg(t('wizard.progressGenerating') || 'Génération du contenu par l\'IA...') try { setProgressMsg(t('wizard.progressCalling') || 'Appel de l\'IA...') const res = await fetch('/api/ai/notebook-wizard', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ profile, topic: topic.trim(), level: t(LEVELS[level]), count, language, notebookName: topic.trim(), }), }) setProgressMsg(t('wizard.progressParsing') || 'Analyse de la réponse...') const data = await res.json() if (!res.ok) { if (data.errorKey === 'ai.featureLocked') { toast.error(t('ai.featureLocked') || 'Cette fonctionnalité nécessite un plan supérieur') } else if (data.errorKey === 'ai.quotaExceeded') { toast.error(t('ai.quotaExceeded') || 'Quota IA dépassé') } else { toast.error(data.error || 'Erreur') } setLoading(false) return } setProgressMsg(t('wizard.progressCreating') || 'Création du carnet et des notes...') setSuccess({ notebookId: data.notebookId, notebookName: data.notebookName || topic, noteTitles: data.noteTitles || [], }) setLoading(false) } catch (e: any) { toast.error(e.message || 'Erreur') setLoading(false) } } const selectedProfile = PROFILES.find(p => p.id === profile) return (
e.stopPropagation()} > {/* Header */}

{t('wizard.title')}

{/* Progress bar */}
{[0, 1, 2].map(s => (
{s + 1}
{s < 2 &&
}
))}
{/* Content */}
{success ? (

{t('wizard.created') || 'Carnet créé :'} {success.notebookName}

{success.noteTitles.length} {t('wizard.notesCreated') || 'notes créées'}

{success.noteTitles.map((title, i) => (
{title}
))}
) : loading ? (

{progressMsg}

{['Toggle', 'Callout', 'Math', 'Colonnes', 'Sommaire', 'Link Preview'].map(tag => ( {tag} ))}
) : step === 0 ? ( /* Step 1: Choose profile */

{t('wizard.chooseProfile')}

{PROFILES.map(p => ( ))}
) : step === 1 ? ( /* Step 2: Topic + options */
setTopic(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && topic.trim()) { setStep(2) } }} placeholder={selectedProfile ? t(selectedProfile.placeholderKey) : ''} autoFocus className="w-full rounded-lg border border-border bg-background px-4 py-3 text-sm outline-none focus:ring-2 focus:ring-brand-accent/30" />
{LEVELS.map((lvlKey, i) => ( ))}
setCount(Number(e.target.value))} className="w-full accent-brand-accent" />
3 12
) : ( /* Step 3: Confirm */
{selectedProfile && } {selectedProfile && t(selectedProfile.titleKey)}
{t('wizard.topic')}{topic}
{t('wizard.level')}{t(LEVELS[level])}
{t('wizard.noteCount')}{count} {t('wizard.notes')}

{t('wizard.confirmHint') || 'L\'IA va créer un carnet avec des notes riches : encadrés, sections repliables, formules mathématiques, colonnes de comparaison, et plus.'}

)}
) }