'use client';

import { Card } from '@/components/ui/card';
import { type User } from '@/lib/types';
import { getUserPerformance } from '@/lib/mock-data';
import { 
  LineChart, 
  Line, 
  XAxis, 
  YAxis, 
  CartesianGrid, 
  Tooltip, 
  ResponsiveContainer,
  Legend
} from 'recharts';
import { TrendingUp, Calendar } from 'lucide-react';

interface UserPerformanceChartProps {
  user: User;
}

export function UserPerformanceChart({ user }: UserPerformanceChartProps) {
  const userPerformance = getUserPerformance(user.id);

  if (!userPerformance || userPerformance.monthlyPerformances.length === 0) {
    return (
      <Card className="p-6">
        <div className="flex items-center space-x-2 mb-4">
          <TrendingUp className="h-5 w-5 text-primary" />
          <h3 className="text-lg font-semibold text-foreground">Performance Trends</h3>
        </div>
        <div className="flex items-center justify-center h-64 text-muted-foreground">
          <div className="text-center">
            <TrendingUp className="h-12 w-12 mx-auto mb-2 opacity-30" />
            <p>No performance data available</p>
          </div>
        </div>
      </Card>
    );
  }

  // Prepare chart data
  const chartData = userPerformance.monthlyPerformances.map(perf => ({
    month: `${perf.month} ${perf.year}`,
    overall: perf.weightedAverage,
    codeQuality: perf.codeQuality,
    productivity: perf.productivity,
    collaboration: perf.collaboration,
    teamMorale: perf.teamMorale
  }));

  return (
    <Card className="p-6">
      <div className="flex items-center space-x-2 mb-6">
        <TrendingUp className="h-5 w-5 text-primary" />
        <h3 className="text-lg font-semibold text-foreground">Performance Trends</h3>
      </div>

      <div className="h-80">
        <ResponsiveContainer width="100%" height="100%">
          <LineChart data={chartData} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
            <CartesianGrid strokeDasharray="3 3" stroke="#f0f0f0" />
            <XAxis 
              dataKey="month" 
              stroke="#666"
              fontSize={12}
            />
            <YAxis 
              stroke="#666"
              fontSize={12}
              domain={[0, 100]}
            />
            <Tooltip 
              contentStyle={{
                backgroundColor: 'white',
                border: '1px solid #e5e7eb',
                borderRadius: '8px',
                boxShadow: '0 4px 6px -1px rgba(0, 0, 0, 0.1)'
              }}
              formatter={(value: number, name: string) => [
                `${value}%`,
                name === 'overall' ? 'Overall Average' :
                name === 'codeQuality' ? 'Code Quality' :
                name === 'productivity' ? 'Productivity' :
                name === 'collaboration' ? 'Collaboration' :
                name === 'teamMorale' ? 'Team Morale' : name
              ]}
            />
            <Legend />
            <Line 
              type="monotone" 
              dataKey="overall" 
              stroke="#101828" 
              strokeWidth={3}
              dot={{ fill: '#101828', strokeWidth: 2, r: 4 }}
              name="Overall Average"
            />
            <Line 
              type="monotone" 
              dataKey="codeQuality" 
              stroke="#10b981" 
              strokeWidth={2}
              dot={{ fill: '#10b981', strokeWidth: 2, r: 3 }}
              name="Code Quality"
            />
            <Line 
              type="monotone" 
              dataKey="productivity" 
              stroke="#f59e0b" 
              strokeWidth={2}
              dot={{ fill: '#f59e0b', strokeWidth: 2, r: 3 }}
              name="Productivity"
            />
            <Line 
              type="monotone" 
              dataKey="collaboration" 
              stroke="#101828" 
              strokeWidth={2}
              dot={{ fill: '#101828', strokeWidth: 2, r: 3 }}
              name="Collaboration"
            />
            <Line 
              type="monotone" 
              dataKey="teamMorale" 
              stroke="#5A6473" 
              strokeWidth={2}
              dot={{ fill: '#5A6473', strokeWidth: 2, r: 3 }}
              name="Team Morale"
            />
          </LineChart>
        </ResponsiveContainer>
      </div>

      {/* Summary Stats */}
      <div className="grid grid-cols-2 md:grid-cols-4 gap-4 mt-6 pt-6 border-t border-border">
        <div className="text-center">
          <p className="text-2xl font-bold text-primary">
            {Math.round(
              userPerformance.monthlyPerformances.reduce((sum, p) => sum + p.weightedAverage, 0) / 
              userPerformance.monthlyPerformances.length
            )}%
          </p>
          <p className="text-sm text-muted-foreground">Avg Overall</p>
        </div>
        <div className="text-center">
          <p className="text-2xl font-bold text-green-600">
            {Math.max(...userPerformance.monthlyPerformances.map(p => p.weightedAverage))}%
          </p>
          <p className="text-sm text-muted-foreground">Best Month</p>
        </div>
        <div className="text-center">
          <p className="text-2xl font-bold text-orange-600">
            {userPerformance.monthlyPerformances.length}
          </p>
          <p className="text-sm text-muted-foreground">Months Tracked</p>
        </div>
        <div className="text-center">
          <p className="text-2xl font-bold text-primary">
            {userPerformance.monthlyPerformances[userPerformance.monthlyPerformances.length - 1]?.weightedAverage || 0}%
          </p>
          <p className="text-sm text-muted-foreground">Latest Score</p>
        </div>
      </div>
    </Card>
  );
} 