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(
  'smx-inline-flex smx-items-center smx-justify-center smx-whitespace-nowrap smx-rounded-md smx-text-sm smx-font-medium smx-ring-offset-background smx-transition-all focus-visible:smx-outline-none focus-visible:smx-ring-2 focus-visible:smx-ring-ring focus-visible:smx-ring-offset-2 disabled:smx-pointer-events-none disabled:smx-opacity-50',
  {
    variants: {
      variant: {
        default: 'smx-bg-primary smx-text-primary-foreground hover:smx-bg-primary/90 hover:smx-scale-[1.02]',
        destructive: 'smx-bg-destructive smx-text-destructive-foreground hover:smx-bg-destructive/90',
        outline: 'smx-border smx-border-input smx-bg-background hover:smx-bg-accent hover:smx-text-accent-foreground',
        secondary: 'smx-bg-secondary smx-text-secondary-foreground hover:smx-bg-secondary/80',
        ghost: 'hover:smx-bg-accent hover:smx-text-accent-foreground',
        link: 'smx-text-primary smx-underline-offset-4 hover:smx-underline',
      },
      size: {
        default: 'smx-h-10 smx-px-4 smx-py-2',
        sm: 'smx-h-9 smx-rounded-md smx-px-3',
        lg: 'smx-h-11 smx-rounded-md smx-px-8',
        icon: 'smx-h-10 smx-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 };
