'use client';

import { Badge } from '@/components/ui/badge';
import { type User } from '@/lib/types';
import { getUserPerformance } from '@/lib/mock-data';
import { TrendingUp, TrendingDown, Minus } from 'lucide-react';

interface UserPerformanceSummaryProps {
  user: User;
}

export function UserPerformanceSummary({ user }: UserPerformanceSummaryProps) {
  const userPerformance = getUserPerformance(user.id);

  if (!userPerformance || userPerformance.monthlyPerformances.length === 0) {
    return (
      <div className="flex items-center space-x-2 text-muted-foreground">
        <Minus className="h-4 w-4" />
        <span className="text-xs">No performance data</span>
      </div>
    );
  }

  // Calculate overall average from recent performances
  const recentPerformances = userPerformance.monthlyPerformances.slice(-3); // Last 3 months
  const overallAverage = Math.round(
    recentPerformances.reduce((sum, p) => sum + p.weightedAverage, 0) / recentPerformances.length
  );

  // Calculate trend (comparing last month vs previous month)
  const trend = recentPerformances.length >= 2 
    ? recentPerformances[recentPerformances.length - 1].weightedAverage - recentPerformances[recentPerformances.length - 2].weightedAverage
    : 0;

  const getPerformanceColor = (score: number) => {
    if (score >= 90) return 'bg-green-100 text-green-800 border-green-200';
    if (score >= 80) return 'bg-primary/15 text-primary border-primary/20';
    if (score >= 70) return 'bg-yellow-100 text-yellow-800 border-yellow-200';
    return 'bg-red-100 text-red-800 border-red-200';
  };

  const getTrendIcon = (trend: number) => {
    if (trend > 0) return <TrendingUp className="h-3 w-3 text-green-600" />;
    if (trend < 0) return <TrendingDown className="h-3 w-3 text-red-600" />;
    return <Minus className="h-3 w-3 text-muted-foreground" />;
  };

  return (
    <div className="flex items-center space-x-2">
      <Badge 
        variant="outline" 
        className={`${getPerformanceColor(overallAverage)} text-xs font-medium px-2 py-1`}
      >
        {overallAverage}% avg
      </Badge>
      {getTrendIcon(trend)}
      <span className="text-xs text-muted-foreground">
        {recentPerformances.length} months
      </span>
    </div>
  );
} 