"use client";

import { Button } from "@/components/ui/button";
import {
  Users,
  TrendingUp,
  BarChart3,
  ExternalLink,
  Target,
  Award,
  Star,
  ChevronRight,
} from "lucide-react";
import { TechIcon } from "@/components/icons/tech-icon";
import {
  COVERAGE_OBJECTIVES,
  isCoverageBelowObjective,
} from "@/lib/evaluation-utils";

interface TeamStatsCardProps {
  teamId: string;
  teamName: string;
  direction: string;
  totalMembers: number;
  averageSkillLevel: number;
  topSkills: Array<{
    skillName: string;
    averageLevel: number;
    color?: string;
    icon?: string;
    importance: "incontournable" | "majeure" | "standard";
    coverage: number;
  }>;
  skillCoverage: number;
  onViewDetails?: () => void;
  onViewReport?: () => void;
}

export function getSkillLevelLabel(level: number): string {
  if (level < 0.5) return "Débutant";
  if (level < 1.5) return "Intermé.";
  if (level < 2.5) return "Avancé";
  return "Expert";
}

export function getSkillLevelColor(level: number): string {
  if (level < 0.5) return "bg-red-500";
  if (level < 1.5) return "bg-orange-500";
  if (level < 2.5) return "bg-blue-500";
  return "bg-green-500";
}

export function getSkillLevelBadgeClasses(level: number): string {
  if (level < 0.5) return "bg-red-500/20 border-red-500/30 text-red-300";
  if (level < 1.5)
    return "bg-orange-500/20 border-orange-500/30 text-orange-300";
  if (level < 2.5) return "bg-blue-500/20 border-blue-500/30 text-blue-300";
  return "bg-green-500/20 border-green-500/30 text-green-300";
}

export function getProgressColor(
  percentage: number,
  importance: "incontournable" | "majeure" | "standard"
): string {
  if (isCoverageBelowObjective(percentage, importance)) return "bg-red-500";
  return "bg-green-500";
}

