"use client";

import { useAutoAnimate } from "@formkit/auto-animate/react";
import { ImagePlusIcon, TrashIcon } from "lucide-react";
import { type Dispatch, type SetStateAction, useCallback, useMemo, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { type TI18nString } from "@formbricks/types/i18n";
import { IMAGE_FILE_EXTENSIONS } from "@formbricks/types/storage";
import {
  TSurveyElement,
  TSurveyElementChoice,
  TSurveyElementTypeEnum,
} from "@formbricks/types/surveys/elements";
import {
  TSurvey,
  TSurveyEndScreenCard,
  TSurveyRecallItem,
  TSurveyRedirectUrlCard,
  TSurveyWelcomeCard,
} from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { cn } from "@/lib/cn";
import { createI18nString, extractLanguageCodes } from "@/lib/i18n/utils";
import { debounce } from "@/lib/utils/debounce";
import { useSyncScroll } from "@/lib/utils/hooks/useSyncScroll";
import { headlineToRecall, recallToHeadline } from "@/lib/utils/recall";
import { ImageAltTextButton } from "@/modules/survey/components/element-form-input/components/image-alt-text-button";
import { RecallWrapper } from "@/modules/survey/components/element-form-input/components/recall-wrapper";
import { getElementsFromBlocks } from "@/modules/survey/lib/client-utils";
import { LocalizedEditor } from "@/modules/survey/multi-language-surveys/components/localized-editor";
import { Button } from "@/modules/ui/components/button";
import { FileInput } from "@/modules/ui/components/file-input";
import { Input } from "@/modules/ui/components/input";
import { Label } from "@/modules/ui/components/label";
import { Switch } from "@/modules/ui/components/switch";
import { TooltipRenderer } from "@/modules/ui/components/tooltip";
import {
  determineImageUploaderVisibility,
  getChoiceLabel,
  getEndingCardText,
  getIndex,
  getMatrixLabel,
  getPlaceHolderById,
  getWelcomeCardText,
  isValueIncomplete,
} from "./utils";

interface ElementFormInputProps {
  readonly id: string;
  readonly value: TI18nString | undefined;
  readonly localSurvey: TSurvey;
  readonly elementIdx: number;
  readonly updateElement?: (elementIdx: number, data: Partial<TSurveyElement>) => void;
  readonly updateSurvey?: (
    data: Partial<TSurveyEndScreenCard> | Partial<TSurveyRedirectUrlCard> | Partial<TSurveyWelcomeCard>
  ) => void;
  readonly updateChoice?: (choiceIdx: number, data: Partial<TSurveyElementChoice>) => void;
  readonly updateMatrixLabel?: (index: number, type: "row" | "column", matrixLabel: TI18nString) => void;
  readonly isInvalid: boolean;
  readonly selectedLanguageCode?: string;
  readonly label: string;
  readonly maxLength?: number;
  readonly placeholder?: string;
  readonly onBlur?: React.FocusEventHandler<HTMLInputElement>;
  readonly className?: string;
  readonly locale: TUserLocale;
  readonly onKeyDown?: React.KeyboardEventHandler<HTMLInputElement>;
  readonly isStorageConfigured: boolean;
  /** Focus the field's editor when it mounts; see `Editor`. */
  readonly focusOnMount?: boolean;
  readonly firstRender?: boolean;
  readonly setFirstRender?: Dispatch<SetStateAction<boolean>>;
  readonly isExternalUrlsAllowed?: boolean;
  /** Read-only, e.g. option text on a published survey. */
  readonly disabled?: boolean;
  /** Overrides the default spacing around the label, e.g. `mt-0` when the input starts a panel. */
  readonly labelClassName?: string;
}

export const ElementFormInput = ({
  id,
  value,
  localSurvey,
  elementIdx,
  updateElement,
  updateSurvey,
  updateChoice,
  updateMatrixLabel,
  isInvalid,
  label,
  selectedLanguageCode = "default",
  maxLength,
  placeholder,
  onBlur,
  className,
  locale,
  onKeyDown,
  isStorageConfigured = true,
  focusOnMount,
  firstRender: externalFirstRender,
  setFirstRender: externalSetFirstRender,
  isExternalUrlsAllowed,
  disabled = false,
  labelClassName,
}: ElementFormInputProps) => {
  const { t } = useTranslation();
  const defaultLanguageCode =
    localSurvey.languages.filter((lang) => lang.default)[0]?.language.code ?? "default";
  const usedLanguageCode = selectedLanguageCode === defaultLanguageCode ? "default" : selectedLanguageCode;

  const elements = useMemo(() => getElementsFromBlocks(localSurvey.blocks), [localSurvey.blocks]);

  const currentElement: TSurveyElement = elements[elementIdx];
  const isChoice = id.includes("choice");
  const isMatrixLabelRow = id.includes("row");
  const isMatrixLabelColumn = id.includes("column");
  const inputId = useMemo(() => {
    return isChoice || isMatrixLabelColumn || isMatrixLabelRow ? id.split("-")[0] : id;
  }, [id, isChoice, isMatrixLabelColumn, isMatrixLabelRow]);

  const isEndingCard = elementIdx >= elements.length;
  const isWelcomeCard = elementIdx === -1;
  const index = getIndex(id, isChoice || isMatrixLabelColumn || isMatrixLabelRow);

  const elementId = useMemo(() => {
    if (isWelcomeCard) return "start";
    if (isEndingCard) return localSurvey.endings[elementIdx - elements.length].id;
    return currentElement.id;
  }, [isWelcomeCard, isEndingCard, currentElement?.id, elementIdx, elements.length, localSurvey.endings]);
  const endingCard = localSurvey.endings.find((ending) => ending.id === elementId);

  const surveyLanguageCodes = useMemo(
    () => extractLanguageCodes(localSurvey.languages),
    [localSurvey.languages]
  );
  const isTranslationIncomplete = useMemo(
    () => isValueIncomplete(inputId, isInvalid, surveyLanguageCodes, value),
    [value, inputId, isInvalid, surveyLanguageCodes]
  );

  const elementText = useMemo((): TI18nString => {
    if (isChoice && typeof index === "number") {
      return getChoiceLabel(currentElement, index, surveyLanguageCodes);
    }

    if (isWelcomeCard) {
      return getWelcomeCardText(localSurvey, id, surveyLanguageCodes);
    }

    if (isEndingCard) {
      return getEndingCardText(localSurvey, elements, id, surveyLanguageCodes, elementIdx);
    }

    if ((isMatrixLabelColumn || isMatrixLabelRow) && typeof index === "number") {
      return getMatrixLabel(currentElement, index, surveyLanguageCodes, isMatrixLabelRow ? "row" : "column");
    }

    // For block-level properties (buttonLabel, backButtonLabel) or when value is explicitly provided,
    // use the value prop directly instead of looking up on currentElement
    if (value && typeof value === "object" && "default" in value) {
      return value;
    }

    return (
      (currentElement &&
        (id.includes(".")
          ? // Handle nested properties
            (
              currentElement[id.split(".")[0] as keyof TSurveyElement] as
                | Record<string, TI18nString>
                | undefined
            )?.[id.split(".")[1]]
          : // Original behavior
            (currentElement[id as keyof TSurveyElement] as TI18nString))) ||
      createI18nString("", surveyLanguageCodes)
    );
  }, [
    id,
    index,
    isChoice,
    isEndingCard,
    isMatrixLabelColumn,
    isMatrixLabelRow,
    isWelcomeCard,
    localSurvey,
    currentElement,
    elementIdx,
    elements,
    surveyLanguageCodes,
    value,
  ]);

  const [text, setText] = useState(elementText);
  const [showImageUploader, setShowImageUploader] = useState<boolean>(
    determineImageUploaderVisibility(elementIdx, elements) ||
      (endingCard?.type === "endScreen" && Boolean(endingCard.imageUrl ?? endingCard.videoUrl))
  );

  // Sync text state when elementText changes (e.g., on page reload or when value prop changes).
  // Adjusted during render.
  const [syncedElementText, setSyncedElementText] = useState(elementText);
  if (syncedElementText !== elementText) {
    setSyncedElementText(elementText);
    setText(elementText);
  }

  const highlightContainerRef = useRef<HTMLInputElement>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  // Hook to synchronize the horizontal scroll position of highlightContainerRef and inputRef.
  useSyncScroll(highlightContainerRef, inputRef);

  const createUpdatedText = useCallback(
    (updatedText: string): TI18nString => {
      return {
        ...elementText,
        [usedLanguageCode]: updatedText,
      };
    },
    [elementText, usedLanguageCode]
  );

  const updateChoiceDetails = useCallback(
    (translatedText: TI18nString) => {
      if (updateChoice && typeof index === "number") {
        updateChoice(index, { label: translatedText });
      }
    },
    [index, updateChoice]
  );

  const updateSurveyDetails = useCallback(
    (translatedText: TI18nString) => {
      if (updateSurvey) {
        updateSurvey({ [id]: translatedText });
      }
    },
    [id, updateSurvey]
  );

  const updateMatrixLabelDetails = useCallback(
    (translatedText: TI18nString) => {
      if (updateMatrixLabel && typeof index === "number") {
        updateMatrixLabel(index, isMatrixLabelRow ? "row" : "column", translatedText);
      }
    },
    [index, isMatrixLabelRow, updateMatrixLabel]
  );

  const updateElementDetails = useCallback(
    (translatedText: TI18nString) => {
      if (updateElement) {
        // Handle nested properties if id contains a dot
        if (id.includes(".")) {
          const [parent, child] = id.split(".");
          updateElement(elementIdx, {
            [parent]: {
              ...((currentElement as Record<string, unknown>)[parent] as Record<string, unknown>),
              [child]: translatedText,
            },
          });
        } else {
          // Original behavior for non-nested properties
          updateElement(elementIdx, { [id]: translatedText });
        }
      }
    },
    [id, elementIdx, updateElement, currentElement]
  );

  const handleUpdate = useCallback(
    (updatedText: string) => {
      const translatedText = createUpdatedText(updatedText);

      if (isChoice) {
        updateChoiceDetails(translatedText);
      } else if (isEndingCard || isWelcomeCard) {
        updateSurveyDetails(translatedText);
      } else if (isMatrixLabelRow || isMatrixLabelColumn) {
        updateMatrixLabelDetails(translatedText);
      } else {
        updateElementDetails(translatedText);
      }
    },
    [
      createUpdatedText,
      isChoice,
      isEndingCard,
      isMatrixLabelColumn,
      isMatrixLabelRow,
      isWelcomeCard,
      updateChoiceDetails,
      updateMatrixLabelDetails,
      updateElementDetails,
      updateSurveyDetails,
    ]
  );

  const handleKeyDown = useCallback(
    (e: React.KeyboardEvent<HTMLInputElement>) => {
      if (onKeyDown) onKeyDown(e);
    },
    [onKeyDown]
  );

  const getFileUrl = (): string | undefined => {
    if (isWelcomeCard) return localSurvey.welcomeCard.fileUrl;
    if (isEndingCard) {
      if (endingCard && endingCard.type === "endScreen") return endingCard.imageUrl;
    } else return currentElement.imageUrl;
  };

  const getImageAltText = (): TI18nString | undefined => {
    if (isEndingCard) return endingCard?.type === "endScreen" ? endingCard.imageAltText : undefined;
    return isWelcomeCard ? undefined : currentElement.imageAltText;
  };

  const getVideoUrl = (): string | undefined => {
    if (isWelcomeCard) return localSurvey.welcomeCard.videoUrl;
    if (isEndingCard) {
      if (endingCard && endingCard.type === "endScreen") return endingCard.videoUrl;
    } else return currentElement.videoUrl;
  };

  const debouncedHandleUpdate = useMemo(
    () => debounce((value: string) => handleUpdate(value), 100),
    [handleUpdate]
  );

  const [animationParent] = useAutoAnimate();
  const [internalFirstRender, setInternalFirstRender] = useState(true);
  const suppressEditorUpdatesRef = useRef(false);

  // Use external firstRender state if provided, otherwise use internal state
  const firstRender = externalFirstRender ?? internalFirstRender;
  const setFirstRender = externalSetFirstRender ?? setInternalFirstRender;

  const renderRemoveDescriptionButton = () => {
    if (id === "subheader") {
      return !!currentElement?.subheader || (endingCard?.type === "endScreen" && !!endingCard?.subheader);
    }

    return false;
  };

  const getIsRequiredToggleDisabled = (): boolean => {
    if (!currentElement) return false;

    // CTA elements should always have the required toggle disabled
    if (currentElement.type === TSurveyElementTypeEnum.CTA) {
      return true;
    }

    if (currentElement.type === TSurveyElementTypeEnum.Address) {
      const allFieldsAreOptional = [
        currentElement.addressLine1,
        currentElement.addressLine2,
        currentElement.city,
        currentElement.state,
        currentElement.zip,
        currentElement.country,
      ]
        .filter((field) => field.show)
        .every((field) => !field.required);

      if (allFieldsAreOptional) {
        return true;
      }

      return [
        currentElement.addressLine1,
        currentElement.addressLine2,
        currentElement.city,
        currentElement.state,
        currentElement.zip,
        currentElement.country,
      ]
        .filter((field) => field.show)
        .some((condition) => condition.required === true);
    }

    if (currentElement.type === TSurveyElementTypeEnum.ContactInfo) {
      const allFieldsAreOptional = [
        currentElement.firstName,
        currentElement.lastName,
        currentElement.email,
        currentElement.phone,
        currentElement.company,
      ]
        .filter((field) => field.show)
        .every((field) => !field.required);

      if (allFieldsAreOptional) {
        return true;
      }

      return [
        currentElement.firstName,
        currentElement.lastName,
        currentElement.email,
        currentElement.phone,
        currentElement.company,
      ]
        .filter((field) => field.show)
        .some((condition) => condition.required === true);
    }

    return false;
  };

  const handleMultiLangChange = useCallback(
    (newValue: string, recallItems?: TSurveyRecallItem[], fallbacks?: { [key: string]: string }) => {
      const updatedValue = {
        ...text,
        [usedLanguageCode]:
          recallItems && fallbacks ? headlineToRecall(newValue, recallItems, fallbacks) : newValue,
      };
      setText(updatedValue);
      debouncedHandleUpdate(updatedValue[usedLanguageCode]);
    },
    [text, usedLanguageCode, debouncedHandleUpdate]
  );

  const useRichTextEditor = id === "headline" || id === "subheader" || id === "html";

  // For rich text editor fields, we need either updateElement or updateSurvey
  if (useRichTextEditor && !updateElement && !updateSurvey) {
    throw new Error("Either updateElement or updateSurvey must be provided");
  }

  if (useRichTextEditor) {
    return (
      <div className="w-full">
        {label && (
          <div className={cn("mt-3 mb-2 flex items-center justify-between", labelClassName)}>
            <Label htmlFor={id}>{label}</Label>
            {id === "headline" && currentElement && updateElement && (
              <div className="flex items-center gap-x-2">
                <Label htmlFor="required-toggle" className="text-sm">
                  {t("workspace.surveys.edit.required")}
                </Label>
                <Switch
                  id="required-toggle"
                  checked={currentElement.required}
                  disabled={getIsRequiredToggleDisabled()}
                  onCheckedChange={(checked) => {
                    updateElement(elementIdx, { required: checked });
                  }}
                />
              </div>
            )}
          </div>
        )}
        <div className="flex flex-col gap-4" ref={animationParent}>
          {showImageUploader && id === "headline" && (
            <FileInput
              id="element-image"
              allowedFileExtensions={[...IMAGE_FILE_EXTENSIONS]}
              workspaceId={localSurvey.workspaceId}
              onFileUpload={(url: string[] | undefined, fileType: "image" | "video") => {
                if (url) {
                  // Alt text describes one specific image, so it goes when that image is replaced or removed.
                  const keepsImage = fileType === "image" && url[0] === getFileUrl();
                  const update =
                    fileType === "video"
                      ? { videoUrl: url[0], imageUrl: undefined, imageAltText: undefined }
                      : {
                          imageUrl: url[0],
                          videoUrl: undefined,
                          ...(keepsImage ? {} : { imageAltText: undefined }),
                        };
                  if ((isWelcomeCard || isEndingCard) && updateSurvey) {
                    updateSurvey(update);
                  } else if (updateElement) {
                    updateElement(elementIdx, update);
                  }
                }
              }}
              fileUrl={getFileUrl()}
              videoUrl={getVideoUrl()}
              isVideoAllowed={true}
              maxSizeInMB={5}
              isStorageConfigured={isStorageConfigured}
              renderImageAction={
                isWelcomeCard
                  ? undefined
                  : () => (
                      <ImageAltTextButton
                        label={t("workspace.surveys.edit.image_alt_text")}
                        hasAltText={Boolean(getImageAltText()?.default?.trim())}
                        isInvalid={isValueIncomplete(
                          "imageAltText",
                          isInvalid,
                          surveyLanguageCodes,
                          getImageAltText()
                        )}>
                        <ElementFormInput
                          id="imageAltText"
                          value={getImageAltText()}
                          localSurvey={localSurvey}
                          elementIdx={elementIdx}
                          updateElement={updateElement}
                          updateSurvey={updateSurvey}
                          isInvalid={isInvalid}
                          selectedLanguageCode={selectedLanguageCode}
                          label={t("workspace.surveys.edit.image_alt_text")}
                          placeholder={t("workspace.surveys.edit.image_alt_text_placeholder")}
                          locale={locale}
                          isStorageConfigured={isStorageConfigured}
                        />
                      </ImageAltTextButton>
                    )
              }
            />
          )}

          <div className="flex w-full items-start gap-2">
            <div className="flex-1">
              <LocalizedEditor
                key={`${elementId}-${id}-${selectedLanguageCode}`}
                id={id}
                value={value}
                localSurvey={localSurvey}
                elementIdx={elementIdx}
                isInvalid={isInvalid}
                updateElement={updateElement}
                updateCard={updateSurvey}
                selectedLanguageCode={selectedLanguageCode}
                firstRender={firstRender}
                setFirstRender={setFirstRender}
                locale={locale}
                elementId={elementId}
                isCard={isWelcomeCard || isEndingCard}
                focusOnMount={focusOnMount}
                isExternalUrlsAllowed={isExternalUrlsAllowed}
                suppressUpdates={() => suppressEditorUpdatesRef.current}
              />
            </div>

            {id === "headline" && !isWelcomeCard && (
              <TooltipRenderer
                tooltipContent={t("workspace.surveys.edit.add_photo_or_video")}
                delayDuration={100}>
                <Button
                  variant="secondary"
                  size="icon"
                  aria-label="Toggle image uploader"
                  data-testid="toggle-image-uploader-button"
                  onClick={(e) => {
                    e.preventDefault();
                    setShowImageUploader((prev) => !prev);
                  }}>
                  <ImagePlusIcon />
                </Button>
              </TooltipRenderer>
            )}

            {id === "subheader" && renderRemoveDescriptionButton() && (
              <TooltipRenderer tooltipContent={t("workspace.surveys.edit.remove_description")}>
                <Button
                  variant="secondary"
                  size="icon"
                  aria-label="Remove description"
                  onClick={(e) => {
                    e.preventDefault();

                    // Suppress Editor updates BEFORE calling updateElement to prevent race condition
                    // Use ref for immediate synchronous access
                    if (id === "subheader") {
                      suppressEditorUpdatesRef.current = true;
                    }

                    if (updateSurvey) {
                      updateSurvey({ subheader: undefined });
                    }

                    if (updateElement) {
                      updateElement(elementIdx, { subheader: undefined });
                    }

                    // Re-enable updates after a short delay to allow state to update
                    if (id === "subheader") {
                      setTimeout(() => {
                        suppressEditorUpdatesRef.current = false;
                      }, 100);
                    }
                  }}>
                  <TrashIcon />
                </Button>
              </TooltipRenderer>
            )}
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="w-full">
      {label && (
        <div className={cn("mt-3 mb-2 flex items-center justify-between", labelClassName)}>
          <Label htmlFor={id}>{label}</Label>
        </div>
      )}
      <RecallWrapper
        localSurvey={localSurvey}
        elementId={elementId}
        value={text[usedLanguageCode]}
        onChange={(value, recallItems, fallbacks) => {
          handleMultiLangChange(value, recallItems, fallbacks);
        }}
        onAddFallback={() => {
          inputRef.current?.focus();
        }}
        isRecallAllowed={false}
        usedLanguageCode={usedLanguageCode}
        render={({ value, onChange, highlightedJSX, children: recallComponents, isRecallSelectVisible }) => {
          return (
            <div className="flex flex-col gap-4 bg-white" ref={animationParent}>
              <div className="flex w-full items-center gap-x-2">
                <div className="group relative w-full">
                  {/* The highlight container is absolutely positioned behind the input */}
                  <div className="h-10 w-full"></div>
                  <div
                    ref={highlightContainerRef}
                    className="absolute top-0 z-0 mt-0.5 flex h-10 w-full no-scrollbar overflow-scroll px-3 py-2 text-center text-sm whitespace-nowrap text-transparent"
                    dir="auto"
                    key={highlightedJSX.toString()}>
                    {highlightedJSX}
                  </div>

                  <Input
                    key={`${elementId}-${id}-${usedLanguageCode}`}
                    value={
                      recallToHeadline(
                        {
                          [usedLanguageCode]: value,
                        },
                        localSurvey,
                        false,
                        usedLanguageCode
                      )[usedLanguageCode]
                    }
                    dir="auto"
                    onChange={(e) => onChange(e.target.value)}
                    id={id}
                    name={id}
                    placeholder={placeholder ?? getPlaceHolderById(id, t)}
                    aria-label={label}
                    maxLength={maxLength}
                    ref={inputRef}
                    onBlur={onBlur}
                    className={`absolute top-0 text-black caret-black ${className}`}
                    isInvalid={
                      isInvalid &&
                      text[usedLanguageCode]?.trim() === "" &&
                      localSurvey.languages?.length > 1 &&
                      isTranslationIncomplete
                    }
                    autoComplete={isRecallSelectVisible ? "off" : "on"}
                    autoFocus={false}
                    disabled={disabled}
                    onKeyDown={handleKeyDown}
                  />
                  {recallComponents}
                </div>
              </div>
            </div>
          );
        }}
      />
    </div>
  );
};
ElementFormInput.displayName = "ElementFormInput";
