import type { ReactNode } from 'react';

import { cn } from '@/lib/utils';

/**
 * Lightweight CSS tooltip: dark pill, 12px text, ~120ms delay, shown on hover and keyboard focus.
 * The trigger must carry its own accessible name (aria-label or visible text); the tooltip is decorative.
 */
export function Tooltip({
  label,
  children,
  side = 'top',
  disabled = false,
  className,
}: {
  label: string;
  children: ReactNode;
  side?: 'top' | 'right' | 'bottom';
  disabled?: boolean;
  className?: string;
}) {
  if (disabled) return <>{children}</>;
  return (
    <span className={cn('group/tooltip relative inline-flex', className)}>
      {children}
      <span
        role="presentation"
        aria-hidden="true"
        className={cn(
          'pointer-events-none absolute z-50 whitespace-nowrap rounded-md bg-foreground px-2 py-1 text-caption font-medium text-background opacity-0 shadow-md transition-opacity duration-120 ease-brand',
          'group-hover/tooltip:opacity-100 group-hover/tooltip:delay-[120ms] group-focus-within/tooltip:opacity-100',
          side === 'top' && 'bottom-full left-1/2 mb-2 -translate-x-1/2',
          side === 'bottom' && 'left-1/2 top-full mt-2 -translate-x-1/2',
          side === 'right' && 'left-full top-1/2 ml-3 -translate-y-1/2',
        )}
      >
        {label}
      </span>
    </span>
  );
}
