import type { FC, ReactNode } from 'react';
import { cn } from '../../lib/utils';

export type BadgeVariant =
  | 'confirmed'
  | 'pending'
  | 'done'
  | 'cancelled'
  | 'paid'
  | 'unpaid'
  | 'info'
  | 'default'
  | 'warning'
  | 'danger'
  | 'success'
  | 'primary';

interface BadgeProps {
  children: ReactNode;
  variant?: BadgeVariant;
  className?: string;
}

const variants: Record<BadgeVariant, string> = {
  confirmed: 'bg-theme-success text-theme-success-text',
  pending: 'bg-theme-accent-bg text-theme-heading',
  done: 'bg-theme-lilac text-theme-heading',
  cancelled: 'bg-rose-100 text-rose-700',
  paid: 'bg-theme-success text-theme-success-text',
  unpaid: 'bg-rose-100 text-rose-700',
  info: 'bg-theme-sky text-theme-heading',
  default: 'bg-theme-accent-bg text-theme-muted',
  primary: 'bg-theme-accent text-theme-inverse',
  warning: 'bg-theme-warning text-theme-warning-text',
  danger: 'bg-rose-100 text-rose-700',
  success: 'bg-theme-success text-theme-success-text',
};

const normalizeVariant = (variant: BadgeVariant): BadgeVariant => {
  if (variant === 'confirmed' || variant === 'paid' || variant === 'success') return variant;
  if (variant === 'pending' || variant === 'warning') return variant;
  if (variant === 'done' || variant === 'info') return variant;
  if (variant === 'cancelled' || variant === 'danger' || variant === 'unpaid') return variant;
  return 'default';
};

export const Badge: FC<BadgeProps> = ({ children, variant = 'default', className }) => {
  const normalized = normalizeVariant(variant);
  return (
    <span
      className={cn(
        'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium',
        variants[normalized],
        className
      )}
    >
      {children}
    </span>
  );
};

export default Badge;
