import { Building2, Users, UsersRound } from 'lucide-react';
import { cn } from '@rutatiina/ui/lib/utils';
import { initials } from '@nugsoft/members/lib/format';
import type { Kind } from '@nugsoft/members/types';

/**
 * The member's photo, or their initials (people) or an icon (groups, institutions).
 */
export function MemberAvatar({
  name,
  kind,
  photoUrl,
  className,
}: {
  name: string;
  kind: Kind;
  photoUrl: string | null;
  className?: string;
}) {
  const Icon =
    kind === 'group' ? UsersRound : kind === 'joint' ? Users : Building2;

  return (
    <span
      className={cn(
        'bg-primary/10 text-primary flex size-9 shrink-0 items-center justify-center overflow-hidden rounded-full text-xs font-semibold',
        className,
      )}
    >
      {photoUrl ? (
        <img
          src={photoUrl}
          alt=""
          loading="lazy"
          className="size-full object-cover"
        />
      ) : kind === 'individual' ? (
        initials(name)
      ) : (
        <Icon className="size-1/2" />
      )}
    </span>
  );
}
