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

const buttonVariants = cva(
  "inline-flex items-center cursor-pointer justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-white  focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-gray-950 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
  {
    variants: {
      variant: {
        primary: "bg-biru-300 text-slate-50 hover:bg-primary-hover",
        secondary: "bg-secondary text-gray-900 hover:bg-secondary-hover",
        danger: "bg-danger text-white hover:bg-danger-hover",
        dangerOutlined:
          "bg-transparent border border-danger text-danger hover:bg-danger-hover",
        tertiary: "bg-tertiary text-white hover:bg-tertiary-hover",
        muted: "bg-white text-zinc-900 hover:text-primary",
        success: "bg-success text-white hover:bg-success/90",
        destructive: "bg-destructive text-white hover:bg-destructive/90",
        outline:
          "border border-gray-300 bg-white text-gray-700 hover:bg-gray-50 hover:text-gray-900",
        ghost:
          "hover:bg-zinc-100 text-zinc-500 font-normal hover:text-gray-900",
        link: "text-primary underline-offset-4 hover:underline",
      },
      size: {
        xs: "h-7 px-2 text-xs",
        sm: "h-9 px-3",
        default: "h-10 px-6 py-3",
        lg: "h-11 px-8",
        icon: "h-10 w-10",
      },
    },
    defaultVariants: {
      variant: "primary",
      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 };
