import { cva, type VariantProps } from 'class-variance-authority';
import { LoaderCircle } from 'lucide-react';
import type { ButtonHTMLAttributes } from 'react';

import { cn } from '@/lib/utils';

export const buttonVariants = cva(
  'inline-flex items-center justify-center gap-2 rounded-md text-body font-semibold transition duration-180 ease-brand disabled:cursor-not-allowed disabled:opacity-60 active:scale-[0.98]',
  {
    variants: {
      variant: {
        primary:
          'bg-brand-gradient text-white shadow-sm [box-shadow:inset_0_1px_0_rgb(255_255_255_/_0.22)] hover:brightness-[0.97] hover:-translate-y-px',
        secondary: 'border border-border bg-surface text-foreground hover:bg-surface-muted',
        ghost: 'text-foreground hover:bg-surface-muted',
        outline: 'border border-primary-foreground/40 bg-transparent text-primary-foreground hover:bg-primary-foreground/10',
        white: 'bg-surface text-primary shadow-sm hover:bg-surface-muted',
        destructive: 'bg-danger text-white hover:opacity-90',
      },
      size: {
        sm: 'h-9 min-h-9 px-3 text-small',
        md: 'h-10 min-h-10 px-4',
        lg: 'h-12 min-h-12 px-5 text-input',
      },
    },
    defaultVariants: { variant: 'primary', size: 'md' },
  },
);

export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement>, VariantProps<typeof buttonVariants> {
  loading?: boolean;
}

export function Button({ loading = false, className, children, disabled, variant, size, ...props }: ButtonProps) {
  return (
    <button className={cn(buttonVariants({ variant, size }), className)} disabled={disabled === true || loading} {...props}>
      {loading ? <LoaderCircle className="h-4 w-4 animate-spin" aria-hidden="true" /> : null}
      {children}
    </button>
  );
}
