import { cn } from '@/lib/cn';
import { cva, type VariantProps } from 'class-variance-authority';
import { forwardRef, type ButtonHTMLAttributes } from 'react';

const buttonVariants = cva(
    'ms-btn inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-[var(--button-radius)] font-medium transition-all duration-200 active:scale-[0.98] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-60',
    {
        variants: {
            variant: {
                primary:
                    'ms-btn-primary bg-gradient-to-r from-accent to-accent-secondary text-accent-foreground shadow-sm hover:-translate-y-0.5 hover:brightness-110 hover:shadow-accent-lg',
                secondary:
                    'ms-btn-secondary border border-border bg-card text-foreground hover:-translate-y-0.5 hover:border-accent/30 hover:shadow-md',
                ghost:
                    'text-muted-foreground hover:text-foreground hover:bg-muted',
                danger:
                    'bg-destructive text-destructive-foreground shadow-sm hover:-translate-y-0.5 hover:brightness-110',
                dangerGhost:
                    'text-destructive hover:bg-destructive/10',
            },
            size: {
                sm: 'h-9 px-3 text-xs',
                md: 'h-10 px-4 text-sm',
                lg: 'h-12 px-6 text-base',
                icon: 'h-9 w-9',
            },
        },
        defaultVariants: {
            variant: 'primary',
            size: 'md',
        },
    },
);

type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> &
    VariantProps<typeof buttonVariants>;

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
    ({ className, variant, size, type = 'button', ...props }, ref) => (
        <button
            ref={ref}
            type={type}
            className={cn(buttonVariants({ variant, size }), className)}
            {...props}
        />
    ),
);

Button.displayName = 'Button';

export { buttonVariants };
