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

/*
 * Los tamaños arrancan en 44px de alto (el mínimo táctil de Apple) porque el
 * sistema se usa en celular y tablet dentro del taller. `sm` existe sólo para
 * acciones secundarias dentro de una fila en pantallas grandes.
 */
const buttonVariants = cva(
    'inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-md text-sm font-semibold transition-colors duration-150 ease-out focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:pointer-events-none disabled:opacity-40 [&_svg]:size-4 [&_svg]:shrink-0',
    {
        variants: {
            variant: {
                default:
                    'bg-primary text-primary-foreground shadow-sm hover:bg-primary/90 active:bg-primary/95',
                destructive:
                    'bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90',
                outline:
                    'border border-border-strong bg-surface text-foreground shadow-sm hover:bg-muted',
                secondary:
                    'bg-secondary text-secondary-foreground hover:bg-secondary/70',
                ghost: 'text-foreground hover:bg-muted',
                link: 'text-primary underline-offset-4 hover:underline',
            },
            size: {
                default: 'h-11 px-4 py-2',
                sm: 'h-9 rounded px-3 text-sm',
                lg: 'h-12 px-6 text-base',
                xl: 'h-14 rounded-lg px-8 text-base',
                icon: 'h-11 w-11',
                'icon-sm': 'h-9 w-9 rounded',
            },
        },
        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 };
