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

type StatusVariant = 'success' | 'warning' | 'info' | 'pending' | 'destructive' | 'default' | 'secondary';

interface StatusBadgeProps {
  status: string;
  variant?: StatusVariant;
  className?: string;
}

const statusConfig: Record<string, { variant: StatusVariant; label: string }> = {
  // Common status
  draft: { variant: 'default', label: 'Rascunho' },
  active: { variant: 'success', label: 'Ativo' },
  inactive: { variant: 'default', label: 'Inativo' },

  // Agent & Payroll status
  suspended: { variant: 'warning', label: 'Suspenso' },
  retired: { variant: 'info', label: 'Aposentado' },
  terminated: { variant: 'destructive', label: 'Cessado' },
  processing: { variant: 'pending', label: 'Em Processamento' },
  approved: { variant: 'info', label: 'Aprovado' },
  paid: { variant: 'success', label: 'Pago' },
  cancelled: { variant: 'destructive', label: 'Cancelado' },

  // Payment status
  pending: { variant: 'pending', label: 'Pendente' },
  sent: { variant: 'info', label: 'Enviado' },
  completed: { variant: 'success', label: 'Concluído' },
  rejected: { variant: 'destructive', label: 'Rejeitado' },
  created: { variant: 'default', label: 'Criado' },
  sent_to_bank: { variant: 'info', label: 'Enviado ao Banco' },
  confirmed: { variant: 'success', label: 'Confirmado' },

  // Budget status
  open: { variant: 'success', label: 'Aberto' },
  planning: { variant: 'pending', label: 'Planeamento' },
  closed: { variant: 'default', label: 'Encerrado' },
  frozen: { variant: 'warning', label: 'Congelado' },

  // Contract status
  'fixed-term': { variant: 'info', label: 'Prazo Determinado' },
  permanent: { variant: 'success', label: 'Efectivo' },
  temporary: { variant: 'pending', label: 'Temporário' },
  internship: { variant: 'info', label: 'Estágio' },
  expired: { variant: 'warning', label: 'Expirado' },

  // Treasury status
  cash: { variant: 'default', label: 'Caixa' },
  bank_account: { variant: 'secondary', label: 'Conta Bancária' },
};

const variantStyles: Record<StatusVariant, string> = {
  success: 'gov-badge-success',
  warning: 'gov-badge-warning',
  info: 'gov-badge-info',
  pending: 'gov-badge-pending',
  destructive: 'gov-badge-destructive',
  default: 'bg-muted text-muted-foreground',
  secondary: 'bg-secondary text-secondary-foreground hover:bg-secondary/80',
};

export function StatusBadge({ status, variant, className }: StatusBadgeProps) {
  const config = statusConfig[status.toLowerCase()];
  const finalVariant = variant || config?.variant || 'default';
  const label = config?.label || status;

  return (
    <span
      className={cn(
        'gov-badge',
        variantStyles[finalVariant],
        className
      )}
    >
      {label}
    </span>
  );
}
