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-2xl text-sm font-semibold transition-all duration-200 disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring)] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--background)]",
  {
    variants: {
      variant: {
        default:
          "bg-[var(--primary)] px-4 py-2.5 text-[var(--primary-foreground)] shadow-[0_14px_30px_-18px_rgba(125,74,39,0.65)] hover:-translate-y-0.5 hover:bg-[var(--primary-strong)]",
        secondary:
          "bg-[var(--muted)] px-4 py-2.5 text-[var(--foreground)] hover:bg-[var(--muted-strong)]",
        outline:
          "border border-[var(--border)] bg-white/80 px-4 py-2.5 text-[var(--foreground)] hover:bg-[var(--paper)]",
        ghost:
          "px-3 py-2 text-[var(--foreground-muted)] hover:bg-[var(--muted)] hover:text-[var(--foreground)]",
        destructive:
          "bg-[var(--destructive)] px-4 py-2.5 text-white shadow-[0_10px_24px_-18px_rgba(185,28,28,0.8)] hover:bg-[var(--destructive-strong)]",
      },
      size: {
        default: "h-11",
        sm: "h-9 rounded-xl px-3",
        lg: "h-12 rounded-2xl px-6 text-base",
        icon: "h-10 w-10 rounded-2xl",
      },
    },
    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 };
