"use client"

import type * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
import { useSemanticColors } from "@/contexts/semantic-color-context"
import type { SemanticColorName } from "@/utils/semantic-colors"

const badgeVariants = cva(
  "inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
  {
    variants: {
      variant: {
        success: "",
        warning: "",
        error: "",
        info: "",
        neutral: "",
      },
      size: {
        sm: "px-2 py-0.5 text-xs",
        md: "px-2.5 py-0.5 text-xs",
        lg: "px-3 py-1 text-sm",
      },
    },
    defaultVariants: {
      variant: "neutral",
      size: "md",
    },
  },
)

export interface SemanticBadgeProps extends React.HTMLAttributes<HTMLDivElement>, VariantProps<typeof badgeVariants> {
  variant?: SemanticColorName
}

function SemanticBadge({ className, variant = "neutral", size, style, ...props }: SemanticBadgeProps) {
  const { getSemanticColorVar } = useSemanticColors()

  const semanticStyle = {
    backgroundColor: getSemanticColorVar(variant, "bg"),
    color: getSemanticColorVar(variant, "text"),
    borderColor: getSemanticColorVar(variant, "border"),
    ...style,
  }

  return <div className={cn(badgeVariants({ variant, size }), className)} style={semanticStyle} {...props} />
}

export { SemanticBadge, badgeVariants }
