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

const buttonVariants = cva(
  [
    "inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-none",
    "text-sm font-medium leading-none",
    "transition-[transform,box-shadow] duration-[120ms] [transition-timing-function:steps(2)]",
    "disabled:pointer-events-none disabled:opacity-50",
    "[&_svg]:h-4 [&_svg]:w-4 [&_svg]:shrink-0",
  ].join(" "),
  {
    variants: {
      variant: {
        default: [
          "border-2 border-ink-primary bg-brand-500 text-surface-base",
          "shadow-[4px_4px_0_rgb(var(--ink-primary))]",
          "hover:bg-brand-500 hover:translate-x-[4px] hover:translate-y-[4px] hover:shadow-[0_0_0_rgb(var(--ink-primary))]",
          "active:translate-x-[4px] active:translate-y-[4px] active:shadow-[0_0_0_rgb(var(--ink-primary))]",
        ].join(" "),
        gradient: [
          "bg-gradient-brand text-white shadow-[0_6px_20px_-6px_rgb(43_127_255/0.5)]",
          "hover:shadow-[0_10px_28px_-6px_rgb(43_127_255/0.6)] hover:brightness-[1.06]",
          "active:translate-y-px",
        ].join(" "),
        destructive: [
          "bg-destructive text-destructive-foreground shadow-rest",
          "hover:bg-destructive/90 hover:shadow-raised",
          "active:translate-y-px",
        ].join(" "),
        outline: [
          "border-2 border-ink-primary bg-surface-raised text-ink-primary",
          "shadow-[4px_4px_0_rgb(var(--ink-primary))]",
          "hover:bg-surface-sunken hover:translate-x-[4px] hover:translate-y-[4px] hover:shadow-[0_0_0_rgb(var(--ink-primary))]",
        ].join(" "),
        secondary: [
          "bg-surface-sunken text-ink-primary",
          "hover:bg-ink-primary/[0.06]",
          "active:translate-y-px",
        ].join(" "),
        ghost: [
          "text-ink-secondary",
          "hover:bg-surface-sunken hover:text-ink-primary",
        ].join(" "),
        link: "text-brand-700 underline-offset-4 hover:underline hover:text-brand-600",
      },
      size: {
        default: "h-10 px-4",
        sm: "h-9 px-3.5",
        lg: "h-12 px-6 text-[15px]",
        xl: "h-14 px-7 text-base",
        icon: "h-10 w-10",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
);

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean;
}

const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, ...props }, ref) => {
    const Comp = asChild ? Slot : "button";
    return (
      <Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props} />
    );
  }
);
Button.displayName = "Button";

export { Button, buttonVariants };
