import { Check, ChevronDown } from "lucide-react";
import * as React from "react";
import { Button } from "@/components/general/button";
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuTrigger,
} from "@/components/general/dropdown-menu";
import {
  DropdownSearchInput,
  SEARCH_THRESHOLD,
  useDropdownSearch,
} from "@/components/general/dropdown-search";
import { ElementError, getElementErrorAria } from "@/components/general/element-error";
import { ElementHeader } from "@/components/general/element-header";
import { Input } from "@/components/general/input";
import { type TOptionLayout, getOptionGroupClassName, getOptionLayout } from "@/lib/option-layout";
import { FB_PART } from "@/lib/parts";
import { cn } from "@/lib/utils";

/**
 * Option for multi-select element
 */
export interface MultiSelectOption {
  /** Unique identifier for the option */
  id: string;
  /** Display label for the option */
  label: string;
}

/**
 * Text direction: 'ltr' (left-to-right), 'rtl' (right-to-left), or 'auto' (auto-detect from content)
 */
type TextDirection = "ltr" | "rtl" | "auto";

interface MultiSelectProps {
  /** Unique identifier for the element container */
  elementId: string;
  /** The main element or prompt text displayed as the headline */
  headline: string;
  /** Optional descriptive text displayed below the headline */
  description?: string;
  /** Unique identifier for the multi-select group */
  inputId: string;
  /** Array of options to choose from */
  options: MultiSelectOption[];
  /** Currently selected option IDs */
  value?: string[];
  /** Callback function called when selection changes */
  onChange: (value: string[]) => void;
  /** Whether the field is required (shows asterisk indicator) */
  required?: boolean;
  /** Custom label for the required indicator */
  requiredLabel?: string;
  /** Error message to display below the options */
  errorMessage?: string;
  /** Text direction: 'ltr' (left-to-right), 'rtl' (right-to-left), or 'auto' (auto-detect from content) */
  dir?: TextDirection;
  /** Whether the options are disabled */
  disabled?: boolean;
  /** Display variant: 'list' stacks checkboxes, 'row' lays them side by side, 'dropdown' shows a dropdown menu */
  variant?: "list" | "row" | "dropdown";
  /** Placeholder text for dropdown button when no options are selected */
  placeholder?: string;
  /** ID for the 'other' option that allows custom input */
  otherOptionId?: string;
  /** Label for the 'other' option */
  otherOptionLabel?: string;
  /** Placeholder text for the 'other' input field */
  otherOptionPlaceholder?: string;
  /** Custom value entered in the 'other' input field */
  otherValue?: string;
  /** Callback when the 'other' input value changes */
  onOtherValueChange?: (value: string) => void;
  /** IDs of options that should be exclusive (selecting them deselects all others) */
  exclusiveOptionIds?: string[];
  /** Image URL to display above the headline */
  imageUrl?: string;
  /** Alt text for the image; empty or absent marks it decorative */
  imageAltText?: string;
  /** Video URL to display above the headline */
  videoUrl?: string;
  /** Placeholder text for the search input in dropdown mode */
  searchPlaceholder?: string;
  /** Message shown when search yields no results */
  searchNoResultsText?: string;
}

// Shared className for option labels
const optionLabelClassName = "font-option text-option font-option-weight text-option-label";

// Shared className for option containers
const getOptionContainerClassName = (isSelected: boolean, isDisabled: boolean): string =>
  cn(
    "relative flex flex-col border transition-colors outline-none",
    "rounded-option px-option-x py-option-y",
    isSelected ? "bg-option-selected-bg border-brand-readable" : "bg-option-bg border-option-border",
    // No focus-within fill: it repainted the option in the *selected* colors, so the card's
    // mount autofocus made option 1 look answered (ENG-2288). Focus has its own uniform ring
    // on the option label, from survey-ui's globals.css.
    "hover:bg-option-hover-bg",
    isDisabled ? "cursor-not-allowed opacity-50" : "cursor-pointer"
  );

