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-xl text-sm font-medium transition-all duration-200 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 active:scale-[0.98] hover-lift',
  {
    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 tracking-tight',
        /** 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-kodo-secondary hover:bg-white/5 hover:text-white hover:border-kodo-cyan/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 hover:text-white text-kodo-secondary',
      },
      size: {
        /** Default size - standard buttons */
        default: 'h-10 px-4 py-2',
        /** Small size - compact buttons, inline actions */
        sm: 'h-8 rounded-lg px-3 text-xs',
        /** Large size - prominent CTAs */
        lg: 'h-12 rounded-xl px-8 text-base',
        /** Icon size - icon-only buttons (square) */
        icon: 'h-10 w-10',
      },
    },
    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;
}

/**
 * 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, ...props }, ref) => {
    const Comp = asChild ? Slot : 'button';
    return (
      <Comp
        className={cn(buttonVariants({ variant, size, className }))}
        ref={ref}
        {...props}
      />
    );
  },
);
Button.displayName = 'Button';

export { Button, buttonVariants };
