"use client";

import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { createId } from "@paralleldrive/cuid2";
import * as Collapsible from "@radix-ui/react-collapsible";
import { GripIcon, SquareArrowRightExitIcon, Undo2 } from "lucide-react";
import { useMemo, useState } from "react";
import toast from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { TSurveyQuota } from "@formbricks/types/quota";
import { TSurvey, TSurveyEndScreenCard, TSurveyRedirectUrlCard } from "@formbricks/types/surveys/types";
import { getTextContent } from "@formbricks/types/surveys/validation";
import { TUserLocale } from "@formbricks/types/user";
import { cn } from "@/lib/cn";
import { extractLanguageCodes } from "@/lib/i18n/utils";
import { recallToHeadline } from "@/lib/utils/recall";
import { EditorCardMenu } from "@/modules/survey/editor/components/editor-card-menu";
import { EndScreenForm } from "@/modules/survey/editor/components/end-screen-form";
import { RedirectUrlForm } from "@/modules/survey/editor/components/redirect-url-form";
import { getEndingCardTypeChangePatch } from "@/modules/survey/editor/lib/ending-card";
import {
  START_LOGIC_USE_INDEX,
  findEndingCardUsedInLogic,
  formatTextWithSlashes,
  isUsedInQuota,
} from "@/modules/survey/editor/lib/utils";
import { ConfirmationModal } from "@/modules/ui/components/confirmation-modal";
import { OptionsSwitch } from "@/modules/ui/components/options-switch";
import { TooltipRenderer } from "@/modules/ui/components/tooltip";

interface EditEndingCardProps {
  readonly localSurvey: TSurvey;
  readonly endingCardIndex: number;
  readonly setLocalSurvey: React.Dispatch<React.SetStateAction<TSurvey>>;
  readonly setActiveElementId: (id: string | null) => void;
  readonly activeElementId: string | null;
  readonly isInvalid: boolean;
  readonly addEndingCard: (index: number) => void;
  readonly isFormbricksCloud: boolean;
  readonly locale: TUserLocale;
  readonly isStorageConfigured: boolean;
  readonly quotas: TSurveyQuota[];
  readonly isExternalUrlsAllowed: boolean;
}

/** Drops `endingId` from the follow-ups that a deleted ending card triggered. */
const removeEndingFromFollowUps = (followUps: TSurvey["followUps"], endingId: string): TSurvey["followUps"] =>
  followUps.map((f) => {
    if (f.trigger.properties?.endingIds?.includes(endingId)) {
      return {
        ...f,
        trigger: {
          ...f.trigger,
          properties: {
            ...f.trigger.properties,
            endingIds: f.trigger.properties.endingIds.filter((id) => id !== endingId),
          },
        },
      };
    }

    return f;
  });

