import { type ReactElement } from "react";
import * as React from "react";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/utils";
import { Slot } from "@/lib/slot";

/**
 * Badge — shadcn/WealthX
 * Base: npx shadcn\@latest add badge (latest)
 * Figma: Design-System---shadcn?node-id=665-2024
 *
 * WealthX: added font-sans. Extended with success/warning/info semantic variants.
 * Semantic variants use a tinted outline style: bg-X/10 + border-X/40 + text-X-text.
 * This gives softer, more readable badges and works correctly in light and dark mode.
 */
const badgeVariants = cva(
  "inline-flex w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-full border border-transparent px-2.5 py-0.5 text-caption whitespace-nowrap transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>svg]:pointer-events-none [&>svg]:size-3",
  {
    variants: {
      variant: {
        default:
          "border-primary/40 bg-primary/10 text-foreground [a&]:hover:bg-primary/15",
        secondary:
          "border-border bg-muted text-muted-foreground [a&]:hover:bg-muted/80",
        destructive:
          "border-destructive/40 bg-destructive/10 text-destructive-text focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 [a&]:hover:bg-destructive/15",
        success:
          "border-success/40 bg-success/10 text-success-text [a&]:hover:bg-success/15",
        warning:
          "border-warning/40 bg-warning/10 text-warning-text [a&]:hover:bg-warning/15",
        info: "border-info/40 bg-info/10 text-info-text [a&]:hover:bg-info/15",
        outline:
          "border-border text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
        ghost: "[a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
        link: "text-primary underline-offset-4 [a&]:hover:underline",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  },
);

export type BadgeProps = React.ComponentProps<"span"> &
  VariantProps<typeof badgeVariants> & { asChild?: boolean };

function Badge({
  className,
  variant = "default",
  asChild = false,
  ...props
}: BadgeProps): ReactElement {
  const Comp = asChild ? Slot : "span";

  return (
    <Comp
      className={cn(badgeVariants({ variant }), className)}
      data-slot="badge"
      data-variant={variant}
      {...props}
    />
  );
}

export { Badge, badgeVariants };
