import { ROLE_NAME } from '@/types/enum';
import { m } from '@paraglide/messages';
import { Badge, badgeVariants } from '@ui/badge';
import { VariantProps } from 'class-variance-authority';

type BadgeVariant = VariantProps<typeof badgeVariants>['variant'];

type RoleProps = {
  type?: string | null; // type can be any string, undefined, or null
  className?: string;
};

const RoleBadge = ({ type, className }: RoleProps) => {
  // List all valid badge variant keys
  const validBadgeVariants: BadgeVariant[] = [
    'admin',
    'user',
    'member',
    'owner',
  ];

  // Determine the actual variant to apply.
  // If 'type' is a valid variant key, use it. Otherwise, fallback to 'default'.
  const displayVariant: BadgeVariant =
    type && validBadgeVariants.includes(type as BadgeVariant)
      ? (type as BadgeVariant)
      : 'user';

  return (
    <Badge variant={displayVariant} className={className}>
      {m.role_tags({ role: type as ROLE_NAME })}
    </Badge>
  );
};

export default RoleBadge;
