"use client"

import * as React from "react"
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown, ChevronUp, Loader2, Search, X } from "lucide-react"

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

/**
 * Premium Select Component
 * 
 * Advanced dropdown/select component with variants, sizes and accessibility features.
 * Compatible with dark and light mode, providing a modern user experience with smooth animations.
 */

// Directly re-exporting the Root component
const MoonUISelectPro = SelectPrimitive.Root
MoonUISelectPro.displayName = "MoonUISelectPro"

const MoonUISelectGroupPro = SelectPrimitive.Group

const MoonUISelectValuePro = SelectPrimitive.Value

type SelectTriggerVariant = "standard" | "outline" | "ghost" | "underline";
type SelectTriggerSize = "sm" | "md" | "lg";

interface MoonUISelectTriggerProProps extends React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> {
  /** Visual variant */
  variant?: SelectTriggerVariant;
  /** Size */
  size?: SelectTriggerSize;
  /** Error state */
  error?: boolean | string;
  /** Success state */
  success?: boolean;
  /** Loading state */
  loading?: boolean;
  /** Icon displayed on the left */
  leftIcon?: React.ReactNode;
  /** Icon displayed on the right (instead of default chevron) */
  rightIcon?: React.ReactNode;
  /** Enable clear button */
  clearable?: boolean;
  /** On clear callback */
  onClear?: () => void;
  /** Custom placeholder when value is selected */
  selectedPlaceholder?: string;
}

const MoonUISelectTriggerPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Trigger>,
  MoonUISelectTriggerProProps
>(({ className, children, variant = "standard", size = "md", error, success, loading, leftIcon, rightIcon, ...props }, ref) => (
  <SelectPrimitive.Trigger
    ref={ref}
    className={cn(
      /* Base styles */
      "flex w-full items-center justify-between gap-1 rounded-md transition-all duration-200",
      "disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
      "focus-visible:outline-none",
      /* Error state */
      error && "border-error focus-visible:ring-error/30 focus-visible:border-error",
      /* Success state */
      success && "border-success focus-visible:ring-success/30 focus-visible:border-success",
      /* Size variants */
      size === "sm" && "h-8 text-xs px-2",
      size === "md" && "h-10 text-sm px-3",
      size === "lg" && "h-12 text-base px-4",
      /* Visual variants */
      variant === "standard" && "border border-gray-300 dark:border-gray-700 bg-background dark:bg-gray-900 hover:border-gray-400 dark:hover:border-gray-600 focus-visible:ring-2 focus-visible:ring-primary/30 dark:focus-visible:ring-primary/50 focus-visible:border-primary dark:focus-visible:border-primary",
      variant === "outline" && "border border-gray-300 dark:border-gray-700 bg-transparent hover:border-gray-400 dark:hover:border-gray-600 focus-visible:ring-2 focus-visible:ring-primary/30 dark:focus-visible:ring-primary/50 focus-visible:border-primary dark:focus-visible:border-primary",
      variant === "ghost" && "border-none bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 focus-visible:ring-2 focus-visible:ring-primary/30 dark:focus-visible:ring-primary/50",
      variant === "underline" && "border-t-0 border-l-0 border-r-0 border-b border-gray-300 dark:border-gray-600 rounded-none px-0 hover:border-gray-400 dark:hover:border-gray-500 focus-visible:ring-0 focus-visible:border-b-2 focus-visible:border-primary dark:focus-visible:border-primary",
      className
    )}
    {...props}
    disabled={props.disabled || loading}
    data-error={error ? true : undefined}
    data-success={success ? true : undefined}
    data-loading={loading ? true : undefined}
  >
    <div className="flex items-center flex-1 gap-2">
      {leftIcon && (
        <span className="flex items-center justify-center text-gray-500 dark:text-gray-400">
          {leftIcon}
        </span>
      )}
      {loading ? (
        <div className="flex items-center gap-2">
          <Loader2 className="h-3.5 w-3.5 animate-spin text-muted-foreground dark:text-gray-400" />
          <span className="text-muted-foreground dark:text-gray-400">Loading...</span>
        </div>
      ) : children}
    </div>
    {!loading && (
      <SelectPrimitive.Icon asChild>
        {rightIcon ? (
          <span className="opacity-60">{rightIcon}</span>
        ) : (
          <ChevronDown className="h-4 w-4 opacity-60 transition-transform duration-200 ease-out group-data-[state=open]:rotate-180" />
        )}
      </SelectPrimitive.Icon>
    )}
  </SelectPrimitive.Trigger>
))
MoonUISelectTriggerPro.displayName = SelectPrimitive.Trigger.displayName

/**
 * Scroll Up Button Component
 */
const MoonUISelectScrollUpButtonPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.ScrollUpButton>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpButton>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.ScrollUpButton
    ref={ref}
    className={cn(
      "flex cursor-default items-center justify-center py-1 text-muted-foreground hover:text-foreground transition-colors",
      className
    )}
    {...props}
  >
    <ChevronUp className="h-4 w-4" />
  </SelectPrimitive.ScrollUpButton>
))
MoonUISelectScrollUpButtonPro.displayName = SelectPrimitive.ScrollUpButton.displayName

/**
 * Scroll Down Button Component
 */
const MoonUISelectScrollDownButtonPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.ScrollDownButton>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownButton>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.ScrollDownButton
    ref={ref}
    className={cn(
      "flex cursor-default items-center justify-center py-1 text-muted-foreground hover:text-foreground transition-colors",
      className
    )}
    {...props}
  >
    <ChevronDown className="h-4 w-4" />
  </SelectPrimitive.ScrollDownButton>
))
MoonUISelectScrollDownButtonPro.displayName =
  SelectPrimitive.ScrollDownButton.displayName

interface MoonUISelectContentProProps extends React.ComponentPropsWithoutRef<typeof SelectPrimitive.Content> {
  /** Enable search functionality */
  enableSearch?: boolean;
  /** Search placeholder */
  searchPlaceholder?: string;
  /** No results text */
  noResultsText?: string;
  /** Loading search results */
  searchLoading?: boolean;
  /** Custom search icon */
  searchIcon?: React.ReactNode;
}

const MoonUISelectContentPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Content>,
  MoonUISelectContentProProps
>(({ className, children, position = "popper", enableSearch, searchPlaceholder = "Search...", noResultsText = "No results found", searchLoading, searchIcon, ...props }, ref) => {
  const [searchValue, setSearchValue] = React.useState("");
  
  // Filter children based on search
  const filteredChildren = React.useMemo(() => {
    if (!enableSearch || !searchValue) return children;
    
    const searchLower = searchValue.toLowerCase();
    return React.Children.toArray(children).filter((child) => {
      if (!React.isValidElement(child)) return true;
      
      // Keep groups and labels
      if (child.type === MoonUISelectGroupPro || child.type === MoonUISelectLabelPro || child.type === MoonUISelectSeparatorPro) {
        return true;
      }
      
      // Filter items
      if (child.type === MoonUISelectItemPro) {
        const reactChild = child as React.ReactElement<any>;
        const text = reactChild.props.children?.toString().toLowerCase() || "";
        return text.includes(searchLower);
      }
      
      return true;
    });
  }, [children, searchValue, enableSearch]);
  
  return (
    <SelectPrimitive.Portal>
      <SelectPrimitive.Content
        ref={ref}
        className={cn(
          ["relative z-50 max-h-96 min-w-[8rem] overflow-hidden", 
          "rounded-md border border-gray-200 dark:border-gray-700",
          "bg-white dark:bg-gray-900 text-foreground", 
          "shadow-lg dark:shadow-gray-900/20",
          "data-[state=open]:animate-in data-[state=closed]:animate-out",
          "data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
          "data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95", 
          "data-[state=open]:duration-150"],
          position === "popper" &&
            "data-[side=bottom]:translate-y-1 data-[side=left]:-translate-x-1 data-[side=right]:translate-x-1 data-[side=top]:-translate-y-1",
          className
        )}
        position={position}
        sideOffset={5}
        {...props}
      >
        {enableSearch && (
          <div className="p-2 border-b border-gray-200 dark:border-gray-700">
            <div className="flex items-center gap-2 px-2 py-1.5 rounded-md bg-gray-100 dark:bg-gray-800">
              {searchIcon || <Search className="h-4 w-4 text-gray-500 dark:text-gray-400" />}
              <input
                type="text"
                placeholder={searchPlaceholder}
                value={searchValue}
                onChange={(e) => setSearchValue(e.target.value)}
                className="flex-1 bg-transparent outline-none text-sm placeholder:text-gray-500 dark:placeholder:text-gray-400"
                onClick={(e) => e.stopPropagation()}
                onKeyDown={(e) => e.stopPropagation()}
              />
              {searchValue && (
                <button
                  onClick={(e) => {
                    e.stopPropagation();
                    setSearchValue("");
                  }}
                  className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
                >
                  <X className="h-3 w-3" />
                </button>
              )}
            </div>
          </div>
        )}
        <MoonUISelectScrollUpButtonPro />
        <SelectPrimitive.Viewport
          className={cn(
            "p-1",
            position === "popper" &&
              "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"
          )}
        >
          {searchLoading ? (
            <div className="flex items-center justify-center py-8">
              <Loader2 className="h-5 w-5 animate-spin text-gray-400" />
            </div>
          ) : filteredChildren && React.Children.count(filteredChildren) > 0 ? (
            filteredChildren
          ) : enableSearch ? (
            <div className="py-6 text-center text-sm text-gray-500 dark:text-gray-400">
              {noResultsText}
            </div>
          ) : (
            children
          )}
        </SelectPrimitive.Viewport>
        <MoonUISelectScrollDownButtonPro />
      </SelectPrimitive.Content>
    </SelectPrimitive.Portal>
  );
})
MoonUISelectContentPro.displayName = SelectPrimitive.Content.displayName

const MoonUISelectLabelPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Label>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.Label
    ref={ref}
    className={cn("py-1.5 pl-8 pr-2 text-sm font-semibold", className)}
    {...props}
  />
))
MoonUISelectLabelPro.displayName = SelectPrimitive.Label.displayName

type SelectItemVariant = "default" | "subtle" | "destructive" | "success" | "warning";
type SelectItemSize = "sm" | "md" | "lg";

interface MoonUISelectItemProProps extends React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item> {
  /** Visual variant */
  variant?: SelectItemVariant;
  /** Size */
  size?: SelectItemSize;
  /** Icon displayed on the right */
  rightIcon?: React.ReactNode;
  /** Custom indicator icon (instead of default check) */
  customIndicator?: React.ReactNode;
  /** Enable hover scale animation */
  enableHoverScale?: boolean;
  /** Description text shown below the main text */
  description?: string;
}

const MoonUISelectItemPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Item>,
  MoonUISelectItemProProps
>(({ className, children, variant = "default", size = "md", rightIcon, customIndicator, enableHoverScale, description, ...props }, ref) => (
  <SelectPrimitive.Item
    ref={ref}
    className={cn(
      "relative flex w-full cursor-default select-none items-center rounded-sm outline-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50 transition-all duration-150",
      
      /* Size variants */
      size === "sm" && "py-1 pl-7 pr-2 text-xs",
      size === "md" && "py-1.5 pl-8 pr-2 text-sm",
      size === "lg" && "py-2 pl-9 pr-3 text-base",
      
      /* Color variants */
      variant === "default" && "focus:bg-accent focus:text-accent-foreground dark:focus:bg-gray-700 dark:focus:text-gray-100",
      variant === "subtle" && "focus:bg-gray-100 dark:focus:bg-gray-800 focus:text-foreground dark:focus:text-gray-200",
      variant === "destructive" && "text-error dark:text-red-400 focus:bg-error/10 dark:focus:bg-red-900/20 focus:text-error dark:focus:text-red-300",
      variant === "success" && "text-success dark:text-green-400 focus:bg-success/10 dark:focus:bg-green-900/20 focus:text-success dark:focus:text-green-300",
      variant === "warning" && "text-warning dark:text-yellow-400 focus:bg-warning/10 dark:focus:bg-yellow-900/20 focus:text-warning dark:focus:text-yellow-300",
      
      /* Hover scale animation */
      enableHoverScale && "hover:scale-[1.02] hover:pl-9",
      
      className
    )}
    {...props}
  >
    <span className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
      <SelectPrimitive.ItemIndicator>
        {customIndicator || <Check className="h-4 w-4" />}
      </SelectPrimitive.ItemIndicator>
    </span>

    <div className="flex-1">
      <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
      {description && (
        <p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">{description}</p>
      )}
    </div>
    
    {rightIcon && (
      <span className="ml-auto pl-2 opacity-70">
        {rightIcon}
      </span>
    )}
  </SelectPrimitive.Item>
))
MoonUISelectItemPro.displayName = SelectPrimitive.Item.displayName

const MoonUISelectSeparatorPro = React.forwardRef<
  React.ElementRef<typeof SelectPrimitive.Separator>,
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
>(({ className, ...props }, ref) => (
  <SelectPrimitive.Separator
    ref={ref}
    className={cn("-mx-1 my-1 h-px bg-muted dark:bg-gray-700", className)}
    {...props}
  />
))
MoonUISelectSeparatorPro.displayName = SelectPrimitive.Separator.displayName

export { MoonUISelectPro,
  MoonUISelectGroupPro,
  MoonUISelectValuePro,
  MoonUISelectTriggerPro,
  MoonUISelectContentPro,
  MoonUISelectLabelPro,
  MoonUISelectItemPro,
  MoonUISelectSeparatorPro,
  MoonUISelectScrollUpButtonPro as SelectScrollUpButton,
  MoonUISelectScrollDownButtonPro as SelectScrollDownButton,
 };

// Backward compatibility exports
export { MoonUISelectPro as Select, MoonUISelectTriggerPro as SelectTrigger, MoonUISelectContentPro as SelectContent, MoonUISelectItemPro as SelectItem, MoonUISelectValuePro as SelectValue, MoonUISelectGroupPro as SelectGroup, MoonUISelectLabelPro as SelectLabel, MoonUISelectSeparatorPro as SelectSeparator }