export const EditEndingCard = ({
  localSurvey,
  endingCardIndex,
  setLocalSurvey,
  setActiveElementId,
  activeElementId,
  isInvalid,
  addEndingCard,
  isFormbricksCloud,
  locale,
  isStorageConfigured,
  quotas,
  isExternalUrlsAllowed,
}: EditEndingCardProps) => {
  const selectedLanguageCode = "default";
  const { t } = useTranslation();

  const endingCard = useMemo(
    () => localSurvey.endings[endingCardIndex],
    [localSurvey.endings, endingCardIndex]
  );

  const isRedirectToUrlDisabled = isFormbricksCloud
    ? !isExternalUrlsAllowed && endingCard.type !== "redirectToUrl"
    : false;

  const [openDeleteConfirmationModal, setOpenDeleteConfirmationModal] = useState(false);

  const endingCardTypes = [
    { value: "endScreen", label: t("workspace.surveys.edit.ending_card") },
    {
      value: "redirectToUrl",
      label: t("workspace.surveys.edit.redirect_to_url"),
      disabled: isRedirectToUrlDisabled,
    },
  ];

  const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({
    id: endingCard.id,
  });

  const open = activeElementId === endingCard.id;

  const setOpen = (e: boolean) => {
    if (e) {
      setActiveElementId(endingCard.id);
    } else {
      setActiveElementId(null);
    }
  };

  const updateSurvey = (
    data: Partial<TSurveyEndScreenCard & { _forceUpdate?: boolean }> | Partial<TSurveyRedirectUrlCard>
  ) => {
    setLocalSurvey((prevSurvey) => {
      const currentEnding = prevSurvey.endings[endingCardIndex];

      // If subheader was explicitly deleted (is undefined) in the current state,
      // block ALL attempts to recreate it (from Editor cleanup/updates)
      // UNLESS it's a forced update from the "Add Description" button
      const filteredData = { ...data };
      const isForceUpdate = "_forceUpdate" in filteredData;
      if (isForceUpdate) {
        delete filteredData._forceUpdate; // Remove the flag
      }

      if (!isForceUpdate && currentEnding?.type === "endScreen" && currentEnding.subheader === undefined) {
        if ("subheader" in filteredData) {
          // Block subheader updates when it's been deleted (Editor cleanup trying to recreate)
          delete filteredData.subheader;
        }
      }

      const updatedEndings = prevSurvey.endings.map((ending, idx) =>
        idx === endingCardIndex ? { ...ending, ...filteredData } : ending
      );
      return { ...prevSurvey, endings: updatedEndings };
    });
  };

  const deleteEndingCard = () => {
    const quotaIdx = quotas.findIndex((quota) => isUsedInQuota(quota, { endingCardId: endingCard.id }));
    if (quotaIdx !== -1) {
      toast.error(
        t("workspace.surveys.edit.ending_used_in_quota", {
          quotaName: quotas[quotaIdx].name,
        })
      );
      return;
    }
    const isEndingCardUsedInFollowUps = localSurvey.followUps.some((followUp) => {
      if (followUp.trigger.type === "endings") {
        if (followUp.trigger.properties?.endingIds?.includes(endingCard.id)) {
          return true;
        }
      }

      return false;
    });

    // checking if this ending card is used in logic
    const quesIdx = findEndingCardUsedInLogic(localSurvey, endingCard.id);

    if (quesIdx === START_LOGIC_USE_INDEX) {
      toast.error(t("workspace.surveys.edit.ending_card_used_in_start_logic"));
      return;
    }
    if (quesIdx !== -1) {
      toast.error(t("workspace.surveys.edit.ending_card_used_in_logic", { questionIndex: quesIdx + 1 }));
      return;
    }

    if (isEndingCardUsedInFollowUps) {
      setOpenDeleteConfirmationModal(true);
      return;
    }

    setLocalSurvey((prevSurvey) => {
      const updatedEndings = prevSurvey.endings.filter((_, index) => index !== endingCardIndex);
      return { ...prevSurvey, endings: updatedEndings };
    });
  };

  const style = {
    transition: transition ?? "transform 100ms ease",
    transform: CSS.Translate.toString(transform),
    zIndex: isDragging ? 10 : 1,
  };

  const duplicateEndingCard = () => {
    setLocalSurvey((prevSurvey) => {
      const endingToDuplicate = prevSurvey.endings[endingCardIndex];
      const duplicatedEndingCard = {
        ...endingToDuplicate,
        id: createId(),
      };
      const updatedEndings = [
        ...prevSurvey.endings.slice(0, endingCardIndex + 1),
        duplicatedEndingCard,
        ...prevSurvey.endings.slice(endingCardIndex + 1),
      ];
      return { ...prevSurvey, endings: updatedEndings };
    });
  };

  const moveEndingCard = (index: number, up: boolean) => {
    setLocalSurvey((prevSurvey) => {
      const newEndings = [...prevSurvey.endings];
      const [movedEnding] = newEndings.splice(index, 1);
      newEndings.splice(up ? index - 1 : index + 1, 0, movedEnding);
      return { ...prevSurvey, endings: newEndings };
    });
  };

  return (
    <div
      className={cn(open ? "shadow-lg" : "shadow-md", "group z-20 flex flex-row rounded-lg bg-white")}
      ref={setNodeRef}
      style={style}
      id={endingCard.id}>
      <div
        {...listeners}
        {...attributes}
        className={cn(
          open ? "bg-slate-50" : "",
          "flex w-10 flex-col items-center justify-between rounded-l-lg border-t border-b border-l py-2 group-aria-expanded:rounded-bl-none",
          isInvalid ? "bg-red-400" : "bg-white group-hover:bg-slate-50"
        )}>
        <div className="mt-3 flex w-full justify-center">
          {/* The Flow tab's ending icon, so an ending card reads the same on both tabs. */}
          {endingCard.type === "endScreen" ? (
            <SquareArrowRightExitIcon className="size-4" />
          ) : (
            <Undo2 className="size-4 rotate-180" />
          )}
        </div>
        <button
          type="button"
          className="opacity-0 transition-all duration-300 group-hover:opacity-100 hover:cursor-move">
          <GripIcon className="size-4" />
        </button>
      </div>
      <Collapsible.Root
        open={open}
        onOpenChange={setOpen}
        className="flex-1 rounded-r-lg border border-slate-200 transition-all duration-300 ease-in-out">
        <Collapsible.CollapsibleTrigger
          asChild
          className="flex cursor-pointer justify-between rounded-r-lg p-5 hover:bg-slate-50">
          <div>
            <div className="inline-flex">
              <div>
                <p className="text-sm font-semibold">
                  {endingCard.type === "endScreen" &&
                    (endingCard.headline &&
                    recallToHeadline(endingCard.headline, localSurvey, true, selectedLanguageCode)[
                      selectedLanguageCode
                    ]
                      ? formatTextWithSlashes(
                          getTextContent(
                            recallToHeadline(endingCard.headline, localSurvey, true, selectedLanguageCode)[
                              selectedLanguageCode
                            ]
                          )
                        )
                      : t("workspace.surveys.edit.ending_card"))}
                  {endingCard.type === "redirectToUrl" &&
                    (endingCard.label || t("workspace.surveys.edit.redirect_to_url"))}
                </p>
                {!open && (
                  <p className="mt-1 truncate text-xs text-slate-500">
                    {endingCard.type === "endScreen"
                      ? t("workspace.surveys.edit.ending_card")
                      : t("workspace.surveys.edit.redirect_to_url")}
                  </p>
                )}
              </div>
            </div>

            <div className="flex items-center gap-x-4">
              <EditorCardMenu
                survey={localSurvey}
                cardIdx={endingCardIndex}
                lastCard={endingCardIndex === localSurvey.endings.length - 1}
                duplicateCard={duplicateEndingCard}
                deleteCard={deleteEndingCard}
                moveCard={moveEndingCard}
                card={endingCard}
                updateCard={() => {}}
                addEndingCard={addEndingCard}
                cardType="ending"
              />
            </div>
          </div>
        </Collapsible.CollapsibleTrigger>
        <Collapsible.CollapsibleContent className={`flex flex-col px-4 ${open && "mt-3 pb-6"}`}>
          <TooltipRenderer
            shouldRender={endingCard.type === "endScreen" && isRedirectToUrlDisabled}
            tooltipContent={t("workspace.surveys.edit.external_urls_paywall_tooltip")}
            triggerClass="w-full">
            <OptionsSwitch
              options={endingCardTypes}
              currentOption={endingCard.type}
              handleOptionChange={(newType) => {
                const selectedOption = endingCardTypes.find((option) => option.value === newType);
                if (!selectedOption?.disabled) {
                  updateSurvey(
                    getEndingCardTypeChangePatch(
                      endingCard,
                      newType === "redirectToUrl" ? "redirectToUrl" : "endScreen",
                      extractLanguageCodes(localSurvey.languages)
                    )
                  );
                }
              }}
            />
          </TooltipRenderer>
          {endingCard.type === "endScreen" && (
            <EndScreenForm
              localSurvey={localSurvey}
              endingCardIndex={endingCardIndex}
              isInvalid={isInvalid}
              selectedLanguageCode={selectedLanguageCode}
              updateSurvey={updateSurvey}
              endingCard={endingCard}
              locale={locale}
              isStorageConfigured={isStorageConfigured}
              isExternalUrlsAllowed={isExternalUrlsAllowed}
            />
          )}
          {endingCard.type === "redirectToUrl" && (
            <RedirectUrlForm localSurvey={localSurvey} endingCard={endingCard} updateSurvey={updateSurvey} />
          )}
        </Collapsible.CollapsibleContent>
      </Collapsible.Root>

      <ConfirmationModal
        buttonText={t("common.delete")}
        onConfirm={() => {
          setLocalSurvey((prevSurvey) => {
            const updatedEndings = prevSurvey.endings.filter((_, index) => index !== endingCardIndex);
            const surveyFollowUps = removeEndingFromFollowUps(prevSurvey.followUps, endingCard.id);

            return { ...prevSurvey, endings: updatedEndings, followUps: surveyFollowUps };
          });
        }}
        open={openDeleteConfirmationModal}
        setOpen={setOpenDeleteConfirmationModal}
        body={t("workspace.surveys.edit.follow_ups_ending_card_delete_modal_text")}
        title={t("workspace.surveys.edit.follow_ups_ending_card_delete_modal_title")}
      />
    </div>
  );
};
