'use client'; import { Card, CardContent, Typography, Box, Grid, LinearProgress, Chip, Divider, List, ListItem, ListItemText, IconButton, Button, } from '@mui/material'; import { Restaurant, Hotel, BabyChangingStation, TrendingUp, TrendingDown, TrendingFlat, NavigateBefore, NavigateNext, Download, } from '@mui/icons-material'; import { useState, useEffect } from 'react'; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, LineChart, Line } from 'recharts'; import { WeeklyReport, analyticsApi } from '@/lib/api/analytics'; import { useLocalizedDate } from '@/hooks/useLocalizedDate'; import { format, startOfWeek, addWeeks, subWeeks } from 'date-fns'; interface WeeklyReportCardProps { childId: string; } export default function WeeklyReportCard({ childId }: WeeklyReportCardProps) { const [report, setReport] = useState(null); const [loading, setLoading] = useState(true); const [currentWeekStart, setCurrentWeekStart] = useState(startOfWeek(new Date())); const { format: formatDate } = useLocalizedDate(); useEffect(() => { loadReport(); }, [childId, currentWeekStart]); const loadReport = async () => { setLoading(true); try { const data = await analyticsApi.getWeeklyReport(childId, currentWeekStart); setReport(data); } catch (error) { console.error('Failed to load weekly report:', error); } finally { setLoading(false); } }; const handlePreviousWeek = () => { setCurrentWeekStart(subWeeks(currentWeekStart, 1)); }; const handleNextWeek = () => { setCurrentWeekStart(addWeeks(currentWeekStart, 1)); }; const handleExport = async (format: 'json' | 'csv' | 'pdf') => { try { const blob = await analyticsApi.exportData( childId, format, report?.weekStart, report?.weekEnd, ); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `weekly-report-${formatDate(currentWeekStart, 'yyyy-MM-dd')}.${format}`; link.click(); window.URL.revokeObjectURL(url); } catch (error) { console.error('Failed to export report:', error); } }; const getTrendIcon = (trend: 'increasing' | 'stable' | 'decreasing' | 'improving' | 'declining') => { if (trend === 'increasing' || trend === 'improving') { return ; } else if (trend === 'decreasing' || trend === 'declining') { return ; } return ; }; if (loading) { return ( Weekly Report ); } if (!report) { return ( Weekly Report No data available for this week ); } // Prepare chart data const chartData = report.dailyData.map((day) => ({ date: format(day.date, 'EEE'), Feedings: day.feedings, 'Sleep (hrs)': day.sleepHours, Diapers: day.diapers, })); return ( {/* Header */} Weekly Report {formatDate(report.weekStart, 'MMM d')} - {formatDate(report.weekEnd, 'MMM d')} = startOfWeek(new Date())} sx={{ minWidth: 48, minHeight: 48 }}> {/* Summary Cards */} {getTrendIcon(report.trends.feedingTrend)} {report.summary.totalFeedings} Feedings {report.summary.averageFeedingsPerDay.toFixed(1)}/day {getTrendIcon(report.trends.sleepTrend)} {Math.round(report.summary.totalSleepHours)}h Sleep {report.summary.averageSleepHoursPerDay.toFixed(1)}h/day {report.summary.totalDiapers} Diapers {report.summary.averageDiapersPerDay.toFixed(1)}/day {/* Chart */} Daily Breakdown {/* Highlights */} {report.highlights && report.highlights.length > 0 && ( Highlights {report.highlights.map((highlight, index) => ( ))} )} {/* Export Options */} ); }