'use client' import { useState } from 'react' import { Calendar, Loader2, X, Sparkles, CheckCircle2, BookOpen } from 'lucide-react' import { useLanguage } from '@/lib/i18n' import { cn } from '@/lib/utils' import { toast } from 'sonner' interface StudyDay { date: string noteIds: string[] noteTitles: string[] activity: string } export function StudyPlannerDialog({ notebookId, notebookName, onClose, }: { notebookId: string notebookName: string onClose: () => void }) { const { t } = useLanguage() const [examDate, setExamDate] = useState('') const [loading, setLoading] = useState(false) const [plan, setPlan] = useState<{ days: StudyDay[]; totalDays: number } | null>(null) const handleGenerate = async () => { if (!examDate) return setLoading(true) try { const res = await fetch('/api/ai/study-plan', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ notebookId, examDate }), }) const data = await res.json() if (!res.ok) { toast.error(data.errorKey === 'ai.featureLocked' ? (t('ai.featureLocked') || 'Plan requis') : (data.error || 'Erreur')) } else { setPlan(data) toast.success(t('wizard.studyPlanSuccess') || 'Planning créé ! Des rappels ont été ajoutés à vos notes.') } } catch (e: any) { toast.error(e.message || 'Erreur') } finally { setLoading(false) } } const today = new Date().toISOString().slice(0, 10) return (
e.stopPropagation()}>

{t('wizard.studyPlanner') || 'Planning de révision'}

{!plan && !loading && (

{t('wizard.studyPlannerDesc') || `L'IA va créer un planning de révision pour le carnet "${notebookName}" basé sur la répétition espacée.`}

setExamDate(e.target.value)} 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" />
)} {loading && (

{t('wizard.studyPlanLoading') || 'Création du planning...'}

)} {plan && (
{plan.totalDays} {t('wizard.daysPlanned') || 'jours planifiés'}
{plan.days.map((day, i) => (
{new Date(day.date).toLocaleDateString(undefined, { weekday: 'short' })}
{new Date(day.date).getDate()}

{day.activity}

{day.noteTitles.length > 0 && (
{day.noteTitles.map((title, j) => ( {title.length > 30 ? title.slice(0, 30) + '...' : title} ))}
)}
))}

{t('wizard.studyPlanReminders') || 'Des rappels ont été ajoutés automatiquement à vos notes.'}

)}
) }