"use client";

import {
  DndContext,
  DragEndEvent,
  PointerSensor,
  closestCorners,
  useSensor,
  useSensors,
} from "@dnd-kit/core";
import { SortableContext, verticalListSortingStrategy } from "@dnd-kit/sortable";
import { useAutoAnimate } from "@formkit/auto-animate/react";
import { createId } from "@paralleldrive/cuid2";
import React, { SetStateAction, useEffect, useMemo, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { TI18nString } from "@formbricks/types/i18n";
import { TSurveyQuota } from "@formbricks/types/quota";
import { TSurveyBlock } from "@formbricks/types/surveys/blocks";
import { TSurveyElement } from "@formbricks/types/surveys/elements";
import { TSurvey } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { getDefaultEndingCard } from "@/app/lib/survey-builder";
import { addMultiLanguageLabels, createI18nString, extractLanguageCodes } from "@/lib/i18n/utils";
import { structuredClone } from "@/lib/pollyfills/structuredClone";
import { checkForEmptyFallBackValue } from "@/lib/utils/recall";
import { type TWorkspaceWithoutCustomCss } from "@/modules/custom-css/lib/types";
import { AddElementButton } from "@/modules/survey/editor/components/add-element-button";
import { AddEndingCardButton } from "@/modules/survey/editor/components/add-ending-card-button";
import { BlocksDroppable } from "@/modules/survey/editor/components/blocks-droppable";
import { EditEndingCard } from "@/modules/survey/editor/components/edit-ending-card";
import { EditWelcomeCard } from "@/modules/survey/editor/components/edit-welcome-card";
import { EmbeddedDataCard } from "@/modules/survey/editor/components/embedded-data-card";
import {
  addElementToBlock,
  deleteBlock,
  deleteElementFromBlock,
  duplicateBlock as duplicateBlockHelper,
  findElementLocation,
  getAutoBlockName,
  moveBlock as moveBlockHelper,
  moveElementInBlock,
  renumberAutoNamedBlocks,
  updateElementInBlock,
  withFilledCodes,
} from "@/modules/survey/editor/lib/blocks";
import { describeDeletionWarning } from "@/modules/survey/editor/lib/deletion-warning";
import {
  type TUseKind,
  findBlockUsedInLogic,
  findElementUse,
  isUsedInQuota,
  isUsedInRecall,
  scrollElementCardIntoView,
} from "@/modules/survey/editor/lib/utils";
import { getElementsFromBlocks } from "@/modules/survey/lib/client-utils";
import { Alert, AlertButton, AlertDescription, AlertTitle } from "@/modules/ui/components/alert";
import { ConfirmationModal } from "@/modules/ui/components/confirmation-modal";
import { useStableCallback } from "@/modules/ui/hooks/use-stable-callback";
import {
  isBlockLogicItemValid,
  isEndingCardValid,
  isWelcomeCardValid,
  validateElement,
} from "../lib/validation";

// Module-level so the sensor descriptor keeps its identity: a fresh options object per render gives
// DndContext new activators, which re-renders every useSortable consumer (each BlockCard) on every edit.
const POINTER_SENSOR_OPTIONS = { activationConstraint: { distance: 5 } };

interface ElementsViewProps {
  localSurvey: TSurvey;
  setLocalSurvey: React.Dispatch<SetStateAction<TSurvey>>;
  /** The survey as stored. The Embedded Data card reads it for promote and for the name guard. */
  persistedSurvey: TSurvey;
  activeElementId: string | null;
  setActiveElementId: (elementId: string | null) => void;
  workspace: TWorkspaceWithoutCustomCss;
  invalidElements: string[] | null;
  setInvalidElements: React.Dispatch<SetStateAction<string[] | null>>;
  selectedLanguageCode: string;
  isFormbricksCloud: boolean;
  isCxMode: boolean;
  locale: TUserLocale;
  responseCount: number;
  isStorageConfigured: boolean;
  quotas: TSurveyQuota[];
  isExternalUrlsAllowed: boolean;
  /** The one-time "Logic has moved" notice (ENG-6929), until the Flow tab has been opened once. */
  showLogicMovedNotice?: boolean;
  onOpenFlow?: () => void;
}

export const ElementsView = ({
  activeElementId,
  setActiveElementId,
  localSurvey,
  setLocalSurvey,
  persistedSurvey,
  workspace,
  invalidElements,
  setInvalidElements,
  selectedLanguageCode,
  isFormbricksCloud,
  isCxMode,
  locale,
  responseCount,
  isStorageConfigured = true,
  quotas,
  isExternalUrlsAllowed,
  showLogicMovedNotice = false,
  onOpenFlow,
}: Readonly<ElementsViewProps>) => {
  const { t } = useTranslation();
  const [logicDeletionWarning, setLogicDeletionWarning] = React.useState<{
    open: boolean;
    elementIdx: number;
    type: "element" | "block";
    blockId?: string;
    /** What uses the question: a rule (the default), a show-if, or another question's copied options. */
    useKind?: TUseKind;
  }>({
    open: false,
    elementIdx: 0,
    type: "element",
  });
  const deletionWarning = describeDeletionWarning(logicDeletionWarning.useKind, t);

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

  const surveyLanguages = localSurvey.languages;

  const updateElement = (elementIdx: number, updatedAttributes: Record<string, unknown>) => {
    // Get element ID from current elements array (for validation)
    const element = elements[elementIdx];
    if (!element) return;

    // Store element ID for use in functional updater
    const elementId = element.id;

    // Use functional update to ensure we work with the latest state
    setLocalSurvey((prevSurvey) => {
      // Re-find element location in the CURRENT state to avoid stale data
      const { blockId, blockIndex } = findElementLocation(prevSurvey, elementId);

      // If element no longer exists in survey (e.g., block was deleted), don't update
      if (!blockId || blockIndex === -1) {
        return prevSurvey;
      }

      let updatedSurvey = { ...prevSurvey };

      // Handle block-level attributes (logic, logicFallback, buttonLabel, backButtonLabel) separately
      const blockLevelAttributes: Record<string, unknown> = {};
      const elementLevelAttributes: Record<string, unknown> = {};

      Object.keys(updatedAttributes).forEach((key) => {
        if (
          key === "logic" ||
          key === "logicFallback" ||
          key === "buttonLabel" ||
          key === "backButtonLabel"
        ) {
          blockLevelAttributes[key] = updatedAttributes[key];
        } else {
          elementLevelAttributes[key] = updatedAttributes[key];
        }
      });

      // Update block-level attributes if any
      if (Object.keys(blockLevelAttributes).length > 0) {
        const blocks = [...(updatedSurvey.blocks ?? [])];
        blocks[blockIndex] = {
          ...blocks[blockIndex],
          ...blockLevelAttributes,
        };
        updatedSurvey = { ...updatedSurvey, blocks };
      }

      // Update element-level attributes if any
      if (Object.keys(elementLevelAttributes).length > 0) {
        const attributesToCheck = ["upperLabel", "lowerLabel"];

        // If the value of upperLabel or lowerLabel is equal to {default:""}, then delete the key
        const cleanedAttributes = { ...elementLevelAttributes };
        attributesToCheck.forEach((attribute) => {
          if (Object.keys(cleanedAttributes).includes(attribute)) {
            const currentLabel = cleanedAttributes[attribute] as TI18nString | undefined;
            if (
              currentLabel &&
              Object.keys(currentLabel).length === 1 &&
              currentLabel["default"].trim() === ""
            ) {
              delete cleanedAttributes[attribute];
            }
          }
        });

        const result = updateElementInBlock(updatedSurvey, blockId, elementId, cleanedAttributes);

        if (!result.ok) {
          // Can't show toast inside functional updater, return unchanged
          return prevSurvey;
        }

        updatedSurvey = result.data;
      }

      return updatedSurvey;
    });
  };

  // Update block name (block-level property)
  const updateBlockName = (blockIdx: number, name: string) => {
    if (blockIdx < 0 || blockIdx >= localSurvey.blocks.length) return;

    setLocalSurvey((prevSurvey) => {
      const blocks = [...(prevSurvey.blocks ?? [])];
      blocks[blockIdx] = {
        // Written through verbatim, including empty: clearing the field must leave it cleared
        // rather than snapping back to the auto name. isSurveyValid blocks the save instead.
        ...blocks[blockIdx],
        name,
      };
      return { ...prevSurvey, blocks };
    });
  };

  // Update block button label (block-level property)
  const updateBlockButtonLabel = (
    blockIndex: number,
    labelKey: "buttonLabel" | "backButtonLabel",
    labelValue: TI18nString | undefined
  ) => {
    setLocalSurvey((prevSurvey) => {
      const blocks = [...(prevSurvey.blocks ?? [])];

      // Bounds check
      if (blockIndex < 0 || blockIndex >= blocks.length) {
        return prevSurvey;
      }

      blocks[blockIndex] = {
        ...blocks[blockIndex],
        [labelKey]: labelValue,
      };

      return { ...prevSurvey, blocks };
    });
  };

  const validateElementDeletion = (elementId: string, elementIdx: number): boolean => {
    const recallElementIdx = isUsedInRecall(localSurvey, elementId);
    if (recallElementIdx === elements.length) {
      toast.error(t("workspace.surveys.edit.question_used_in_recall_ending_card"));
      return false;
    }
    if (recallElementIdx !== -1) {
      toast.error(
        t("workspace.surveys.edit.question_used_in_recall", { questionIndex: recallElementIdx + 1 })
      );
      return false;
    }

    const quotaIdx = quotas.findIndex((quota) => isUsedInQuota(quota, { elementId: elementId }));
    if (quotaIdx !== -1) {
      toast.error(
        t("workspace.surveys.edit.question_used_in_quota", {
          questionIndex: elementIdx + 1,
          quotaName: quotas[quotaIdx].name,
        })
      );
      return false;
    }

    return true;
  };

  const handleActiveElementAfterDeletion = (
    elementId: string,
    elementIdx: number,
    updatedSurvey: TSurvey,
    activeElementIdTemp: string
  ) => {
    if (elementId === activeElementIdTemp) {
      const newElements = updatedSurvey.blocks.flatMap((b) => b.elements) ?? [];
      const firstEndingCard = localSurvey.endings[0];
      if (elementIdx <= newElements.length && newElements.length > 0) {
        setActiveElementId(newElements[elementIdx % newElements.length].id);
      } else if (firstEndingCard) {
        setActiveElementId(firstEndingCard.id);
      }
    }
  };

  const executeDeletion = (elementIdx: number) => {
    const element = elements[elementIdx];
    if (!element) return;

    const elementId = element.id;
    const activeElementIdTemp = activeElementId ?? elements[0]?.id;
    // let updatedSurvey = removeRecallReferences(localSurvey, elementId);
    let updatedSurvey = structuredClone(localSurvey);

    const { blockId, blockIndex } = findElementLocation(localSurvey, elementId);
    if (!blockId || blockIndex === -1) return;

    const block = updatedSurvey.blocks[blockIndex];
    const result =
      block.elements.length === 1
        ? deleteBlock(updatedSurvey, blockId)
        : deleteElementFromBlock(updatedSurvey, blockId, elementId);

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    updatedSurvey = result.data;
    setLocalSurvey(updatedSurvey);

    handleActiveElementAfterDeletion(elementId, elementIdx, updatedSurvey, activeElementIdTemp);

    toast.success(t("workspace.surveys.edit.question_deleted"));
  };

  const deleteElement = (elementIdx: number) => {
    const element = elements[elementIdx];
    if (!element) return;

    const elementId = element.id;
    if (!validateElementDeletion(elementId, elementIdx)) {
      return;
    }

    const use = findElementUse(localSurvey, elementId);
    if (use) {
      setLogicDeletionWarning({ open: true, elementIdx, type: "element", useKind: use.kind });
      return;
    }

    executeDeletion(elementIdx);
  };

  const duplicateElement = (elementIdx: number) => {
    const element = elements[elementIdx];
    if (!element) return;

    const { blockId, blockIndex } = findElementLocation(localSurvey, element.id);
    if (!blockId || blockIndex === -1) return;

    // Create a duplicate of the element with a new ID
    const newElementId = createId();
    // The copy drops the question code so it takes the next unused one (answer codes stay).
    const { code: _code, ...elementWithoutCode } = element as TSurveyElement & { code?: string };
    const duplicatedElement = { ...elementWithoutCode, id: newElementId } as TSurveyElement;

    // Add the duplicated element to the same block
    const result = addElementToBlock(localSurvey, blockId, duplicatedElement);

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    setActiveElementId(newElementId);

    setLocalSurvey(result.data);
    toast.success(t("workspace.surveys.edit.question_duplicated"));
  };

  const addElement = (element: TSurveyElement, index?: number) => {
    // Use functional update to ensure we work with the latest state
    const newBlockId = createId();

    setLocalSurvey((prevSurvey) => {
      const languageSymbols = extractLanguageCodes(prevSurvey.languages);
      const updatedElement = addMultiLanguageLabels(element, languageSymbols);

      const newBlock = {
        id: newBlockId,
        name: getAutoBlockName(index ?? prevSurvey.blocks.length),
        elements: [{ ...updatedElement, isDraft: true }],
        buttonLabel: createI18nString("", []),
        backButtonLabel: createI18nString("", []),
      };

      return withFilledCodes({
        ...prevSurvey,
        blocks: [...prevSurvey.blocks, newBlock],
      });
    });

    setActiveElementId(element.id);
    scrollElementCardIntoView(element.id);
  };

  const _addElementToBlock = (element: TSurveyElement, blockId: string, afterElementIdx: number) => {
    const languageSymbols = extractLanguageCodes(localSurvey.languages);
    const updatedElement = addMultiLanguageLabels(element, languageSymbols);

    const targetIndex = afterElementIdx + 1;
    const result = addElementToBlock(
      localSurvey,
      blockId,
      {
        ...updatedElement,
        isDraft: true,
      },
      targetIndex
    );

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    setLocalSurvey(result.data);
    setActiveElementId(updatedElement.id);
    scrollElementCardIntoView(updatedElement.id);
  };

  const moveElementToBlock = (elementId: string, targetBlockId: string) => {
    const updatedSurvey = structuredClone(localSurvey);

    // Find the source block and element
    let sourceBlock: TSurveyBlock | undefined;
    let elementToMove: TSurveyElement | undefined;
    let elementIndexInBlock = -1;

    for (const block of updatedSurvey.blocks) {
      const idx = block.elements.findIndex((el) => el.id === elementId);
      if (idx !== -1) {
        sourceBlock = block;
        elementToMove = block.elements[idx];
        elementIndexInBlock = idx;
        break;
      }
    }

    if (!sourceBlock || !elementToMove) {
      toast.error(t("workspace.surveys.edit.element_not_found"));
      return;
    }

    // Remove element from source block
    sourceBlock.elements.splice(elementIndexInBlock, 1);

    // If source block is now empty, delete it
    if (sourceBlock.elements.length === 0) {
      const blockIdx = updatedSurvey.blocks.findIndex((b) => b.id === sourceBlock?.id);
      if (blockIdx !== -1) {
        updatedSurvey.blocks.splice(blockIdx, 1);
      }
    }

    // Add element to target block at the end
    const targetBlock = updatedSurvey.blocks.find((b) => b.id === targetBlockId);
    if (!targetBlock) {
      toast.error(t("workspace.surveys.edit.target_block_not_found"));
      return;
    }

    targetBlock.elements.push(elementToMove);

    setLocalSurvey(updatedSurvey);
    setActiveElementId(elementId);
  };

  const addEndingCard = (index: number) => {
    const updatedSurvey = structuredClone(localSurvey);
    const newEndingCard = getDefaultEndingCard(localSurvey.languages, t);

    updatedSurvey.endings.splice(index, 0, newEndingCard);
    setActiveElementId(newEndingCard.id);

    setLocalSurvey(updatedSurvey);
  };

  const moveElement = (elementIdx: number, up: boolean) => {
    const element = elements[elementIdx];
    if (!element) return;

    const { blockId, blockIndex } = findElementLocation(localSurvey, element.id);
    if (!blockId || blockIndex === -1) return;

    const block = localSurvey.blocks[blockIndex];
    const elementIndex = block.elements.findIndex((el) => el.id === element.id);

    // If block has multiple elements, move element within the block
    if (block.elements.length > 1) {
      // Check if we can move in the desired direction within the block
      if ((up && elementIndex > 0) || (!up && elementIndex < block.elements.length - 1)) {
        const direction = up ? "up" : "down";
        const result = moveElementInBlock(localSurvey, blockId, element.id, direction);

        if (!result.ok) {
          toast.error(result.error.message);
          return;
        }

        setLocalSurvey(result.data);
        return;
      }
      // If we can't move within block, fall through to move the entire block
    }

    // Move the entire block
    const direction = up ? "up" : "down";
    const result = moveBlockHelper(localSurvey, blockId, direction);

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    setLocalSurvey(result.data);
  };

  // Block-level operations
  const duplicateBlock = (blockId: string) => {
    const result = duplicateBlockHelper(localSurvey, blockId);

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    // Find the duplicated block and set the first element as active
    const blockIndex = localSurvey.blocks.findIndex((b) => b.id === blockId);
    if (blockIndex !== -1) {
      const duplicatedBlock = result.data.blocks[blockIndex + 1];
      if (duplicatedBlock?.elements[0]) {
        setActiveElementId(duplicatedBlock.elements[0].id);
      }
    }

    setLocalSurvey(result.data);
    toast.success(t("workspace.surveys.edit.block_duplicated"));
  };

  const executeBlockDeletion = (blockId: string) => {
    // First check if block exists in current state (for validation and calculating next active element)
    const blockExists = localSurvey.blocks.some((b) => b.id === blockId);
    if (!blockExists) {
      return;
    }

    // Prevent deleting the last block
    if (localSurvey.blocks.length === 1) {
      return;
    }

    // Calculate the new active element before deletion
    const remainingBlocks = localSurvey.blocks.filter((b) => b.id !== blockId);
    let newActiveElementId: string | null = null;
    if (remainingBlocks.length > 0 && remainingBlocks[0].elements.length > 0) {
      newActiveElementId = remainingBlocks[0].elements[0].id;
    } else if (localSurvey.endings[0]) {
      newActiveElementId = localSurvey.endings[0].id;
    }

    // Use functional update to ensure we work with the latest state
    setLocalSurvey((prevSurvey) => {
      const result = deleteBlock(prevSurvey, blockId);
      if (!result.ok) {
        // Return unchanged if block not found (shouldn't happen but be safe)
        return prevSurvey;
      }
      return result.data;
    });

    // Set active element after queuing the survey update
    if (newActiveElementId) {
      setActiveElementId(newActiveElementId);
    }
  };

  const deleteBlockById = (blockId: string) => {
    // First check if block is used in logic
    const usedElementIdx = findBlockUsedInLogic(localSurvey, blockId);
    if (usedElementIdx !== -1) {
      setLogicDeletionWarning({ open: true, elementIdx: 0, type: "block", blockId });
      return;
    }

    // Then check if any element in the block is used in recall/quota
    const block = localSurvey.blocks.find((b) => b.id === blockId);
    if (block) {
      for (const element of block.elements) {
        const elementIdx = elements.findIndex((e) => e.id === element.id);
        if (!validateElementDeletion(element.id, elementIdx)) {
          return;
        }
      }
    }

    executeBlockDeletion(blockId);
  };

  const moveBlockById = (blockId: string, direction: "up" | "down") => {
    const result = moveBlockHelper(localSurvey, blockId, direction);

    if (!result.ok) {
      toast.error(result.error.message);
      return;
    }

    setLocalSurvey(result.data);
  };

  // Validate survey when changes are made to languages or elements
  // using set for O(1) lookup
  useEffect(() => {
    if (!invalidElements) return;

    const currentInvalidSet = new Set(invalidElements);
    let hasChanges = false;

    // Live re-validation: clear errors as elements become valid (including
    // drafts), but don't flag freshly added drafts — save/publish does that.
    elements.forEach((element) => {
      const isValid = validateElement(element, surveyLanguages);
      if (isValid) {
        if (currentInvalidSet.has(element.id)) {
          currentInvalidSet.delete(element.id);
          hasChanges = true;
        }
      } else if (!element.isDraft && !currentInvalidSet.has(element.id)) {
        currentInvalidSet.add(element.id);
        hasChanges = true;
      }
    });

    // Check welcome card
    if (localSurvey.welcomeCard.enabled && !isWelcomeCardValid(localSurvey.welcomeCard, surveyLanguages)) {
      if (!currentInvalidSet.has("start")) {
        currentInvalidSet.add("start");
        hasChanges = true;
      }
    } else if (currentInvalidSet.has("start")) {
      currentInvalidSet.delete("start");
      hasChanges = true;
    }

    // Check thank you card
    localSurvey.endings.forEach((ending) => {
      if (!isEndingCardValid(ending, surveyLanguages)) {
        if (!currentInvalidSet.has(ending.id)) {
          currentInvalidSet.add(ending.id);
          hasChanges = true;
        }
      } else if (currentInvalidSet.has(ending.id)) {
        currentInvalidSet.delete(ending.id);
        hasChanges = true;
      }
    });

    // Live-clear conditional logic and block-name errors as they get fixed. We only clear
    // here (never add) so freshly added, not-yet-filled-in rules aren't
    // flagged prematurely — logic errors are added on save/publish instead.
    localSurvey.blocks.forEach((block) => {
      if (currentInvalidSet.has(block.id) && block.name.trim()) {
        currentInvalidSet.delete(block.id);
        hasChanges = true;
      }

      (block.logic ?? []).forEach((logicItem) => {
        if (currentInvalidSet.has(logicItem.id) && isBlockLogicItemValid(logicItem)) {
          currentInvalidSet.delete(logicItem.id);
          hasChanges = true;
        }
      });
    });

    if (hasChanges) {
      setInvalidElements(Array.from(currentInvalidSet));
    }
  }, [
    elements,
    surveyLanguages,
    invalidElements,
    setInvalidElements,
    localSurvey.welcomeCard,
    localSurvey.endings,
    localSurvey.blocks,
  ]);

  useEffect(() => {
    const elementWithEmptyFallback = checkForEmptyFallBackValue(localSurvey, selectedLanguageCode);
    if (elementWithEmptyFallback && activeElementId !== elementWithEmptyFallback.id) {
      setActiveElementId(elementWithEmptyFallback.id);
      toast.error(t("workspace.surveys.edit.fallback_missing"));
    }
  }, [activeElementId, setActiveElementId, localSurvey, selectedLanguageCode, t]);

  const sensors = useSensors(useSensor(PointerSensor, POINTER_SENSOR_OPTIONS));

  const onBlockCardDragEnd = (event: DragEndEvent) => {
    const { active, over } = event;

    if (!over) return;

    const sourceBlockIndex = localSurvey.blocks.findIndex((b) => b.id === active.id);
    const destBlockIndex = localSurvey.blocks.findIndex((b) => b.id === over.id);

    if (sourceBlockIndex !== -1 && destBlockIndex !== -1) {
      // We're dragging blocks
      if (sourceBlockIndex === destBlockIndex) return; // No move needed

      const blocks = [...localSurvey.blocks];
      const [movedBlock] = blocks.splice(sourceBlockIndex, 1);
      blocks.splice(destBlockIndex, 0, movedBlock);

      setLocalSurvey({ ...localSurvey, blocks: renumberAutoNamedBlocks(blocks) });
    }
  };

  const onEndingCardDragEnd = (event: DragEndEvent) => {
    const { active, over } = event;
    const newEndings = Array.from(localSurvey.endings);
    const sourceIndex = newEndings.findIndex((ending) => ending.id === active.id);
    const destinationIndex = newEndings.findIndex((ending) => ending.id === over?.id);
    const [reorderedEndings] = newEndings.splice(sourceIndex, 1);
    newEndings.splice(destinationIndex, 0, reorderedEndings);
    const updatedSurvey = { ...localSurvey, endings: newEndings };
    setLocalSurvey(updatedSurvey);
  };

  // Auto animate
  const [parent] = useAutoAnimate();

  // BlockCard is memoized, so everything handed to it must keep its identity across renders. These
  // wrappers always run the handler from the latest render, i.e. against the current localSurvey.
  const [lastInteractedBlockId, setLastInteractedBlockId] = useState<string | null>(null);
  const stableMoveElement = useStableCallback(moveElement);
  const stableUpdateElement = useStableCallback(updateElement);
  const stableUpdateBlockName = useStableCallback(updateBlockName);
  const stableUpdateBlockButtonLabel = useStableCallback(updateBlockButtonLabel);
  const stableDuplicateElement = useStableCallback(duplicateElement);
  const stableDeleteElement = useStableCallback(deleteElement);
  const stableAddElement = useStableCallback(addElement);
  const stableDuplicateBlock = useStableCallback(duplicateBlock);
  const stableDeleteBlock = useStableCallback(deleteBlockById);
  const stableMoveBlock = useStableCallback(moveBlockById);
  const stableAddElementToBlock = useStableCallback(_addElementToBlock);
  const stableMoveElementToBlock = useStableCallback(moveElementToBlock);

  return (
    <div className="mt-12 w-full px-5 py-4">
      {showLogicMovedNotice && onOpenFlow && (
        <Alert variant="info" className="mb-5" alertRole="note">
          <AlertTitle>{t("workspace.surveys.edit.flow.logic_moved_title")}</AlertTitle>
          <AlertDescription>{t("workspace.surveys.edit.flow.logic_moved_description")}</AlertDescription>
          <AlertButton onClick={onOpenFlow}>{t("workspace.surveys.edit.flow.open_flow")}</AlertButton>
        </Alert>
      )}
      {!isCxMode && (
        <div className="mb-5 flex w-full flex-col gap-5">
          <EditWelcomeCard
            localSurvey={localSurvey}
            setLocalSurvey={setLocalSurvey}
            setActiveElementId={setActiveElementId}
            activeElementId={activeElementId}
            isInvalid={invalidElements ? invalidElements.includes("start") : false}
            locale={locale}
            isStorageConfigured={isStorageConfigured}
            isExternalUrlsAllowed={isExternalUrlsAllowed}
          />
        </div>
      )}

      <DndContext
        id="blocks"
        sensors={sensors}
        onDragEnd={onBlockCardDragEnd}
        collisionDetection={closestCorners}>
        <BlocksDroppable
          localSurvey={localSurvey}
          persistedSurvey={persistedSurvey}
          setLocalSurvey={setLocalSurvey}
          workspace={workspace}
          moveElement={stableMoveElement}
          updateElement={stableUpdateElement}
          updateBlockName={stableUpdateBlockName}
          updateBlockButtonLabel={stableUpdateBlockButtonLabel}
          duplicateElement={stableDuplicateElement}
          deleteElement={stableDeleteElement}
          activeElementId={activeElementId}
          setActiveElementId={setActiveElementId}
          invalidElements={invalidElements}
          addElement={stableAddElement}
          isFormbricksCloud={isFormbricksCloud}
          isCxMode={isCxMode}
          locale={locale}
          isStorageConfigured={isStorageConfigured}
          isExternalUrlsAllowed={isExternalUrlsAllowed}
          duplicateBlock={stableDuplicateBlock}
          deleteBlock={stableDeleteBlock}
          moveBlock={stableMoveBlock}
          addElementToBlock={stableAddElementToBlock}
          moveElementToBlock={stableMoveElementToBlock}
          lastInteractedBlockId={lastInteractedBlockId}
          onBlockInteract={setLastInteractedBlockId}
        />
      </DndContext>

      <AddElementButton addElement={addElement} workspace={workspace} isCxMode={isCxMode} />
      <div className="mt-5 flex flex-col gap-5" ref={parent}>
        <hr className="border-t border-dashed" />
        <DndContext
          id="endings"
          sensors={sensors}
          onDragEnd={onEndingCardDragEnd}
          collisionDetection={closestCorners}>
          <SortableContext items={localSurvey.endings} strategy={verticalListSortingStrategy}>
            {localSurvey.endings.map((ending, index) => {
              return (
                <EditEndingCard
                  key={ending.id}
                  localSurvey={localSurvey}
                  endingCardIndex={index}
                  setLocalSurvey={setLocalSurvey}
                  setActiveElementId={setActiveElementId}
                  activeElementId={activeElementId}
                  isInvalid={invalidElements ? invalidElements.includes(ending.id) : false}
                  addEndingCard={addEndingCard}
                  isFormbricksCloud={isFormbricksCloud}
                  locale={locale}
                  isStorageConfigured={isStorageConfigured}
                  quotas={quotas}
                  isExternalUrlsAllowed={isExternalUrlsAllowed}
                />
              );
            })}
          </SortableContext>
        </DndContext>

        {!isCxMode && (
          <>
            <AddEndingCardButton localSurvey={localSurvey} addEndingCard={addEndingCard} />
            <hr />
            <EmbeddedDataCard
              localSurvey={localSurvey}
              setLocalSurvey={setLocalSurvey}
              persistedSurvey={persistedSurvey}
              activeElementId={activeElementId}
              setActiveElementId={setActiveElementId}
              quotas={quotas}
              responseCount={responseCount}
              workspaceId={workspace.id}
              locale={locale}
            />
          </>
        )}
      </div>

      <ConfirmationModal
        open={logicDeletionWarning.open}
        setOpen={(open) => setLogicDeletionWarning((prev) => ({ ...prev, open: open as boolean }))}
        title={deletionWarning.title}
        body={deletionWarning.body}
        buttonText={t("workspace.surveys.edit.delete_anyways")}
        onConfirm={() => {
          if (logicDeletionWarning.type === "element") {
            executeDeletion(logicDeletionWarning.elementIdx);
          } else if (logicDeletionWarning.type === "block" && logicDeletionWarning.blockId) {
            executeBlockDeletion(logicDeletionWarning.blockId);
          }
          setLogicDeletionWarning((prev) => ({ ...prev, open: false }));
        }}
      />
    </div>
  );
};
