'use client' import { useMemo } from 'react' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from '@/components/ui/collapsible' import { AlertCircle, AlertTriangle, Info, ChevronDown, Lightbulb, Plus, } from 'lucide-react' import { DataPoint } from '@/lib/sdk/einwilligungen/types' import { validateDocument, ValidationWarning, } from '@/lib/sdk/document-generator/datapoint-helpers' interface DocumentValidationProps { dataPoints: DataPoint[] documentContent: string language?: 'de' | 'en' onInsertPlaceholder?: (placeholder: string) => void } /** * Icon für den Warnungstyp */ function getWarningIcon(type: ValidationWarning['type']) { switch (type) { case 'error': return AlertCircle case 'warning': return AlertTriangle case 'info': default: return Info } } /** * Alert-Variante für den Warnungstyp */ function getAlertVariant(type: ValidationWarning['type']): 'default' | 'destructive' { return type === 'error' ? 'destructive' : 'default' } /** * Placeholder-Vorschlag aus der Warnung extrahieren */ function extractPlaceholderSuggestion(warning: ValidationWarning): string | null { const match = warning.suggestion.match(/\[([A-Z_]+)\]/) return match ? match[0] : null } /** * DocumentValidation Komponente * * Zeigt Validierungswarnungen basierend auf ausgewählten Datenpunkten und * dem generierten Dokumentinhalt. */ export function DocumentValidation({ dataPoints, documentContent, language = 'de', onInsertPlaceholder, }: DocumentValidationProps) { // Führe Validierung durch const warnings = useMemo(() => { if (dataPoints.length === 0 || !documentContent) { return [] } return validateDocument(dataPoints, documentContent, language) }, [dataPoints, documentContent, language]) // Gruppiere nach Typ const errorCount = warnings.filter(w => w.type === 'error').length const warningCount = warnings.filter(w => w.type === 'warning').length const infoCount = warnings.filter(w => w.type === 'info').length if (warnings.length === 0) { // Keine Warnungen - zeige Erfolgsmeldung wenn Datenpunkte vorhanden if (dataPoints.length > 0 && documentContent.length > 100) { return ( {language === 'de' ? 'Dokument valide' : 'Document valid'} {language === 'de' ? 'Alle notwendigen Abschnitte für die ausgewählten Datenpunkte sind vorhanden.' : 'All necessary sections for the selected data points are present.'} ) } return null } return (
{/* Zusammenfassung */}
{language === 'de' ? 'Validierung:' : 'Validation:'} {errorCount > 0 && ( {errorCount} {language === 'de' ? 'Fehler' : 'Error'} {errorCount > 1 && (language === 'de' ? '' : 's')} )} {warningCount > 0 && ( {warningCount} {language === 'de' ? 'Warnung' : 'Warning'} {warningCount > 1 && (language === 'de' ? 'en' : 's')} )} {infoCount > 0 && ( {infoCount} {language === 'de' ? 'Hinweis' : 'Info'} {infoCount > 1 && (language === 'de' ? 'e' : 's')} )}
{/* Warnungen */} {warnings.map((warning, index) => { const Icon = getWarningIcon(warning.type) const placeholder = extractPlaceholderSuggestion(warning) return ( {warning.message} {/* Vorschlag */}
{warning.suggestion}
{/* Quick-Fix Button */} {placeholder && onInsertPlaceholder && ( )} {/* Betroffene Datenpunkte */} {warning.affectedDataPoints && warning.affectedDataPoints.length > 0 && ( {warning.affectedDataPoints.length}{' '} {language === 'de' ? 'betroffene Datenpunkte' : 'affected data points'}
    {warning.affectedDataPoints.slice(0, 5).map(dp => (
  • {language === 'de' ? dp.name.de : dp.name.en}
  • ))} {warning.affectedDataPoints.length > 5 && (
  • ... {language === 'de' ? 'und' : 'and'}{' '} {warning.affectedDataPoints.length - 5}{' '} {language === 'de' ? 'weitere' : 'more'}
  • )}
)}
) })}
) } export default DocumentValidation