'use client' import { useState, useEffect } from 'react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from './ui/dialog' import { Sparkles, CheckCircle2, Loader2, Tag } from 'lucide-react' import { toast } from 'sonner' import { useLanguage } from '@/lib/i18n' import { useAiConsent } from '@/components/legal/ai-consent-provider' import type { AutoLabelSuggestion, SuggestedLabel } from '@/lib/ai/services' import { cn } from '@/lib/utils' interface AutoLabelSuggestionDialogProps { open: boolean onOpenChange: (open: boolean) => void notebookId: string | null onLabelsCreated: () => void } export function AutoLabelSuggestionDialog({ open, onOpenChange, notebookId, onLabelsCreated, }: AutoLabelSuggestionDialogProps) { const { t } = useLanguage() const { requestAiConsent } = useAiConsent() const [suggestions, setSuggestions] = useState(null) const [loading, setLoading] = useState(false) const [creating, setCreating] = useState(false) const [selectedLabels, setSelectedLabels] = useState>(new Set()) useEffect(() => { if (open && notebookId) { fetchSuggestions() } else { setSuggestions(null) setSelectedLabels(new Set()) } }, [open, notebookId]) const fetchSuggestions = async () => { if (!notebookId) return const consented = await requestAiConsent() if (!consented) return setLoading(true) try { const response = await fetch('/api/ai/auto-labels', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ notebookId, language: document.documentElement.lang || 'en', }), }) const data = await response.json() if (data.success && data.data) { setSuggestions(data.data) const allLabelNames = new Set(data.data.suggestedLabels.map((l: SuggestedLabel) => l.name as string)) setSelectedLabels(allLabelNames) } else { if (data.message) { toast.info(data.message) } else { toast.info(t('ai.autoLabels.noSuggestions') || 'Pas assez de notes pour générer des labels (minimum 15)') } onOpenChange(false) } } catch (error) { console.error('Failed to fetch label suggestions:', error) toast.error(t('ai.autoLabels.error')) onOpenChange(false) } finally { setLoading(false) } } const toggleLabelSelection = (labelName: string) => { const newSelected = new Set(selectedLabels) if (newSelected.has(labelName)) { newSelected.delete(labelName) } else { newSelected.add(labelName) } setSelectedLabels(newSelected) } const handleCreateLabels = async () => { if (!suggestions || selectedLabels.size === 0) { toast.error(t('ai.autoLabels.noLabelsSelected')) return } setCreating(true) try { const response = await fetch('/api/ai/auto-labels', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ suggestions, selectedLabels: Array.from(selectedLabels), }), }) const data = await response.json() if (data.success) { toast.success(t('ai.autoLabels.created', { count: data.data.createdCount })) onLabelsCreated() onOpenChange(false) } else { toast.error(data.error || t('ai.autoLabels.error')) } } catch (error) { console.error('Failed to create labels:', error) toast.error(t('ai.autoLabels.error')) } finally { setCreating(false) } } if (loading) { return ( {t('ai.autoLabels.analyzing')}

{t('ai.autoLabels.analyzing')}

) } if (!suggestions) { return null } return ( {t('ai.autoLabels.title')} {t('ai.autoLabels.description', { notebook: suggestions.notebookName, count: suggestions.totalNotes, })}
{suggestions.suggestedLabels.map((label) => { const isSelected = selectedLabels.has(label.name) return (
toggleLabelSelection(label.name)} >
{isSelected && }
{label.name}
{t('ai.autoLabels.notesCount', { count: label.count })} {Math.round(label.confidence * 100)}%
) })}
) }