export const AVATARS = [
  { emoji: "🐱", bg: "bg-amber-100" },
  { emoji: "🐶", bg: "bg-orange-100" },
  { emoji: "🦊", bg: "bg-red-100" },
  { emoji: "🐰", bg: "bg-pink-100" },
  { emoji: "🐼", bg: "bg-zinc-100" },
  { emoji: "🐨", bg: "bg-sky-100" },
  { emoji: "🦁", bg: "bg-yellow-100" },
  { emoji: "🐸", bg: "bg-lime-100" },
  { emoji: "🐵", bg: "bg-stone-100" },
  { emoji: "🐷", bg: "bg-rose-100" },
  { emoji: "🐙", bg: "bg-purple-100" },
  { emoji: "🦄", bg: "bg-violet-100" },
] as const;

export function getAvatar(uid: string) {
  let hash = 0;
  for (let i = 0; i < uid.length; i++) {
    hash = (hash * 31 + uid.charCodeAt(i)) | 0;
  }
  return AVATARS[((hash % AVATARS.length) + AVATARS.length) % AVATARS.length];
}

export function getAvatarBg(emoji: string): string {
  const found = AVATARS.find((a) => a.emoji === emoji);
  return found?.bg ?? "bg-zinc-100";
}

export function resolveAvatar(
  userId: string,
  profile?: { avatar: string } | null,
): { emoji: string; bg: string } {
  if (profile) {
    return { emoji: profile.avatar, bg: getAvatarBg(profile.avatar) };
  }
  const fallback = getAvatar(userId);
  return { emoji: fallback.emoji, bg: fallback.bg };
}