/**
 * Custom checkbox indicator driven by the sibling native input's :checked state via the Tailwind
 * `peer` utility. The native <input type="checkbox"> is visually hidden (sr-only) but stays in the
 * DOM as the real, focusable control; this span only paints the box and tick.
 */
function CheckboxIndicator(): React.JSX.Element {
  return (
    <span
      aria-hidden="true"
      data-fb-part={FB_PART.optionControl}
      className={cn(
        "border-input-border text-brand-foreground dark:bg-input-bg relative flex size-4 shrink-0 items-center justify-center rounded-[4px] border bg-white shadow-xs transition-colors",
        "peer-checked:bg-brand peer-checked:border-brand",
        // The tick lives inside this sibling span, so reveal it via a peer-checked descendant rule.
        "[&>svg]:opacity-0 [&>svg]:transition-opacity peer-checked:[&>svg]:opacity-100"
      )}>
      <Check className="size-3.5" />
    </span>
  );
}

interface MultiSelectOptionItemProps {
  inputId: string;
  option: MultiSelectOption;
  isChecked: boolean;
  isDisabled: boolean;
  errorMessage?: string;
  onAdd: (optionId: string) => void;
  onRemove: (optionId: string) => void;
}

function MultiSelectOptionItem({
  inputId,
  option,
  isChecked,
  isDisabled,
  errorMessage,
  onAdd,
  onRemove,
}: Readonly<MultiSelectOptionItemProps>): React.JSX.Element {
  const optionId = `${inputId}-${option.id}`;

  return (
    // Hooked on the visible row; its state is the native checkbox inside it (`:has(:checked)`).
    <label
      key={option.id}
      htmlFor={optionId}
      data-fb-part={FB_PART.option}
      data-checked={isChecked ? "true" : undefined}
      className={cn(getOptionContainerClassName(isChecked, isDisabled), isChecked && "z-10")}>
      <span className="flex items-center">
        <input
          type="checkbox"
          id={optionId}
          name={inputId}
          value={option.id}
          checked={isChecked}
          disabled={isDisabled}
          aria-invalid={Boolean(errorMessage)}
          className="peer sr-only"
          onChange={(e) => {
            if (e.target.checked) {
              onAdd(option.id);
            } else {
              onRemove(option.id);
            }
          }}
        />
        <CheckboxIndicator />
        <span data-fb-part={FB_PART.optionLabel} className={cn("mx-3", optionLabelClassName)}>
          {option.label}
        </span>
      </span>
    </label>
  );
}

interface DropdownVariantProps {
  inputId: string;
  options: MultiSelectOption[];
  selectedValues: string[];
  handleOptionAdd: (optionId: string) => void;
  handleOptionRemove: (optionId: string) => void;
  disabled: boolean;
  errorMessage?: string;
  displayText: string;
  hasOtherOption: boolean;
  otherOptionId?: string;
  isOtherSelected: boolean;
  otherOptionLabel: string;
  otherValue: string;
  handleOtherInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  otherOptionPlaceholder: string;
  dir: TextDirection;
  otherInputRef: React.RefObject<HTMLInputElement | null>;
  required: boolean;
  searchPlaceholder: string;
  searchNoResultsText: string;
}

