'use client'; import React, { useState } from 'react'; import { Card, CardContent, CardHeader, Box, Chip, Tabs, Tab, LinearProgress } from '@mui/material'; import { LineChart, Line, Area, AreaChart, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ReferenceLine, } from 'recharts'; import { TrendingUp, TrendingDown, Minus, Calendar, Target, ChartLine, Activity, } from 'lucide-react'; import { TrendAnalysis } from '@/lib/api/analytics'; import { format } from 'date-fns'; interface TrendAnalysisChartProps { data: TrendAnalysis | null; activityType: string; loading?: boolean; error?: Error | null; } export function TrendAnalysisChart({ data, activityType, loading, error }: TrendAnalysisChartProps) { const [selectedTimeframe, setSelectedTimeframe] = useState<'short' | 'medium' | 'long'>('short'); if (loading) { return (
Analyzing trends...
); } if (error) { return ( Error loading trend analysis ); } if (!data) { return null; } const getTrendIcon = (direction: string) => { switch (direction) { case 'improving': return ; case 'declining': return ; default: return ; } }; const getTrendColor = (direction: string) => { switch (direction) { case 'improving': return 'bg-green-100 text-green-800'; case 'declining': return 'bg-red-100 text-red-800'; default: return 'bg-gray-100 text-gray-800'; } }; const getTrendData = () => { switch (selectedTimeframe) { case 'short': return data.shortTermTrend; case 'medium': return data.mediumTermTrend; case 'long': return data.longTermTrend; } }; const currentTrend = getTrendData(); // Prepare chart data for predictions const chartData = data.prediction?.next7Days?.map((point, index) => ({ day: format(point.date, 'MMM dd'), predicted: point.predictedValue, upperBound: point.confidenceInterval?.upper ?? 0, lowerBound: point.confidenceInterval?.lower ?? 0, })) ?? []; const getChipColor = (direction: string) => { switch (direction) { case 'improving': return 'success'; case 'declining': return 'error'; default: return 'default'; } }; return ( {activityType} Trend Analysis {getTrendIcon(currentTrend?.direction ?? 'stable')} {currentTrend?.direction ?? 'No data'} } color={getChipColor(currentTrend?.direction ?? 'stable')} size="small" /> } /> {/* Timeframe Tabs */} setSelectedTimeframe(newValue)} variant="fullWidth" > {/* Trend Metrics */} Change {currentTrend?.changePercent != null ? ( `${currentTrend.changePercent > 0 ? '+' : ''}${currentTrend.changePercent.toFixed(1)}%` ) : 'N/A'} Confidence {currentTrend?.confidence != null ? `${(currentTrend.confidence * 100).toFixed(0)}%` : 'N/A'} {/* Statistical Details */}

Slope

{currentTrend?.slope != null ? currentTrend.slope.toFixed(3) : 'N/A'}

R² Score

{currentTrend?.r2Score != null ? currentTrend.r2Score.toFixed(3) : 'N/A'}

Trend

{currentTrend?.direction ?? 'N/A'}

{/* Prediction Chart */} 7-Day Forecast {chartData.length > 0 ? ( {/* Confidence interval area */} {/* Predicted trend line */} ) : ( No prediction data available )} {/* Prediction Factors */} {data.prediction?.factors && data.prediction.factors.length > 0 && (

Factors Influencing Prediction

{data.prediction.factors.map((factor, index) => ( ))}
)}
{/* Seasonal Patterns */} {data.seasonalPatterns && data.seasonalPatterns.length > 0 && ( Seasonal Patterns Detected {data.seasonalPatterns.map((pattern, index) => (

{pattern.type} Pattern

{pattern.pattern}

Strength

{pattern.strength != null ? (pattern.strength * 100).toFixed(0) : '0'}%

))}
)}
); }