import { useEffect, useState } from 'react' import { Box, Button, CircularProgress, IconButton, TextField, Typography, } from '@mui/material' import { Paperclip, Send, Sparkles, X } from 'lucide-react' import { useSendInquiryReply } from '../../hooks/useInquiries' import { useToastStore } from '../../stores/toastStore' import type { Attachment } from '../../domain/inquiry' import { formatFileSize } from './inquiryUtils' interface InquiryReplyComposerProps { inquiryId: string defaultSubject: string tenantName?: string onSent?: () => void pendingAttachment?: Attachment | null onPendingAttachmentConsumed?: () => void } export function InquiryReplyComposer({ inquiryId, defaultSubject, tenantName, onSent, pendingAttachment, onPendingAttachmentConsumed, }: InquiryReplyComposerProps) { const [subject, setSubject] = useState( defaultSubject.startsWith('Re:') ? defaultSubject : `Re: ${defaultSubject}`, ) const [body, setBody] = useState('') const [attachments, setAttachments] = useState([]) const [isAIDraft, setIsAIDraft] = useState(false) const sendReply = useSendInquiryReply() const showToast = useToastStore(s => s.showToast) useEffect(() => { if (pendingAttachment) { setAttachments(prev => { if (prev.find(a => a.id === pendingAttachment.id)) return prev return [...prev, pendingAttachment] }) onPendingAttachmentConsumed?.() } }, [pendingAttachment, onPendingAttachmentConsumed]) const sending = sendReply.isPending function handleAIDraft() { const salutation = tenantName ? `Sehr geehrte/r ${tenantName}` : 'Sehr geehrte Damen und Herren' setBody( `${salutation},\n\nVielen Dank für Ihre Anfrage. Gerne bestätigen wir, dass das Objekt zum gewünschten Zeitpunkt verfügbar ist.\n\nWir würden Ihnen gerne einen Besichtigungstermin vorschlagen — bitte teilen Sie uns Ihre Verfügbarkeit mit, damit wir einen passenden Termin finden können.\n\nFür Rückfragen stehen wir Ihnen jederzeit gerne zur Verfügung.\n\nMit freundlichen Grüssen` ) setIsAIDraft(true) } const handleAddMockAttachment = () => { const name = `Anhang_${attachments.length + 1}.pdf` setAttachments(prev => [ ...prev, { id: crypto.randomUUID(), fileName: name, fileType: 'application/pdf', fileSize: 240_000 + Math.floor(Math.random() * 800_000), }, ]) } const handleRemoveAttachment = (id: string) => { setAttachments(prev => prev.filter(a => a.id !== id)) } const handleSend = async () => { if (!body.trim()) { showToast('Bitte geben Sie eine Nachricht ein', 'warning') return } const result = await sendReply.mutateAsync({ inquiryId, payload: { subject, body, attachments }, }) if (result.error) { showToast(`Fehler: ${result.error}`, 'error') return } showToast('Antwort gesendet', 'success') setBody('') setAttachments([]) setIsAIDraft(false) onSent?.() } return ( setSubject(e.target.value)} fullWidth /> {isAIDraft && ( KI-Entwurf — bitte prüfen und anpassen )} { setBody(e.target.value); setIsAIDraft(false) }} multiline rows={5} placeholder="Antwort verfassen..." fullWidth sx={isAIDraft ? { '& .MuiOutlinedInput-root': { borderColor: '#ddd6fe' }, '& fieldset': { borderColor: '#ddd6fe !important' } } : {}} /> {attachments.length > 0 && ( {attachments.map(a => ( {a.fileName} {a.fileSize && ( {formatFileSize(a.fileSize)} )} handleRemoveAttachment(a.id)} sx={{ p: 0.25 }}> ))} )} ) }