function DropdownVariant({
  inputId,
  options,
  selectedValues,
  handleOptionAdd,
  handleOptionRemove,
  disabled,
  errorMessage,
  displayText,
  hasOtherOption,
  otherOptionId,
  isOtherSelected,
  otherOptionLabel,
  otherValue,
  handleOtherInputChange,
  otherOptionPlaceholder,
  dir,
  otherInputRef,
  searchPlaceholder,
  searchNoResultsText,
}: Readonly<DropdownVariantProps>): React.JSX.Element {
  const errorAria = getElementErrorAria(inputId, errorMessage);

  const handleOptionToggle = (optionId: string): void => {
    if (selectedValues.includes(optionId)) {
      handleOptionRemove(optionId);
    } else {
      handleOptionAdd(optionId);
    }
  };

  // Search + side-locking
  const allDropdownOptionCount = options.length + (hasOtherOption ? 1 : 0);
  const showSearch = allDropdownOptionCount > SEARCH_THRESHOLD;

  const {
    searchQuery,
    setSearchQuery,
    searchInputRef,
    lockedSide,
    contentRef,
    noneOption,
    noneMatchesSearch,
    filteredRegularOptions,
    otherMatchesSearch,
    hasNoResults,
    handleDropdownOpen,
    handleDropdownClose,
    focusMenuItem,
    handleContentKeyDown,
  } = useDropdownSearch({ options, hasOtherOption, otherOptionLabel, isSearchEnabled: showSearch });

  return (
    <div>
      <ElementError errorMessage={errorMessage} dir={dir} id={errorAria.errorId} />
      <DropdownMenu
        onOpenChange={(open) => {
          if (open) handleDropdownOpen();
          else handleDropdownClose();
        }}>
        <DropdownMenuTrigger asChild>
          {/* Named via aria-labelledby (headline + visible value) instead of aria-label:
              a rich-text headline would leak raw HTML into the accessible name, and the
              name must contain the visible text (WCAG 2.5.3 Label in Name). */}
          <Button
            variant="outline"
            disabled={disabled}
            data-fb-part={FB_PART.dropdown}
            className="rounded-input min-h-input bg-input-bg border-input-border text-input-text py-input-y px-input-x w-full justify-between"
            aria-invalid={errorAria.ariaInvalid}
            aria-describedby={errorAria.ariaDescribedBy}
            aria-labelledby={`${inputId}-headline ${inputId}-trigger-value`}>
            <span
              id={`${inputId}-trigger-value`}
              className="font-input font-input-weight text-input-text truncate">
              {displayText}
            </span>
            <ChevronDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
          </Button>
        </DropdownMenuTrigger>
        <DropdownMenuContent
          ref={contentRef}
          side={lockedSide}
          avoidCollisions={lockedSide === undefined}
          className="bg-option-bg border-input-border w-(--radix-dropdown-menu-trigger-width) overflow-hidden"
          align="start"
          onKeyDown={handleContentKeyDown}>
          {showSearch ? (
            <DropdownSearchInput
              searchQuery={searchQuery}
              setSearchQuery={setSearchQuery}
              searchInputRef={searchInputRef}
              placeholder={searchPlaceholder}
              dir={dir}
              onNavigateToOptions={focusMenuItem}
            />
          ) : null}
          <div className="max-h-[260px] overflow-y-auto">
            {filteredRegularOptions.map((option) => {
              const isChecked = selectedValues.includes(option.id);
              const optionId = `${inputId}-${option.id}`;

              return (
                <DropdownMenuCheckboxItem
                  key={option.id}
                  id={optionId}
                  dir={dir}
                  checked={isChecked}
                  onCheckedChange={() => {
                    handleOptionToggle(option.id);
                  }}
                  onSelect={(e) => {
                    e.preventDefault();
                  }}
                  disabled={disabled}>
                  <span
                    data-fb-part={FB_PART.optionLabel}
                    className="font-input font-input-weight text-input-text">
                    {option.label}
                  </span>
                </DropdownMenuCheckboxItem>
              );
            })}
            {otherMatchesSearch && otherOptionId ? (
              <DropdownMenuCheckboxItem
                id={`${inputId}-${otherOptionId}`}
                dir={dir}
                checked={isOtherSelected}
                onCheckedChange={() => {
                  if (isOtherSelected) {
                    handleOptionRemove(otherOptionId);
                  } else {
                    handleOptionAdd(otherOptionId);
                  }
                }}
                onSelect={(e) => {
                  e.preventDefault();
                }}
                disabled={disabled}>
                <span
                  data-fb-part={FB_PART.optionLabel}
                  className="font-input font-input-weight text-input-text">
                  {otherOptionLabel}
                </span>
              </DropdownMenuCheckboxItem>
            ) : null}
            {noneOption && noneMatchesSearch ? (
              <DropdownMenuCheckboxItem
                key={noneOption.id}
                id={`${inputId}-${noneOption.id}`}
                dir={dir}
                checked={selectedValues.includes(noneOption.id)}
                onCheckedChange={() => {
                  handleOptionToggle(noneOption.id);
                }}
                onSelect={(e) => {
                  e.preventDefault();
                }}
                disabled={disabled}>
                <span
                  data-fb-part={FB_PART.optionLabel}
                  className="font-input font-input-weight text-input-text">
                  {noneOption.label}
                </span>
              </DropdownMenuCheckboxItem>
            ) : null}
            {hasNoResults ? (
              <div className="text-input-placeholder px-2 py-4 text-center text-sm">
                {searchNoResultsText}
              </div>
            ) : null}
          </div>
        </DropdownMenuContent>
      </DropdownMenu>
      {/* The dropdown branch renders no fieldset/group, and this free text is a SIBLING of the
          trigger rather than a descendant, so nothing above it would supply a description. It also
          needs one most: validateMultiSelectOtherValue errors precisely when this box is empty, and
          it is the only native input[aria-invalid] here, so focusFirstControl lands right on it. */}
      {isOtherSelected ? (
        <Input
          ref={otherInputRef}
          type="text"
          value={otherValue}
          onChange={handleOtherInputChange}
          placeholder={otherOptionPlaceholder}
          disabled={disabled}
          aria-required
          aria-invalid={errorAria.ariaInvalid}
          aria-describedby={errorAria.ariaDescribedBy}
          dir={dir}
          className="mt-2 w-full"
        />
      ) : null}
    </div>
  );
}

