import { Badge, Spinner, Tooltip } from '@olly/modern-sk';
import { useTranslation } from 'react-i18next';
import type { MetadataStatus } from '../../api/types';

interface Props {
  status: MetadataStatus;
  /** Reason shown in the tooltip for a `failed` status. */
  error?: string;
  /** When true, render nothing for the normal `enriched` state (keeps dense
   * track lists quiet; the upload screen sets this false to confirm success). */
  hideWhenEnriched?: boolean;
}

type Variant = 'lime' | 'ember' | 'neutral' | 'outline';

const VARIANT: Record<MetadataStatus, Variant> = {
  pending: 'neutral',
  enriched: 'lime',
  failed: 'ember',
  manual: 'outline',
};

/**
 * Shows a track's metadata-enrichment state (distinct from file availability).
 * `pending` carries a spinner; `failed` exposes the backend reason on hover.
 */
export function MetadataStatusBadge({
  status,
  error,
  hideWhenEnriched = true,
}: Props) {
  const { t } = useTranslation();
  if (status === 'enriched' && hideWhenEnriched) return null;

  const label = t(`metadata.status.${status}`);
  const tooltip =
    status === 'failed' && error ? error : t(`metadata.statusHint.${status}`);

  return (
    <Tooltip content={tooltip}>
      <Badge variant={VARIANT[status]} dot={status !== 'pending'}>
        {status === 'pending' ? <Spinner size="sm" /> : null}
        {label}
      </Badge>
    </Tooltip>
  );
}