export function TeamStatsCard({
  teamId,
  teamName,
  direction,
  totalMembers,
  averageSkillLevel,
  topSkills,
  skillCoverage,
  onViewDetails,
  onViewReport,
}: TeamStatsCardProps) {
  return (
    <div className="group bg-white/5 backdrop-blur-sm border border-white/10 rounded-2xl p-5 hover:bg-white/10 hover:border-white/20 transition-all duration-300 cursor-pointer">
      {/* Header avec nom et direction */}
      <div className="flex items-start justify-between mb-5">
        <div className="flex-1 min-w-0">
          <h3 className="text-lg font-bold text-white mb-2 truncate">
            {teamName}
          </h3>
          <div className="flex items-center gap-2">
            <div className="px-2.5 py-1 bg-blue-500/20 border border-blue-500/30 rounded-lg">
              <span className="text-xs font-medium text-blue-300">
                {direction}
              </span>
            </div>
            <div className="px-2.5 py-1 bg-white/10 border border-white/20 rounded-lg">
              <div className="flex items-center gap-1.5">
                <Users className="h-3 w-3 text-slate-400" />
                <span className="text-xs font-medium text-slate-300">
                  {totalMembers} membre{totalMembers > 1 ? "s" : ""}
                </span>
              </div>
            </div>
          </div>
        </div>

        {/* Indicateur de niveau global */}
        <div className="flex flex-col items-end gap-2">
          <div
            className={`px-3 py-1.5 border rounded-xl text-center ${getSkillLevelBadgeClasses(
              averageSkillLevel
            )}`}
          >
            <span className="text-sm font-semibold">
              {getSkillLevelLabel(averageSkillLevel)}
            </span>
          </div>
          <div className="text-right">
            <div className="text-2xl font-bold text-white">
              {((averageSkillLevel / 3) * 100).toFixed(0)}%
            </div>
            <div className="text-xs text-slate-400">maîtrise</div>
          </div>
        </div>
      </div>

      {/* Barre de progression principale */}
      <div className="mb-5">
        <div className="w-full bg-white/10 rounded-full h-2.5 mb-2">
          <div
            className={`h-2.5 rounded-full transition-all duration-500 ${
              averageSkillLevel < 0.5
                ? "bg-gradient-to-r from-red-500 to-red-400"
                : averageSkillLevel < 1.5
                ? "bg-gradient-to-r from-orange-500 to-orange-400"
                : averageSkillLevel < 2.5
                ? "bg-gradient-to-r from-blue-500 to-blue-400"
                : "bg-gradient-to-r from-green-500 to-green-400"
            }`}
            style={{ width: `${(averageSkillLevel / 3) * 100}%` }}
          />
        </div>
      </div>

      {/* Métriques clés en grille */}
      <div className="grid grid-cols-2 gap-4 mb-5">
        <div className="bg-white/5 border border-white/10 rounded-xl p-3">
          <div className="flex items-center gap-2 mb-2">
            <Target className="h-3.5 w-3.5 text-slate-400" />
            <span className="text-xs font-medium text-slate-300">
              Couverture
            </span>
          </div>
          <div className="text-center">
            <div className="text-lg font-bold text-white">
              {skillCoverage.toFixed(0)}%
            </div>
            <div className="text-xs text-slate-400">compétences</div>
          </div>
        </div>

        <div className="bg-white/5 border border-white/10 rounded-xl p-3">
          <div className="flex items-center gap-2 mb-2">
            <BarChart3 className="h-3.5 w-3.5 text-slate-400" />
            <span className="text-xs font-medium text-slate-300">
              Niveau moyen
            </span>
          </div>
          <div className="text-center">
            <div className="text-lg font-bold text-white">
              {averageSkillLevel.toFixed(1)}
            </div>
            <div className="text-xs text-slate-400">/ 3.0</div>
          </div>
        </div>
      </div>

      {/* Top Skills simplifié */}
      <div className="mb-5">
        <div className="flex items-center gap-2 mb-3">
          <div className="p-1 bg-purple-500/20 border border-purple-500/30 rounded-lg">
            <Award className="h-3 w-3 text-purple-400" />
          </div>
          <span className="text-sm font-semibold text-slate-200">
            Top 3 compétences
          </span>
        </div>
        <div className="space-y-2">
          {topSkills.slice(0, 3).map((skill, idx) => (
            <div
              key={idx}
              className="flex items-center gap-3 p-2.5 bg-white/5 border border-white/10 rounded-lg hover:bg-white/10 transition-colors"
            >
              {/* Icône */}
              <div className="flex-shrink-0">
                {skill.icon ? (
                  <div className="p-1.5 bg-white/10 border border-white/20 rounded-md">
                    <TechIcon
                      iconName={skill.icon}
                      className="w-3 h-3 text-white"
                    />
                  </div>
                ) : (
                  <div className="p-1.5 bg-slate-500/20 border border-slate-500/30 rounded-md">
                    <Star className="w-3 h-3 text-slate-400" />
                  </div>
                )}
              </div>

              {/* Nom et niveau */}
              <div className="flex-1 min-w-0">
                <div className="flex items-center justify-between mb-1">
                  <span className="text-sm font-medium text-white truncate pr-2">
                    {skill.skillName}
                  </span>
                  <div className="flex items-center gap-1.5 flex-shrink-0">
                    <span className="text-xs font-semibold text-white">
                      {skill.averageLevel.toFixed(1)}
                    </span>
                    <div
                      className={`w-1.5 h-1.5 rounded-full ${getSkillLevelColor(
                        skill.averageLevel
                      )}`}
                    />
                  </div>
                </div>

                {/* Mini barre de progression */}
                <div className="w-full bg-white/10 rounded-full h-1">
                  <div
                    className={`h-1 rounded-full transition-all duration-300 ${
                      skill.averageLevel < 0.5
                        ? "bg-gradient-to-r from-red-500 to-red-400"
                        : skill.averageLevel < 1.5
                        ? "bg-gradient-to-r from-orange-500 to-orange-400"
                        : skill.averageLevel < 2.5
                        ? "bg-gradient-to-r from-blue-500 to-blue-400"
                        : "bg-gradient-to-r from-green-500 to-green-400"
                    }`}
                    style={{ width: `${(skill.averageLevel / 3) * 100}%` }}
                  />
                </div>
              </div>
            </div>
          ))}
        </div>
      </div>

      {/* Actions simplifiées */}
      <div className="pt-3 border-t border-white/10">
        <Button
          variant="outline"
          size="sm"
          onClick={onViewDetails}
          className="w-full justify-between bg-white/5 border-white/20 text-white hover:bg-white/10 hover:border-white/30 group-hover:bg-white/10 transition-all"
        >
          <span>Voir les détails</span>
          <ChevronRight className="h-3.5 w-3.5 group-hover:translate-x-0.5 transition-transform" />
        </Button>
      </div>
    </div>
  );
}