interface ListVariantProps {
  inputId: string;
  options: MultiSelectOption[];
  selectedValues: string[];
  value: string[];
  handleOptionAdd: (optionId: string) => void;
  handleOptionRemove: (optionId: string) => void;
  disabled: boolean;
  errorMessage?: string;
  hasOtherOption: boolean;
  otherOptionId?: string;
  isOtherSelected: boolean;
  otherOptionLabel: string;
  otherValue: string;
  handleOtherInputChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  otherOptionPlaceholder: string;
  dir: TextDirection;
  otherInputRef: React.RefObject<HTMLInputElement | null>;
  layout: TOptionLayout;
}

function ListVariant({
  inputId,
  options,
  selectedValues,
  value,
  handleOptionAdd,
  handleOptionRemove,
  disabled,
  errorMessage,
  hasOtherOption,
  otherOptionId,
  isOtherSelected,
  otherOptionLabel,
  otherValue,
  handleOtherInputChange,
  otherOptionPlaceholder,
  dir,
  otherInputRef,
  layout,
}: Readonly<ListVariantProps>): React.JSX.Element {
  const isNoneSelected = value.includes("none");
  const otherTextId = otherOptionId ? `${inputId}-${otherOptionId}-input` : undefined;
  const errorAria = getElementErrorAria(inputId, errorMessage);

  const renderOption = (option: MultiSelectOption): React.JSX.Element => {
    const isChecked = selectedValues.includes(option.id);
    const isDisabled = disabled || (isNoneSelected && option.id !== "none");
    return (
      <MultiSelectOptionItem
        key={option.id}
        inputId={inputId}
        option={option}
        isChecked={isChecked}
        isDisabled={isDisabled}
        errorMessage={errorMessage}
        onAdd={handleOptionAdd}
        onRemove={handleOptionRemove}
      />
    );
  };

  return (
    <>
      <ElementError errorMessage={errorMessage} dir={dir} id={errorAria.errorId} />
      <div className={getOptionGroupClassName(layout)}>
        {options.filter((option) => option.id !== "none").map(renderOption)}
        {hasOtherOption && otherOptionId ? (
          // The free-text input must NOT live inside the option <label>: a label may own only one
          // labelable control, and label-area clicks would forward to the checkbox and toggle "Other"
          // off. The bordered box is a plain container; only the option row is the checkbox's label.
          <div
            data-fb-part={FB_PART.option}
            data-checked={isOtherSelected ? "true" : undefined}
            className={cn(
              getOptionContainerClassName(isOtherSelected, disabled || isNoneSelected),
              isOtherSelected && "z-10"
            )}>
            <label htmlFor={`${inputId}-${otherOptionId}`} className="flex cursor-pointer items-center">
              <input
                type="checkbox"
                id={`${inputId}-${otherOptionId}`}
                name={inputId}
                value={otherOptionId}
                checked={isOtherSelected}
                disabled={disabled || isNoneSelected}
                aria-invalid={Boolean(errorMessage)}
                className="peer sr-only"
                onChange={(e) => {
                  if (e.target.checked) {
                    handleOptionAdd(otherOptionId);
                  } else {
                    handleOptionRemove(otherOptionId);
                  }
                }}
              />
              <CheckboxIndicator />
              <span data-fb-part={FB_PART.optionLabel} className={cn("mx-3 grow", optionLabelClassName)}>
                {otherOptionLabel}
              </span>
            </label>
            {/* The enclosing <fieldset> carries aria-describedby, but an ancestor's description is
                not part of a descendant's accessible description (accname): focusing this input
                announces its own name/state/description only. Without this it would read as invalid
                with no reason. It describes one text input, not each option, so nothing repeats. */}
            {isOtherSelected ? (
              <Input
                type="text"
                id={otherTextId}
                value={otherValue}
                onChange={handleOtherInputChange}
                placeholder={otherOptionPlaceholder}
                disabled={disabled}
                aria-required
                aria-label={otherOptionLabel}
                aria-invalid={errorAria.ariaInvalid}
                aria-describedby={errorAria.ariaDescribedBy}
                dir={dir}
                className="mt-2 w-full"
                ref={otherInputRef}
              />
            ) : null}
          </div>
        ) : null}
        {options.filter((option) => option.id === "none").map(renderOption)}
      </div>
    </>
  );
}

