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

/**
 * Button variant styles using class-variance-authority
 * SaaS Premium: semantic tokens, glass/glow, radius-full, duration-normal, active:scale-[0.98]
 */
const buttonVariants = cva(
  'inline-flex items-center justify-center whitespace-nowrap rounded-full text-sm font-medium tracking-tight transition-[color,transform,box-shadow,border-color,background-color] duration-[var(--duration-normal)] 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-50 gap-2 active:scale-[0.98]',
  {
    variants: {
      variant: {
        /** Primary action button - main CTAs, submit */
        default:
          'bg-primary text-primary-foreground hover:bg-primary/90 hover:shadow-button-primary-glow border border-transparent font-semibold',
        /** Primary alias for Design System compatibility */
        primary:
          'bg-primary text-primary-foreground hover:bg-primary/90 hover:shadow-button-primary-glow border border-transparent font-semibold',
        /** Destructive actions - delete, remove, clear */
        destructive:
          'bg-destructive/10 text-destructive hover:bg-destructive/20 border border-destructive/30 hover:border-destructive/50',
        /** Outlined - secondary actions, cancel */
        outline:
          'border border-border bg-transparent text-foreground hover:bg-muted/50 hover:border-border',
        /** Secondary - less prominent (Spotify/Discord style) */
        secondary:
          'bg-muted/30 text-foreground hover:bg-muted/50 border border-border hover:border-border',
        /** Ghost - icon buttons, menu items */
        ghost: 'text-muted-foreground hover:text-foreground hover:bg-muted/50',
        /** Gaming style - accent warning/gold */
        gaming:
          'bg-muted border border-warning/40 text-warning hover:bg-warning/10 hover:border-warning font-bold tracking-wider uppercase',
        /** Terminal style - monospace, primary accent on hover */
        terminal:
          'bg-card border border-border text-muted-foreground font-mono text-xs hover:border-primary hover:text-primary',
        /** Nature style - success green */
        nature:
          'bg-muted border border-success/30 text-success hover:bg-success/10 hover:border-success/50',
        /** Glass - glassmorphism (Spotify/Discord) */
        glass:
          'bg-muted/30 backdrop-blur-md border border-border text-foreground hover:bg-muted/50 hover:border-border',
      },
      size: {
        /** Default size - standard buttons */
        default: 'h-10 px-4 py-2',
        /** Small size - compact buttons, inline actions */
        sm: 'h-9 rounded-full px-4 text-xs',
        /** Large size - prominent CTAs */
        lg: 'h-12 rounded-full px-8 text-base',
        /** Icon size - icon-only buttons (square, full radius) */
        icon: 'h-10 w-10 rounded-full',
      },
    },
    defaultVariants: {
      variant: 'default',
      size: 'default',
    },
  },
);

/**
 * Button component props
 * 
 * Extends standard HTML button attributes with design system variants and sizes.
 * 
 * @example
 * ```tsx
 * <Button variant="default" size="lg" onClick={handleSave}>
 *   Save Changes
 * </Button>
 * ```
 * 
 * @example
 * ```tsx
 * <Button variant="ghost" size="icon" onClick={handleEdit}>
 *   <Edit className="w-4 h-4" />
 * </Button>
 * ```
 */
export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
  VariantProps<typeof buttonVariants> {
  /** Use asChild to compose with other components (e.g., Link from react-router) */
  asChild?: boolean;
  /** Optional icon to display before the label */
  icon?: React.ReactNode;
  /** Show a loading spinner and disable the button */
  loading?: boolean;
}

/**
 * Button - Design system button component
 * 
 * A versatile button component with multiple variants and sizes following the Kodo design system.
 * 
 * @example
 * ```tsx
 * // Primary action
 * <Button variant="default" onClick={handleSave}>Save</Button>
 * 
 * // Destructive action
 * <Button variant="destructive" onClick={handleDelete}>Delete</Button>
 * 
 * // Secondary action
 * <Button variant="outline" onClick={handleCancel}>Cancel</Button>
 * 
 * // Icon button
 * <Button variant="ghost" size="icon">
 *   <Edit className="w-4 h-4" />
 * </Button>
 * ```
 */
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, icon, loading = false, children, disabled, ...props }, ref) => {
    const Comp = asChild ? Slot : 'button';
    const isDisabled = disabled || loading;
    return (
      <Comp
        className={cn(buttonVariants({ variant, size, className }), loading && 'opacity-70')}
        ref={ref}
        disabled={isDisabled}
        {...props}
      >
        {asChild ? (
          children
        ) : (
          <>
            {loading && (
              <Loader2 className="mr-2 h-4 w-4 animate-spin" />
            )}
            {!loading && icon && (
              <span className="flex items-center justify-center pointer-events-none">
                {icon}
              </span>
            )}
            {children}
          </>
        )}
      </Comp>
    );
  },
);
Button.displayName = 'Button';

export { Button, buttonVariants };
