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

/**
 * Button variant styles using class-variance-authority
 * 
 * Variants:
 * - default: Primary action button with cyan background, subtle hover glow
 * - destructive: Destructive actions (delete, remove) with red styling
 * - outline: Outlined button for secondary actions
 * - secondary: Secondary button with steel background
 * - ghost: Minimal button with hover effect, for tertiary actions
 */
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-kodo-cyan focus-visible:ring-offset-2 focus-visible:ring-offset-kodo-void disabled:pointer-events-none disabled:opacity-50 gap-2 active:scale-95',
  {
    variants: {
      variant: {
        /** Primary action button - use for main CTAs, submit buttons */
        default:
          'bg-kodo-cyan text-kodo-void hover:bg-kodo-cyan-dim hover:shadow-[0_0_15px_rgba(102,252,241,0.3)] border border-transparent font-semibold',
        /** Primary alias for Design System compatibility */
        primary:
          'bg-kodo-cyan text-kodo-void hover:bg-kodo-cyan-dim hover:shadow-[0_0_15px_rgba(102,252,241,0.3)] border border-transparent font-semibold',
        /** Destructive actions - use for delete, remove, clear actions */
        destructive:
          'bg-kodo-red/10 text-kodo-red hover:bg-kodo-red/20 border border-kodo-red/30 hover:border-kodo-red/50',
        /** Outlined button - use for secondary actions, cancel buttons */
        outline:
          'border border-kodo-steel bg-transparent text-white hover:bg-white/5 hover:border-kodo-steel/50',
        /** Secondary button - use for less prominent actions */
        secondary:
          'bg-kodo-steel/30 text-white hover:bg-kodo-steel/50 border border-white/5 hover:border-white/10',
        /** Ghost button - use for icon buttons, menu items, subtle actions */
        ghost: 'hover:bg-white/5 text-white',
        /** Gaming style */
        gaming: 'bg-kodo-slate border border-kodo-gold/40 text-kodo-gold hover:bg-kodo-gold/10 hover:border-kodo-gold font-bold tracking-wider uppercase',
        /** Terminal style */
        terminal: 'bg-kodo-ink border border-kodo-steel text-gray-300 font-mono text-xs hover:border-kodo-cyan hover:text-kodo-cyan',
        /** Nature style */
        nature: 'bg-kodo-slate border border-kodo-lime/30 text-kodo-lime hover:bg-kodo-lime/10',
        /** Glass style */
        glass: 'bg-white/5 backdrop-blur-md border border-white/10 text-white hover:bg-white/10 hover:border-white/20 shadow-lg',
      },
      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;
}

/**
 * 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, children, ...props }, ref) => {
    const Comp = asChild ? Slot : 'button';
    return (
      <Comp
        className={cn(buttonVariants({ variant, size, className }))}
        ref={ref}
        {...props}
      >
        {asChild ? (
          children
        ) : (
          <>
            {icon && (
              <span className="flex items-center justify-center pointer-events-none">
                {icon}
              </span>
            )}
            {children}
          </>
        )}
      </Comp>
    );
  },
);
Button.displayName = 'Button';

export { Button, buttonVariants };
