import { User } from '@/lib/types';
import { Users } from 'lucide-react';

interface TeamDistributionProps {
  users: User[];
}

export function TeamDistribution({ users }: TeamDistributionProps) {
  const teamStats = users.reduce((acc, user) => {
    acc[user.team] = (acc[user.team] || 0) + 1;
    return acc;
  }, {} as Record<string, number>);

  const sortedTeams = Object.entries(teamStats)
    .sort(([, a], [, b]) => b - a)
    .slice(0, 6);

  const maxCount = Math.max(...Object.values(teamStats), 1);

  return (
    <section className="rounded-xl border border-border bg-card h-full">
      <div className="px-5 py-4 border-b border-border">
        <h3 className="text-sm font-semibold tracking-tight">Team distribution</h3>
        <p className="text-xs text-muted-foreground mt-0.5">
          Headcount across teams
        </p>
      </div>
      <div className="p-5 space-y-4">
        {sortedTeams.map(([team, count]) => (
          <div key={team} className="space-y-2">
            <div className="flex items-center justify-between gap-3">
              <span className="text-sm font-medium truncate">{team}</span>
              <span className="flex items-center gap-1.5 text-xs text-muted-foreground tabular-nums">
                <Users className="h-3.5 w-3.5" />
                {count}
              </span>
            </div>
            <div className="h-1.5 w-full rounded-full bg-muted overflow-hidden">
              <div
                className="h-full rounded-full bg-primary"
                style={{ width: `${(count / maxCount) * 100}%` }}
              />
            </div>
          </div>
        ))}
      </div>
      <div className="px-5 py-3 border-t border-border flex items-center justify-between text-xs">
        <span className="text-muted-foreground">Total teams</span>
        <span className="font-semibold tabular-nums">
          {Object.keys(teamStats).length}
        </span>
      </div>
    </section>
  );
}
