"use client";

import * as React from "react";
import { Circle } from "lucide-react";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "../../lib/utils";

/**
 * Radio Group Component
 *
 * Accessible, customizable, and fully integrated with the theme system radio button group.
 * Allows users to select a single option from a group of choices.
 */

const MoonUIradioGroupItemVariantsPro = cva(
  "aspect-square border focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
  {
    variants: {
      variant: {
        default: "border-border bg-background text-primary",
        outline: "border-border bg-transparent text-primary",
        filled: "border-primary bg-primary/10 text-primary",
      },
      size: {
        sm: "h-3.5 w-3.5",
        default: "h-4 w-4",
        md: "h-5 w-5",
        lg: "h-6 w-6",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
    },
  }
);

export interface RadioGroupProps extends React.HTMLAttributes<HTMLDivElement> {
  /**
   * Radio group value
   */
  value?: string;
  /**
   * Function to call when radio group value changes
   */
  onValueChange?: (value: string) => void;
  /**
   * Radio group disabled state
   */
  disabled?: boolean;
  /**
   * Radio group name
   */
  name?: string;
}

const MoonUIRadioGroupContextPro = React.createContext<{
  value?: string;
  onValueChange?: (value: string) => void;
  disabled?: boolean;
  name?: string;
}>({});

const MoonUIRadioGroupPro = React.forwardRef<HTMLDivElement, RadioGroupProps>(
  ({ className, value, onValueChange, disabled, name, ...props }, ref) => {
    return (
      <MoonUIRadioGroupContextPro.Provider value={{ value, onValueChange, disabled, name }}>
        <div
          ref={ref}
          role="radiogroup"
          className={cn("grid gap-2", className)}
          {...props}
        />
      </MoonUIRadioGroupContextPro.Provider>
    );
  }
);
MoonUIRadioGroupPro.displayName = "RadioGroupPro";

export interface RadioGroupItemProps
  extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'>,
    VariantProps<typeof MoonUIradioGroupItemVariantsPro> {
  /**
   * Custom indicator component
   */
  indicator?: React.ReactNode;
  /**
   * HTML id for radio button
   */
  id?: string;
  /**
   * Radio button value
   */
  value: string;
  /**
   * Radio button disabled state
   */
  disabled?: boolean;
}

const MoonUIRadioGroupItemPro = React.forwardRef<
  HTMLInputElement,
  RadioGroupItemProps
>(({ className, variant, size, indicator, id, value, disabled, ...props }, ref) => {
  // Get context values from radio group
  const radioGroup = React.useContext(MoonUIRadioGroupContextPro);
  const generatedId = React.useId();
  const radioId = id || generatedId;
  const isChecked = radioGroup.value === value;
  
  // Call onValueChange function when RadioGroupItem changes
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (radioGroup.onValueChange) {
      radioGroup.onValueChange(e.target.value);
    }
    
    if (props.onChange) {
      props.onChange(e);
    }
  };
  
  return (
    <div className="relative flex items-center">
      <input
        type="radio"
        id={radioId}
        ref={ref}
        value={value}
        checked={isChecked}
        disabled={disabled || radioGroup.disabled}
        name={radioGroup.name}
        onChange={handleChange}
        className="sr-only"
        {...props}
      />
      <label
        htmlFor={radioId}
        className={cn(
          MoonUIradioGroupItemVariantsPro({ variant, size }),
          "rounded-full",
          "focus-visible:ring-primary/50",
          "relative inline-flex shrink-0 cursor-pointer items-center justify-center overflow-hidden",
          disabled && "cursor-not-allowed opacity-50",
          className
        )}
      >
        <span className={cn(
          "absolute inset-0 pointer-events-none",
          isChecked && "flex items-center justify-center"
        )}>
          {isChecked && (indicator || <Circle className="h-[60%] w-[60%] fill-current text-current" />)}
        </span>
      </label>
    </div>
  );
});
MoonUIRadioGroupItemPro.displayName = "RadioGroupItemPro";

// Radio Label Component
interface RadioLabelProps extends React.HTMLAttributes<HTMLLabelElement> {
  /**
   * HTML id for radio button
   */
  htmlFor?: string;
  /**
   * Label content
   */
  children: React.ReactNode;
  /**
   * Disabled state style
   */
  disabled?: boolean;
}

const MoonUIRadioLabelPro = React.forwardRef<HTMLLabelElement, RadioLabelProps>(
  ({ className, htmlFor, children, disabled = false, ...props }, ref) => {
    return (
      <label
        ref={ref}
        htmlFor={htmlFor}
        className={cn(
          "text-sm font-medium leading-none ml-2 text-foreground",
          disabled && "cursor-not-allowed opacity-70",
          className
        )}
        {...props}
      >
        {children}
      </label>
    );
  }
);
MoonUIRadioLabelPro.displayName = "RadioLabelPro";

// Radio Item and Label combination
interface RadioItemWithLabelProps extends RadioGroupItemProps {
  /**
   * Label content
   */
  label: React.ReactNode;
  /**
   * HTML classes for label
   */
  labelClassName?: string;
  /**
   * HTML id for radio button
   */
  id?: string;
  /**
   * Disabled state
   */
  disabled?: boolean;
}

const MoonUIRadioItemWithLabelPro = React.forwardRef<
  HTMLInputElement,
  RadioItemWithLabelProps
>(({ 
  id, 
  label, 
  labelClassName, 
  ...radioProps 
}, ref) => {
  const generatedId = React.useId();
  const radioId = id || generatedId;
  
  return (
    <div className="flex items-center">
      <MoonUIRadioGroupItemPro ref={ref} id={radioId} {...radioProps} />
      <MoonUIRadioLabelPro 
        htmlFor={radioId}
        disabled={radioProps.disabled}
        className={labelClassName}
      >
        {label}
      </MoonUIRadioLabelPro>
    </div>
  );
});
MoonUIRadioItemWithLabelPro.displayName = "RadioItemWithLabelPro";


// Internal aliases for Pro component usage
export const radioGroupItemVariantsInternal = MoonUIradioGroupItemVariantsPro
export const RadioGroupContextInternal = MoonUIRadioGroupContextPro
export const RadioGroupInternal = MoonUIRadioGroupPro
export const RadioGroupItemInternal = MoonUIRadioGroupItemPro
export const radioGroupInternal = MoonUIRadioGroupPro
export const RadioLabelInternal = MoonUIRadioLabelPro
export const RadioItemWithLabelInternal = MoonUIRadioItemWithLabelPro

// Pro exports  
export { MoonUIradioGroupItemVariantsPro, MoonUIRadioGroupContextPro, MoonUIRadioGroupPro, MoonUIRadioGroupItemPro, MoonUIRadioLabelPro, MoonUIRadioItemWithLabelPro }

// Clean exports (without MoonUI prefix for easier usage)
export { MoonUIradioGroupItemVariantsPro as radioGroupItemVariants, MoonUIRadioGroupContextPro as RadioGroupContext, MoonUIRadioGroupPro as RadioGroup, MoonUIRadioGroupItemPro as RadioGroupItem, MoonUIRadioLabelPro as RadioLabel, MoonUIRadioItemWithLabelPro as RadioItemWithLabel };
