import { Badge, type BadgeProps } from '@/components/ui/badge'
import { cn } from '@/lib/utils'

const STATUS_STYLES: Record<string, { variant: BadgeProps['variant']; className?: string }> = {
  // Round statuses
  DRAFT: { variant: 'secondary' },
  ACTIVE: { variant: 'default', className: 'bg-blue-500/10 text-blue-700 border-blue-200' },
  EVALUATION: { variant: 'default', className: 'bg-violet-500/10 text-violet-700 border-violet-200' },
  CLOSED: { variant: 'secondary', className: 'bg-slate-500/10 text-slate-600 border-slate-200' },
  ROUND_DRAFT: { variant: 'secondary' },
  ROUND_ACTIVE: { variant: 'default', className: 'bg-blue-500/10 text-blue-700 border-blue-200' },
  ROUND_CLOSED: { variant: 'secondary', className: 'bg-slate-500/10 text-slate-600 border-slate-200' },
  ROUND_ARCHIVED: { variant: 'secondary', className: 'bg-slate-400/10 text-slate-400 border-slate-200' },

  // Project statuses
  SUBMITTED: { variant: 'secondary', className: 'bg-indigo-500/10 text-indigo-700 border-indigo-200' },
  ELIGIBLE: { variant: 'default', className: 'bg-emerald-500/10 text-emerald-700 border-emerald-200' },
  ASSIGNED: { variant: 'default', className: 'bg-violet-500/10 text-violet-700 border-violet-200' },
  UNDER_REVIEW: { variant: 'default', className: 'bg-blue-500/10 text-blue-700 border-blue-200' },
  SHORTLISTED: { variant: 'default', className: 'bg-amber-500/10 text-amber-700 border-amber-200' },
  SEMIFINALIST: { variant: 'default', className: 'bg-amber-500/10 text-amber-700 border-amber-200' },
  FINALIST: { variant: 'default', className: 'bg-orange-500/10 text-orange-700 border-orange-200' },
  WINNER: { variant: 'default', className: 'bg-yellow-500/10 text-yellow-800 border-yellow-300' },
  REJECTED: { variant: 'destructive' },
  WITHDRAWN: { variant: 'secondary' },

  // Observer-derived statuses
  NOT_REVIEWED: { variant: 'secondary', className: 'bg-slate-500/10 text-slate-600 border-slate-200' },
  REVIEWED: { variant: 'default', className: 'bg-emerald-500/10 text-emerald-700 border-emerald-200' },

  // Round state statuses
  PENDING: { variant: 'secondary', className: 'bg-slate-500/10 text-slate-600 border-slate-200' },
  IN_PROGRESS: { variant: 'default', className: 'bg-blue-500/10 text-blue-700 border-blue-200' },
  COMPLETED: { variant: 'default', className: 'bg-emerald-500/10 text-emerald-700 border-emerald-200' },
  PASSED: { variant: 'default', className: 'bg-green-500/10 text-green-700 border-green-200' },

  // User statuses
  NONE: { variant: 'secondary', className: 'bg-slate-500/10 text-slate-500 border-slate-200' },
  INVITED: { variant: 'secondary', className: 'bg-sky-500/10 text-sky-700 border-sky-200' },
  INACTIVE: { variant: 'secondary' },
  SUSPENDED: { variant: 'destructive' },
}

type StatusBadgeProps = {
  status: string
  className?: string
  size?: 'sm' | 'default'
}

export function StatusBadge({ status, className, size = 'default' }: StatusBadgeProps) {
  const style = STATUS_STYLES[status] || { variant: 'secondary' as const }
  const LABEL_OVERRIDES: Record<string, string> = {
    NONE: 'NOT INVITED',
    NOT_REVIEWED: 'Not Reviewed',
    UNDER_REVIEW: 'Under Review',
    REVIEWED: 'Reviewed',
    SEMIFINALIST: 'Semi-finalist',
  }
  const label = LABEL_OVERRIDES[status] ?? status.replace(/_/g, ' ')

  return (
    <Badge
      variant={style.variant}
      className={cn(
        style.className,
        size === 'sm' && 'text-[10px] px-1.5 py-0',
        className,
      )}
    >
      {label}
    </Badge>
  )
}