function MultiSelect({
  elementId,
  headline,
  description,
  inputId,
  options,
  value = [],
  onChange,
  required = false,
  requiredLabel,
  errorMessage,
  dir = "auto",
  disabled = false,
  variant = "list",
  placeholder = "Select options...",
  otherOptionId,
  otherOptionLabel = "Other",
  otherOptionPlaceholder = "Please specify",
  otherValue = "",
  onOtherValueChange,
  exclusiveOptionIds = [],
  imageUrl,
  imageAltText,
  videoUrl,
  searchPlaceholder = "Search...",
  searchNoResultsText = "No results found",
}: Readonly<MultiSelectProps>): React.JSX.Element {
  // Ensure value is always an array
  const selectedValues = Array.isArray(value) ? value : [];
  const hasOtherOption = Boolean(otherOptionId);
  const isOtherSelected = Boolean(hasOtherOption && otherOptionId && selectedValues.includes(otherOptionId));
  const otherInputRef = React.useRef<HTMLInputElement>(null);
  const errorAria = getElementErrorAria(inputId, errorMessage);

  React.useEffect(() => {
    if (!isOtherSelected || disabled) return;

    // Delay focus to win against focus restoration when dropdown closes / checkbox receives focus.
    const timeoutId = globalThis.setTimeout(() => {
      globalThis.requestAnimationFrame(() => {
        otherInputRef.current?.focus();
      });
    }, 0);

    return () => {
      globalThis.clearTimeout(timeoutId);
    };
  }, [isOtherSelected, disabled, variant]);

  const handleOptionAdd = (optionId: string): void => {
    if (exclusiveOptionIds.includes(optionId)) {
      onChange([optionId]);
    } else {
      const newValues = selectedValues.filter((id) => !exclusiveOptionIds.includes(id));
      onChange([...newValues, optionId]);
    }
  };

  const handleOptionRemove = (optionId: string): void => {
    onChange(selectedValues.filter((id) => id !== optionId));
  };

  const handleOtherInputChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
    onOtherValueChange?.(e.target.value);
  };

  // Get selected option labels for dropdown display
  const selectedLabels = options.filter((opt) => selectedValues.includes(opt.id)).map((opt) => opt.label);

  // Handle "other" option label display
  if (hasOtherOption && otherOptionId && selectedValues.includes(otherOptionId)) {
    const otherLabel = otherValue || otherOptionLabel;
    if (!selectedLabels.includes(otherLabel)) {
      selectedLabels.push(otherLabel);
    }
  }

  let displayText = placeholder;
  if (selectedLabels.length > 0) {
    displayText = selectedLabels.join(", ");
  }

  const isListVariant = variant !== "dropdown";

  return (
    <div className="w-full space-y-4" id={elementId} dir={dir}>
      {isListVariant ? (
        // A checkbox group is role="group", which ARIA 1.2 gives neither aria-required nor
        // aria-invalid (aria-invalid was global in ARIA 1.1 but is not in 1.2), and checkboxes have
        // no radiogroup-equivalent role. Only the visible "Required" badge conveys requiredness;
        // aria-invalid stays as a best-effort hook while the live region carries the announcement.
        // The group is named by its headline via aria-labelledby instead of a <legend>, so the
        // headline's media/required badge are not nested in invalid block content.
        <fieldset
          className="w-full space-y-4"
          aria-labelledby={`${inputId}-headline`}
          aria-invalid={errorAria.ariaInvalid}
          aria-describedby={errorAria.ariaDescribedBy}>
          <ElementHeader
            headlineId={`${inputId}-headline`}
            headline={headline}
            description={description}
            required={required}
            requiredLabel={requiredLabel}
            imageUrl={imageUrl}
            imageAltText={imageAltText}
            videoUrl={videoUrl}
          />
          <div className="relative" data-element-input>
            <ListVariant
              inputId={inputId}
              options={options}
              selectedValues={selectedValues}
              value={value}
              handleOptionAdd={handleOptionAdd}
              handleOptionRemove={handleOptionRemove}
              disabled={disabled}
              errorMessage={errorMessage}
              hasOtherOption={hasOtherOption}
              otherOptionId={otherOptionId}
              isOtherSelected={isOtherSelected}
              otherOptionLabel={otherOptionLabel}
              otherValue={otherValue}
              handleOtherInputChange={handleOtherInputChange}
              otherOptionPlaceholder={otherOptionPlaceholder}
              dir={dir}
              otherInputRef={otherInputRef}
              layout={getOptionLayout(variant)}
            />
          </div>
        </fieldset>
      ) : (
        <>
          {/* Dropdown trigger is a Radix menu button named via aria-labelledby (headline id +
              visible value id); the headline is a plain label here (no htmlFor) to avoid
              pointing at a non-input. */}
          <ElementHeader
            headlineId={`${inputId}-headline`}
            headline={headline}
            description={description}
            required={required}
            requiredLabel={requiredLabel}
            imageUrl={imageUrl}
            imageAltText={imageAltText}
            videoUrl={videoUrl}
          />
          <div className="relative" data-element-input>
            <DropdownVariant
              inputId={inputId}
              options={options}
              selectedValues={selectedValues}
              handleOptionAdd={handleOptionAdd}
              handleOptionRemove={handleOptionRemove}
              disabled={disabled}
              errorMessage={errorMessage}
              displayText={displayText}
              hasOtherOption={hasOtherOption}
              otherOptionId={otherOptionId}
              isOtherSelected={isOtherSelected}
              otherOptionLabel={otherOptionLabel}
              otherValue={otherValue}
              handleOtherInputChange={handleOtherInputChange}
              otherOptionPlaceholder={otherOptionPlaceholder}
              dir={dir}
              otherInputRef={otherInputRef}
              required={required}
              searchPlaceholder={searchPlaceholder}
              searchNoResultsText={searchNoResultsText}
            />
          </div>
        </>
      )}
    </div>
  );
}

export { MultiSelect };
export type { MultiSelectProps };
