'use client'; import { useState, useEffect } from 'react'; import { Box, Typography, Paper, Grid, Chip, Alert, CircularProgress, LinearProgress, Card, CardContent, } from '@mui/material'; import { TrendingUp, TrendingDown, Schedule, Lightbulb, Warning, CheckCircle, } from '@mui/icons-material'; import { motion } from 'framer-motion'; import apiClient from '@/lib/api/client'; interface Pattern { type: string; description: string; confidence: number; trend: 'up' | 'down' | 'stable'; recommendations?: string[]; } interface Insight { category: string; title: string; description: string; severity: 'info' | 'warning' | 'success'; patterns?: Pattern[]; } interface Props { insights?: any; } export default function PatternInsights({ insights: propInsights }: Props) { const [insights, setInsights] = useState([]); const [patterns, setPatterns] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (propInsights) { processInsights(propInsights); setIsLoading(false); } else { fetchPatterns(); } }, [propInsights]); const fetchPatterns = async () => { try { setIsLoading(true); const response = await apiClient.get('/api/v1/insights/patterns'); const data = response.data.data; processInsights(data); } catch (err: any) { console.error('Failed to fetch patterns:', err); setError(err.response?.data?.message || 'Failed to load insights'); } finally { setIsLoading(false); } }; const processInsights = (data: any) => { // Process sleep patterns const sleepInsights: Insight[] = []; if (data?.sleep) { const avgHours = data.sleep.averageHours || 0; if (avgHours < 10) { sleepInsights.push({ category: 'Sleep', title: 'Low Sleep Duration', description: `Average sleep is ${avgHours}h/day. Recommended: 12-16h for infants, 10-13h for toddlers.`, severity: 'warning', }); } else { sleepInsights.push({ category: 'Sleep', title: 'Healthy Sleep Duration', description: `Great! Your child is averaging ${avgHours}h of sleep per day.`, severity: 'success', }); } if (data.sleep.patterns) { sleepInsights[0].patterns = data.sleep.patterns.map((p: any) => ({ type: p.type || 'sleep', description: p.description || 'Sleep pattern detected', confidence: p.confidence || 0.8, trend: p.trend || 'stable', })); } } // Process feeding patterns const feedingInsights: Insight[] = []; if (data?.feeding) { const avgPerDay = data.feeding.averagePerDay || 0; feedingInsights.push({ category: 'Feeding', title: 'Feeding Frequency', description: `Your child is feeding ${avgPerDay} times per day on average.`, severity: 'info', }); if (data.feeding.patterns) { feedingInsights[0].patterns = data.feeding.patterns.map((p: any) => ({ type: p.type || 'feeding', description: p.description || 'Feeding pattern detected', confidence: p.confidence || 0.8, trend: p.trend || 'stable', recommendations: p.recommendations, })); } } // Process diaper patterns const diaperInsights: Insight[] = []; if (data?.diaper) { const avgPerDay = data.diaper.averagePerDay || 0; if (avgPerDay < 5) { diaperInsights.push({ category: 'Diaper', title: 'Low Diaper Changes', description: `Average ${avgPerDay} diaper changes/day. Consider checking hydration if this continues.`, severity: 'warning', }); } else { diaperInsights.push({ category: 'Diaper', title: 'Normal Diaper Activity', description: `Averaging ${avgPerDay} diaper changes per day - within normal range.`, severity: 'success', }); } } setInsights([...sleepInsights, ...feedingInsights, ...diaperInsights]); // Extract all patterns const allPatterns: Pattern[] = []; [...sleepInsights, ...feedingInsights, ...diaperInsights].forEach((insight) => { if (insight.patterns) { allPatterns.push(...insight.patterns); } }); setPatterns(allPatterns); }; const getSeverityIcon = (severity: string) => { switch (severity) { case 'warning': return ; case 'success': return ; default: return ; } }; const getTrendIcon = (trend: string) => { switch (trend) { case 'up': return ; case 'down': return ; default: return ; } }; if (isLoading) { return ( ); } if (error) { return ( {error} ); } return ( Pattern Insights & Recommendations AI-powered insights based on your child's activity patterns {/* Insights Cards */} {insights.map((insight, index) => ( {getSeverityIcon(insight.severity)} {insight.title} {insight.description} {/* Pattern Details */} {insight.patterns && insight.patterns.length > 0 && ( {insight.patterns.map((pattern, pIndex) => ( {getTrendIcon(pattern.trend)} {pattern.description} {pattern.recommendations && pattern.recommendations.length > 0 && ( Recommendations:
    {pattern.recommendations.map((rec, rIndex) => (
  • {rec}
  • ))}
)}
))}
)}
))}
{/* No Insights Message */} {insights.length === 0 && ( Keep tracking activities to see personalized insights and patterns! )}
); }