import { Avatar, AvatarFallback } from '@/components/ui/avatar';
import { Badge } from '@/components/ui/badge';
import { User, UserRole } from '@/lib/types';
import { USER_ROLES } from '@/lib/constants';
import { calculateEmploymentStatus, getEmploymentStatusBadgeColor } from '@/lib/date-utils';
import { Clock, Code } from 'lucide-react';

interface RecentUsersProps {
  users: User[];
}

const getRoleBadgeColor = (role: UserRole) => {
  const colors: Record<UserRole, string> = {
    SUPER_ADMIN: 'bg-primary text-primary-foreground border-transparent',
    UPPER_MANAGEMENT: 'bg-primary/85 text-primary-foreground border-transparent',
    MANAGER: 'bg-primary/10 text-primary border-primary/20',
    UNIT_LEAD: 'bg-muted text-foreground border-border',
    MEMBER: 'bg-transparent text-muted-foreground border-border',
  };
  return colors[role];
};

export function RecentUsers({ users }: RecentUsersProps) {
  const getInitials = (name: string) =>
    name
      .split(' ')
      .map((n) => n[0])
      .join('')
      .toUpperCase();

  const getTimeAgo = (date: Date) => {
    const now = new Date();
    const diffInDays = Math.floor(
      (now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24)
    );
    if (diffInDays === 0) return 'Today';
    if (diffInDays === 1) return 'Yesterday';
    if (diffInDays < 7) return `${diffInDays}d ago`;
    if (diffInDays < 30) return `${Math.floor(diffInDays / 7)}w ago`;
    return `${Math.floor(diffInDays / 30)}mo ago`;
  };

  return (
    <section className="rounded-xl border border-border bg-card">
      <div className="px-5 py-4 border-b border-border">
        <h3 className="text-sm font-semibold tracking-tight">Recent users</h3>
        <p className="text-xs text-muted-foreground mt-0.5">
          Latest people added to the workspace
        </p>
      </div>
      <div className="divide-y divide-border">
        {users.map((user) => (
          <div
            key={user.id}
            className="flex items-center justify-between gap-3 px-5 py-3.5 hover:bg-muted/40 transition-colors"
          >
            <div className="flex items-center gap-3 min-w-0">
              <Avatar className="h-9 w-9">
                <AvatarFallback className="text-xs font-semibold">
                  {getInitials(user.name)}
                </AvatarFallback>
              </Avatar>
              <div className="min-w-0">
                <p className="text-sm font-medium truncate">{user.name}</p>
                <p className="text-xs text-muted-foreground truncate">
                  {user.designation} · {user.team}
                </p>
                {user.team === 'Production' && user.techStack && (
                  <div className="flex items-center gap-1 mt-1">
                    <Code className="h-3 w-3 text-muted-foreground" />
                    <span className="text-[11px] text-muted-foreground">
                      {user.techStack}
                    </span>
                  </div>
                )}
              </div>
            </div>
            <div className="flex items-center gap-2 shrink-0">
              <Badge
                variant="outline"
                className={`text-[10px] ${getRoleBadgeColor(user.role)}`}
              >
                {USER_ROLES[user.role]}
              </Badge>
              {user.joinDate && (
                <Badge
                  variant="outline"
                  className={`${getEmploymentStatusBadgeColor(calculateEmploymentStatus(user.joinDate).status)} font-medium text-[10px] border hidden sm:inline-flex`}
                >
                  {calculateEmploymentStatus(user.joinDate).label}
                </Badge>
              )}
              <span className="hidden md:flex items-center text-xs text-muted-foreground">
                <Clock className="h-3 w-3 mr-1" />
                {getTimeAgo(user.createdAt)}
              </span>
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}
