import React from 'react';
import { Pressable, Text, ActivityIndicator } from 'react-native';
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '@/lib/utils';

const buttonVariants = cva(
  'flex-row items-center justify-center rounded-xl px-5 py-3 active:opacity-80',
  {
    variants: {
      variant: {
        default:     'bg-primary',
        secondary:   'bg-surfaceHigh border border-border',
        destructive: 'bg-destructive',
        ghost:       'bg-transparent',
        outline:     'bg-transparent border border-primary',
      },
      size: {
        sm:   'px-3 py-2',
        md:   'px-5 py-3',
        lg:   'px-6 py-4',
        icon: 'p-2',
      },
    },
    defaultVariants: {
      variant: 'default',
      size:    'md',
    },
  },
);

const textVariants = cva('font-semibold text-center', {
  variants: {
    variant: {
      default:     'text-white',
      secondary:   'text-white',
      destructive: 'text-white',
      ghost:       'text-primary',
      outline:     'text-primary',
    },
    size: {
      sm:   'text-sm',
      md:   'text-base',
      lg:   'text-lg',
      icon: 'text-base',
    },
  },
  defaultVariants: {
    variant: 'default',
    size:    'md',
  },
});

interface ButtonProps extends VariantProps<typeof buttonVariants> {
  onPress?:   () => void;
  disabled?:  boolean;
  loading?:   boolean;
  children:   React.ReactNode;
  className?: string;
}

export function Button({
  variant, size, onPress, disabled, loading, children, className,
}: ButtonProps) {
  return (
    <Pressable
      onPress={onPress}
      disabled={disabled || loading}
      className={cn(buttonVariants({ variant, size }), disabled && 'opacity-50', className)}
    >
      {loading
        ? <ActivityIndicator color="#fff" size="small" />
        : <Text className={textVariants({ variant, size })}>{children}</Text>
      }
    </Pressable>
  );
}
