'use client' import { useState, useMemo } from 'react' import { format } from 'date-fns' import { X, FileText, Download, Share2 } from 'lucide-react' import { Button, showToast } from '@/components/ui' interface Symptom { id: string type: string customName?: string severity: number notes?: string recordedAt: string durationMinutes?: number triggers?: string medicationTaken?: string reliefNotes?: string } interface SymptomReportGeneratorProps { symptoms: Symptom[] workspaceId: string timeRange: number onClose: () => void } const SYMPTOM_LABELS: Record = { FATIGUE: 'Fatigue', NAUSEA: 'Nausea', PAIN: 'Pain', APPETITE: 'Appetite Loss', SLEEP: 'Sleep Issues', MOOD: 'Mood Changes', CUSTOM: 'Other', } export function SymptomReportGenerator({ symptoms, workspaceId, timeRange, onClose }: SymptomReportGeneratorProps) { const [generating, setGenerating] = useState(false) const reportData = useMemo(() => { // Group by type const byType: Record = {} symptoms.forEach(s => { if (!byType[s.type]) byType[s.type] = [] byType[s.type].push(s) }) // Calculate averages const averages = Object.entries(byType).map(([type, items]) => ({ type, label: SYMPTOM_LABELS[type] || type, count: items.length, avgSeverity: (items.reduce((sum, s) => sum + s.severity, 0) / items.length).toFixed(1), maxSeverity: Math.max(...items.map(s => s.severity)), })) // Sort by frequency averages.sort((a, b) => b.count - a.count) // Daily breakdown const byDay: Record = {} symptoms.forEach(s => { const day = format(new Date(s.recordedAt), 'yyyy-MM-dd') if (!byDay[day]) byDay[day] = [] byDay[day].push(s) }) return { byType, averages, byDay } }, [symptoms]) const generatePDF = async () => { setGenerating(true) try { const response = await fetch(`/api/workspaces/${workspaceId}/symptoms/report`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ symptoms, timeRange, generatedAt: new Date().toISOString() }) }) if (!response.ok) throw new Error('Failed to generate report') const blob = await response.blob() const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `symptom-report-${format(new Date(), 'yyyy-MM-dd')}.pdf` document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) showToast('Report downloaded', 'success') } catch { showToast('Failed to generate report', 'error') } finally { setGenerating(false) } } const copySummary = () => { const summary = ` Symptom Summary (Last ${timeRange} Days) Generated: ${format(new Date(), 'MMM d, yyyy')} Total Symptoms Logged: ${symptoms.length} By Symptom Type: ${reportData.averages.map(a => `- ${a.label}: ${a.count} times (avg severity: ${a.avgSeverity}/5)` ).join('\n')} Most Common: ${reportData.averages[0]?.label || 'N/A'} Average Severity: ${(symptoms.reduce((s, x) => s + x.severity, 0) / symptoms.length || 0).toFixed(1)}/5 `.trim() navigator.clipboard.writeText(summary) showToast('Summary copied to clipboard', 'success') } return (
{/* Header */}

Symptom Report

{/* Content */}
{/* Summary */}

Summary

{symptoms.length}

Total Logged

{(symptoms.reduce((s, x) => s + x.severity, 0) / symptoms.length || 0).toFixed(1)}

Avg Severity

{/* Breakdown */}

By Symptom Type

{reportData.averages.map(item => (

{item.label}

{item.count} occurrences

{item.avgSeverity}/5

avg

))}
{/* Notes */}

Share this report with your doctor at your next appointment

{/* Actions */}
) }