'use client'; import React, { useState } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; import { Progress } from '@/components/ui/progress'; 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, lowerBound: point.confidenceInterval.lower, })); return ( {activityType} Trend Analysis {getTrendIcon(currentTrend.direction)} {currentTrend.direction} {/* Timeframe Tabs */} setSelectedTimeframe(v as any)}> Short (7 days) Medium (14 days) Long (30 days) {/* Trend Metrics */}
Change {currentTrend.changePercent > 0 ? '+' : ''}{currentTrend.changePercent.toFixed(1)}%
Confidence {(currentTrend.confidence * 100).toFixed(0)}%
{/* Statistical Details */}

Slope

{currentTrend.slope.toFixed(3)}

R² Score

{currentTrend.r2Score.toFixed(3)}

Trend

{currentTrend.direction}

{/* Prediction Chart */}

7-Day Forecast {(data.prediction.confidence * 100).toFixed(0)}% confidence

{/* Confidence interval area */} {/* Predicted trend line */} {/* Prediction Factors */} {data.prediction.factors.length > 0 && (

Factors Influencing Prediction

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

Seasonal Patterns Detected

{data.seasonalPatterns.map((pattern, index) => (

{pattern.type} Pattern

{pattern.pattern}

Strength

{(pattern.strength * 100).toFixed(0)}%

))}
)}
); }