import React, { ReactNode, MouseEventHandler, forwardRef } from 'react';
import { Stack } from './primitives/Stack';
import { Icon } from './Icon';
import { Loader2 } from 'lucide-react';
import { ResponsiveValue } from './primitives/Box';

export interface ButtonProps {
  children: ReactNode;
  onClick?: MouseEventHandler<HTMLButtonElement | HTMLAnchorElement>;
  variant?: 'primary' | 'secondary' | 'danger' | 'ghost' | 'success' | 'discord' | 'race-final';
  size?: 'sm' | 'md' | 'lg';
  disabled?: boolean;
  isLoading?: boolean;
  type?: 'button' | 'submit' | 'reset';
  icon?: ReactNode;
  fullWidth?: boolean;
  as?: 'button' | 'a';
  href?: string;
  target?: string;
  rel?: string;
  className?: string;
  style?: React.CSSProperties;
  width?: string | number | ResponsiveValue<string | number>;
  height?: string | number | ResponsiveValue<string | number>;
  minWidth?: string | number;
  px?: number;
  py?: number;
  p?: number;
  rounded?: string;
  bg?: string;
  color?: string;
  fontSize?: string;
  h?: string;
  w?: string;
}

export const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(({ 
  children, 
  onClick, 
  variant = 'primary',
  size = 'md',
  disabled = false,
  isLoading = false,
  type = 'button',
  icon,
  fullWidth = false,
  as = 'button',
  href,
  target,
  rel,
  className,
  style: styleProp,
  width,
  height,
  minWidth,
  px,
  py,
  p,
  rounded,
  bg,
  color,
  fontSize,
  h,
  w,
}, ref) => {
  const baseClasses = 'inline-flex items-center justify-center rounded-none transition-all duration-150 ease-in-out focus-visible:outline focus-visible:outline-1 focus-visible:outline-offset-2 active:opacity-80 uppercase tracking-widest font-bold';
  
  const variantClasses = {
    primary: 'bg-[var(--ui-color-intent-primary)] text-white hover:opacity-90 focus-visible:outline-[var(--ui-color-intent-primary)] shadow-[0_0_15px_rgba(25,140,255,0.3)] hover:shadow-[0_0_25px_rgba(25,140,255,0.5)]',
    secondary: 'bg-[var(--ui-color-bg-surface)] text-white border border-[var(--ui-color-border-default)] hover:bg-[var(--ui-color-border-default)] focus-visible:outline-[var(--ui-color-intent-primary)]',
    danger: 'bg-[var(--ui-color-intent-critical)] text-white hover:opacity-90 focus-visible:outline-[var(--ui-color-intent-critical)]',
    ghost: 'bg-transparent text-[var(--ui-color-text-low)] hover:text-[var(--ui-color-text-high)] hover:bg-white/5 focus-visible:outline-[var(--ui-color-text-low)]',
    success: 'bg-[var(--ui-color-intent-success)] text-[var(--ui-color-bg-base)] hover:opacity-90 focus-visible:outline-[var(--ui-color-intent-success)]',
    'race-final': 'bg-[var(--ui-color-intent-success)] text-[var(--ui-color-bg-base)] hover:opacity-90 focus-visible:outline-[var(--ui-color-intent-success)]',
    discord: 'bg-[#5865F2] text-white hover:bg-[#4752C4] focus-visible:outline-[#5865F2]',
  };
  
  const sizeClasses = {
    sm: 'min-h-[32px] px-3 py-1 text-xs',
    md: 'min-h-[40px] px-4 py-2 text-sm',
    lg: 'min-h-[48px] px-6 py-3 text-base'
  };
  
  const disabledClasses = (disabled || isLoading) ? 'opacity-40 cursor-not-allowed' : 'cursor-pointer';
  const widthClasses = fullWidth ? 'w-full' : '';
  
  const classes = [
    baseClasses,
    variantClasses[variant],
    sizeClasses[size],
    disabledClasses,
    widthClasses,
    className
  ].filter(Boolean).join(' ');
  
  const style: React.CSSProperties = {
    ...(width ? { width: typeof width === 'object' ? undefined : width } : {}),
    ...(height ? { height: typeof height === 'object' ? undefined : height } : {}),
    ...(minWidth ? { minWidth } : {}),
    ...(fontSize ? { fontSize } : {}),
    ...(h ? { height: h } : {}),
    ...(w ? { width: w } : {}),
    ...(styleProp || {})
  };

  const content = (
    <Stack direction="row" align="center" gap={2}>
      {isLoading && <Icon icon={Loader2} size={size === 'sm' ? 3 : 4} animate="spin" />}
      {!isLoading && icon}
      {children}
    </Stack>
  );

  if (as === 'a') {
    return (
      <a
        ref={ref as React.ForwardedRef<HTMLAnchorElement>}
        href={href}
        target={target}
        rel={rel}
        className={classes}
        onClick={onClick as MouseEventHandler<HTMLAnchorElement>}
        style={style}
      >
        {content}
      </a>
    );
  }

  return (
    <button 
      ref={ref as React.ForwardedRef<HTMLButtonElement>}
      type={type}
      className={classes}
      onClick={onClick as MouseEventHandler<HTMLButtonElement>}
      disabled={disabled || isLoading}
      style={style}
    >
      {content}
    </button>
  );
});

Button.displayName = 'Button';
