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-homenest-sage-light text-homenest-bark',
  pending: 'bg-homenest-cream-dark text-homenest-bark',
  done: 'bg-homenest-sand-light text-homenest-bark',
  cancelled: 'bg-rose-100 text-rose-700',
  paid: 'bg-homenest-sage-light text-homenest-bark',
  unpaid: 'bg-rose-100 text-rose-700',
  info: 'bg-homenest-sand-light text-homenest-bark',
  default: 'bg-homenest-sand-light text-homenest-bark-muted',
  primary: 'bg-homenest-brown text-white',
  warning: 'bg-homenest-cream-dark text-homenest-bark',
  danger: 'bg-rose-100 text-rose-700',
  success: 'bg-homenest-sage-light text-homenest-bark',
};

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;
