import { useState } from 'react';
import { Plus, Type, X } from 'lucide-react';
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from '@/components/ui/tooltip';
import { HumanAvatar } from '@/components/human-avatar';
import { REACTION_EMOJIS, type Reactions, type Human } from '@/api/types';
import { cn } from '@/lib/utils';
import { resolveHumanDisplay } from '@/lib/humans';

interface ReactionBarProps {
  reactions: Reactions;
  currentHumanId: string;
  humans?: Human[];
  onToggle: (key: string) => void;
  onOpenTextReaction: () => void;
}

const EMOJI_SET = new Set<string>(REACTION_EMOJIS);

function getReactorNames(humanIds: string[], humans?: Human[]): string {
  return humanIds
    .map((id) => resolveHumanDisplay(id, humans).displayName)
    .join(', ');
}

function getReactorList(
  humanIds: string[],
  humans: Human[] | undefined,
  currentHumanId: string,
): { id: string; label: string; isMine: boolean }[] {
  return humanIds.map((id) => ({
    id,
    label: resolveHumanDisplay(id, humans).displayName,
    isMine: id === currentHumanId,
  }));
}

export function ReactionBar({
  reactions,
  currentHumanId,
  humans,
  onToggle,
  onOpenTextReaction,
}: ReactionBarProps) {
  const [expanded, setExpanded] = useState(false);

  const activeEmojis = REACTION_EMOJIS.filter(
    (emoji) => reactions?.[emoji] && reactions[emoji].length > 0,
  );

  const activeTextReactions = Object.keys(reactions ?? {}).filter(
    (key) => !EMOJI_SET.has(key) && (reactions?.[key]?.length ?? 0) > 0,
  );

  const handleToggle = (key: string) => {
    onToggle(key);
    setExpanded(false);
  };

  return (
    <div className="flex flex-col items-end gap-1.5">
      {/* Emoji reaction pills */}
      {activeEmojis.map((emoji) => {
        const reactors = reactions?.[emoji] ?? [];
        const isMine = reactors.includes(currentHumanId);
        return (
          <Tooltip key={emoji}>
            <TooltipTrigger asChild>
              <button
                onClick={(e) => {
                  e.stopPropagation();
                  handleToggle(emoji);
                }}
                className={cn(
                  'flex items-center gap-1 rounded-full px-2 py-0.5 text-xs backdrop-blur-sm transition-colors',
                  isMine
                    ? 'bg-white/20 ring-1 ring-white/40'
                    : 'bg-black/40 hover:bg-black/50',
                )}
              >
                <span className="text-sm">{emoji}</span>
                <span className="text-white/80">{reactors.length}</span>
              </button>
            </TooltipTrigger>
            <TooltipContent side="left" className="text-xs">
              {getReactorNames(reactors, humans)}
            </TooltipContent>
          </Tooltip>
        );
      })}

      {/* Text reaction pills */}
      {activeTextReactions.map((text) => {
        const reactors = reactions?.[text] ?? [];
        const isMine = reactors.includes(currentHumanId);
        const firstReactor = resolveHumanDisplay(reactors[0], humans);
        const reactorList = getReactorList(reactors, humans, currentHumanId);
        return (
          <Tooltip key={text}>
            <TooltipTrigger asChild>
              <button
                onClick={(e) => {
                  e.stopPropagation();
                  handleToggle(text);
                }}
                className={cn(
                  'flex max-w-[200px] items-center gap-1.5 rounded-full py-0.5 pl-0.5 pr-2.5 text-xs backdrop-blur-sm transition-colors',
                  isMine
                    ? 'bg-white/20 ring-1 ring-white/40'
                    : 'bg-black/40 hover:bg-black/50',
                )}
              >
                <HumanAvatar
                  size="xs"
                  className="shrink-0"
                  avatarObjectId={firstReactor.avatarObjectId}
                  initials={firstReactor.initials}
                  fallbackClassName="bg-white/15 text-[9px] font-medium text-white"
                />
                <span className="truncate text-white/90">{text}</span>
                {reactors.length > 1 && (
                  <span className="shrink-0 text-white/60">
                    {reactors.length}
                  </span>
                )}
              </button>
            </TooltipTrigger>
            <TooltipContent
              side="left"
              className="max-w-[260px] space-y-1.5 text-xs"
            >
              <div className="font-medium">“{text}”</div>
              <ul className="flex flex-col gap-0.5 opacity-80">
                {reactorList.map((r) => (
                  <li
                    key={r.id}
                    className={cn(r.isMine && 'font-medium opacity-100')}
                  >
                    {r.label}
                    {r.isMine && <span className="ml-1 opacity-60">(you)</span>}
                  </li>
                ))}
              </ul>
              <div className="border-t border-current/15 pt-1 text-[10px] opacity-60">
                {isMine ? 'Click to remove' : 'Click to add yours'}
              </div>
            </TooltipContent>
          </Tooltip>
        );
      })}

      {/* Picker / actions */}
      {expanded ? (
        <div className="flex flex-col items-center gap-0.5 rounded-full bg-black/40 px-0.5 py-1.5 backdrop-blur-sm">
          {REACTION_EMOJIS.map((emoji) => {
            if (activeEmojis.includes(emoji)) return null;
            return (
              <button
                key={emoji}
                onClick={(e) => {
                  e.stopPropagation();
                  handleToggle(emoji);
                }}
                className="rounded-full px-0.5 py-1 text-sm transition-colors hover:bg-white/15"
              >
                {emoji}
              </button>
            );
          })}
          <button
            onClick={(e) => {
              e.stopPropagation();
              setExpanded(false);
            }}
            className="flex size-5 items-center justify-center rounded-full transition-colors hover:bg-white/15"
          >
            <X className="size-3 text-white/60" />
          </button>
        </div>
      ) : (
        <div className="flex flex-col items-center gap-1">
          <Tooltip>
            <TooltipTrigger asChild>
              <button
                onClick={(e) => {
                  e.stopPropagation();
                  onOpenTextReaction();
                }}
                className="flex size-6 items-center justify-center rounded-full bg-black/40 backdrop-blur-sm transition-colors hover:bg-black/50"
              >
                <Type className="size-3 text-white/60" />
              </button>
            </TooltipTrigger>
            <TooltipContent side="left" className="text-xs">
              Quick reply{' '}
              <kbd className="ml-1 rounded bg-white/10 px-1 font-mono text-[10px]">
                R
              </kbd>
            </TooltipContent>
          </Tooltip>
          <button
            onClick={(e) => {
              e.stopPropagation();
              setExpanded(true);
            }}
            className="flex size-6 items-center justify-center rounded-full bg-black/40 backdrop-blur-sm transition-colors hover:bg-black/50"
          >
            <Plus className="size-3 text-white/60" />
          </button>
        </div>
      )}
    </div>
  );
}
