'use client'; import { Card, CardContent, Typography, Box, Grid, LinearProgress, Divider, List, ListItem, ListItemText, IconButton, Button, Chip, } from '@mui/material'; import { Restaurant, Hotel, BabyChangingStation, NavigateBefore, NavigateNext, Download, EmojiEvents, Timeline, } from '@mui/icons-material'; import { useState, useEffect } from 'react'; import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; import { MonthlyReport, analyticsApi } from '@/lib/api/analytics'; import { useLocalizedDate } from '@/hooks/useLocalizedDate'; import { format, startOfMonth, addMonths, subMonths } from 'date-fns'; interface MonthlyReportCardProps { childId: string; } export default function MonthlyReportCard({ childId }: MonthlyReportCardProps) { const [report, setReport] = useState(null); const [loading, setLoading] = useState(true); const [currentMonth, setCurrentMonth] = useState(startOfMonth(new Date())); const { format: formatDate } = useLocalizedDate(); useEffect(() => { loadReport(); }, [childId, currentMonth]); const loadReport = async () => { setLoading(true); try { const data = await analyticsApi.getMonthlyReport(childId, currentMonth); setReport(data); } catch (error) { console.error('Failed to load monthly report:', error); } finally { setLoading(false); } }; const handlePreviousMonth = () => { setCurrentMonth(subMonths(currentMonth, 1)); }; const handleNextMonth = () => { setCurrentMonth(addMonths(currentMonth, 1)); }; const handleExport = async (format: 'json' | 'csv' | 'pdf') => { try { const blob = await analyticsApi.exportData( childId, format, report?.month, addMonths(report!.month, 1), ); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `monthly-report-${formatDate(currentMonth, 'yyyy-MM')}.${format}`; link.click(); window.URL.revokeObjectURL(url); } catch (error) { console.error('Failed to export report:', error); } }; if (loading) { return ( Monthly Report ); } if (!report) { return ( Monthly Report No data available for this month ); } // Prepare chart data const chartData = report.weeklyData.map((week, index) => ({ week: `Week ${index + 1}`, Feedings: week.feedings, 'Sleep (hrs)': week.sleepHours, Diapers: week.diapers, })); return ( {/* Header */} Monthly Report {formatDate(report.month, 'MMMM yyyy')} = startOfMonth(new Date())} sx={{ minWidth: 48, minHeight: 48 }}> {/* Summary Cards */} Feedings {report.summary.totalFeedings} {report.summary.averageFeedingsPerDay.toFixed(1)} per day average Sleep {Math.round(report.summary.totalSleepHours)}h {report.summary.averageSleepHoursPerDay.toFixed(1)} hours per day Diapers {report.summary.totalDiapers} {report.summary.averageDiapersPerDay.toFixed(1)} per day average {/* Trends Chart */} Weekly Trends {/* Trends Summary */} {report.trends && report.trends.length > 0 && ( Trends Observed {report.trends.map((trend, index) => ( ))} )} {/* Milestones */} {report.milestones && report.milestones.length > 0 && ( Milestones This Month {report.milestones.map((milestone, index) => ( ))} )} {/* Export Options */} ); }