'use client' import { useCallback, useState } from 'react' import type { AdvisorCase } from './useAdvisorCase' function esc(s: string): string { return s .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') } function evidenceHtml(c: AdvisorCase): string { const ev = c.response?.evidence ?? [] if (ev.length === 0) return '' const items = ev .map( (e) => `
  • ${esc(e.document)}${e.section ? ` — ${esc(e.section)}` : ''}${e.paragraph ? ` ${esc(e.paragraph)}` : ''}
  • `, ) .join('') return `

    Evidence:

    ` } /** Sends the consultation cases (question + answer + evidence) as an email to the DSB. */ export function useAdvisorEmail(cases: AdvisorCase[], country: string, currentStep: string) { const [sending, setSending] = useState(false) const [sent, setSent] = useState(false) const send = useCallback(async () => { if (cases.length === 0 || sending) return setSending(true) try { const qaHtml = cases .map((c) => { const a = c.response?.answer || c.response?.general_answer || '(keine Antwort)' return `

    Frage: ${esc( c.question, )}

    ${esc(a)}

    ${evidenceHtml(c)}
    ` }) .join('') const bodyHtml = `

    Compliance Advisor — Beratungsprotokoll

    Datum: ${esc(new Date().toLocaleString('de-DE'))} | Land: ${esc(country)} | Kontext: ${esc(currentStep)}


    ${qaHtml}

    Automatisch erstellt vom BreakPilot Compliance Advisor

    ` await fetch('/api/sdk/v1/agent/notify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ recipient: 'dsb@breakpilot.local', subject: `Compliance Advisor — ${cases.length} Fragen (${currentStep})`, body_html: bodyHtml, role: 'Datenschutzbeauftragter', }), }) setSent(true) setTimeout(() => setSent(false), 3000) } catch (e) { console.error('Email send failed:', e) } finally { setSending(false) } }, [cases, sending, country, currentStep]) return { send, sending, sent } }