import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import * as React from "react"

import { cn } from "@/lib/utils"

const buttonVariants = cva(
  "relative inline-flex cursor-pointer items-center justify-center gap-2 overflow-hidden whitespace-nowrap rounded-md text-sm font-semibold tracking-[0.01em] transition-[color,background-color,box-shadow,transform] disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 active:translate-y-[0.5px] [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0 outline-none focus-visible:ring-ring/40 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground dark:text-black shadow-[var(--shadow-surface)] hover:bg-primary/90 hover:shadow-[var(--shadow-float)]",
        primary: "bg-primary text-primary-foreground dark:text-black shadow-[var(--shadow-surface)] hover:bg-primary/90 hover:shadow-[var(--shadow-float)]",
        secondary: "bg-secondary text-secondary-foreground shadow-[var(--shadow-surface)] hover:bg-secondary/85 hover:shadow-[var(--shadow-float)]",
        info: "bg-info text-info-foreground shadow-[var(--shadow-surface)] hover:bg-info/90 hover:shadow-[var(--shadow-float)]",
        warning: "bg-warning text-warning-foreground shadow-[var(--shadow-surface)] hover:bg-warning/90 hover:shadow-[var(--shadow-float)]",
        danger: "bg-destructive text-destructive-foreground shadow-[var(--shadow-surface)] hover:bg-destructive/90 hover:shadow-[var(--shadow-float)]",
        destructive:
          "bg-destructive text-destructive-foreground shadow-[var(--shadow-surface)] hover:bg-destructive/90 hover:shadow-[var(--shadow-float)] focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40",
        outline:
          "border border-input bg-background text-foreground shadow-[var(--shadow-surface)] hover:bg-accent hover:text-accent-foreground",
        ghost: "text-foreground hover:bg-accent/70 hover:text-accent-foreground",
        link: "cursor-pointer text-primary font-semibold hover:opacity-80",
      },
      size: {
        default: "h-10 px-4 py-2 has-[>svg]:px-3",
        sm: "h-9 rounded-md px-3 has-[>svg]:px-2.5",
        lg: "h-11 rounded-md px-6 has-[>svg]:px-4",
        icon: "size-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
)

function Button({
  className,
  variant,
  size,
  asChild = false,
  ...props
}: React.ComponentProps<"button"> &
  VariantProps<typeof buttonVariants> & {
    asChild?: boolean
  }) {
  const Comp = asChild ? Slot : "button"

  return (
    <Comp
      data-slot="button"
      className={cn(buttonVariants({ variant, size, className }))}
      {...props}
    />
  )
}

export { Button, buttonVariants }
