'use client' import { useState } from 'react' import { Sparkles, Loader2, X, Tag, Copy, FolderTree, AlertTriangle, Check } from 'lucide-react' import { useLanguage } from '@/lib/i18n' import { cn } from '@/lib/utils' import { toast } from 'sonner' interface SuggestedTag { name: string; noteIds: string[] } interface Duplicate { note1Id: string; note1Title: string; note2Id: string; note2Title: string; reason: string } interface Category { name: string; noteIds: string[]; noteTitles: string[] } interface OrgResult { suggestedTags: SuggestedTag[] duplicates: Duplicate[] categories: Category[] summary: string } export function NotebookOrganizerDialog({ notebookId, notebookName, onClose, }: { notebookId: string notebookName: string onClose: () => void }) { const { t } = useLanguage() const [loading, setLoading] = useState(false) const [result, setResult] = useState(null) const [appliedTags, setAppliedTags] = useState>(new Set()) const handleAnalyze = async () => { setLoading(true) try { const res = await fetch('/api/ai/organize-notebook', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ notebookId }), }) const data = await res.json() if (!res.ok) { toast.error(data.errorKey === 'ai.featureLocked' ? (t('ai.featureLocked') || 'Plan requis') : (data.error || 'Erreur')) } else { setResult(data) window.dispatchEvent(new Event('ai-usage-changed')) } } catch (e: any) { toast.error(e.message || 'Erreur') } finally { setLoading(false) } } const applyTag = async (tagName: string, noteIds: string[]) => { try { const res = await fetch('/api/ai/organize-notebook', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'apply_tag', tagName, noteIds, notebookId }), }) if (!res.ok) throw new Error('Failed') setAppliedTags(prev => new Set(prev).add(tagName)) toast.success(t('structuredViews.tagApplied') || `Tag "${tagName}" appliqué à ${noteIds.length} notes`) } catch { toast.error('Erreur') } } return (
e.stopPropagation()}>

{t('wizard.organizer') || 'Organiser avec l\'IA'}

{!result && !loading && (

{t('wizard.organizerDesc') || `L'IA va analyser toutes les notes du carnet "${notebookName}" et proposer : tags, regroupements, et détection de doublons.`}

)} {loading && (

{t('wizard.organizing') || 'Analyse des notes...'}

)} {result && (
{/* Summary */}

{result.summary}

{/* Tags */} {result.suggestedTags.length > 0 && (

{t('wizard.suggestedTags') || 'Tags suggérés'}

{result.suggestedTags.map((tag, i) => (
{tag.name} {tag.noteIds.length} notes
))}
)} {/* Categories */} {result.categories.length > 0 && (

{t('wizard.categories') || 'Regroupements suggérés'}

{result.categories.map((cat, i) => (
{cat.name}
{cat.noteTitles.map((title, j) => ( {title.length > 35 ? title.slice(0, 35) + '...' : title} ))}
))}
)} {/* Duplicates */} {result.duplicates.length > 0 && (

{t('wizard.duplicates') || 'Doublons détectés'}

{result.duplicates.map((dup, i) => (
{dup.note1Title} {dup.note2Title}

{dup.reason}

))}
)} {result.suggestedTags.length === 0 && result.duplicates.length === 0 && result.categories.length === 0 && (

{t('wizard.noSuggestions') || 'Aucune suggestion — le carnet semble bien organisé.'}

)}
)}
) }