"use client";

import type { CommunityEntityId } from "@/features/community/communityRecord";
import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";
import ReplyForm from "./ReplyForm";
import ReplyOfReplyForm from "./ReplyOfReplyForm";
import type { useReplyDetail } from "./useReplyDetail";

/**
 * The dialogs on a reply's own page: reply, edit, and the two confirmations.
 */
export default function ReplyDetailModals({
  screen,
  postId,
  replyId,
}: {
  /* The page hook's whole return, destructured below. */
  screen: ReturnType<typeof useReplyDetail>;
  /* The route's own params: not on the bag. */
  postId: CommunityEntityId;
  replyId: CommunityEntityId;
}) {
  const { t } = useTranslation();
  const {
    open,
    setOpen,
    openEditReplyModal,
    setOpenEditReplyModal,
    openConfirmModal,
    setOpenConfirmModal,
    openDeleteConfirmModal,
    setOpenDeleteConfirmModal,
    isSubmitting,
    setIsSubmitting,
    setIsReplyCreated,
    formRef,
    validateFormRef,
    handleSaveClick,
    handleConfirmUpdate,
    handleConfirmDelete,
    deleteMutation,
    replyToId,
    setReplyToId,
    selectedReplyId,
    refetch,
  } = screen;

  return (
    <>
      <Modal
        open={open}
        onClose={() => setOpen(false)}
        title={t("COMMON.REPLY_FORM")}
        size="md"
        footer={
          <div className="flex xs:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xs:!w-full"
              onClick={() => {
                const form = document.querySelector("form") as HTMLFormElement;
                if (form) form.requestSubmit();
              }}
              disabled={isSubmitting}
            >
              {t("COMMON.SUBMIT")}
            </Button>
          </div>
        }
      >
        <ReplyOfReplyForm
          postId={postId || ""}
          replyId={replyToId || replyId || ""}
          onLoadingChange={(loading) => setIsSubmitting(loading)}
          afterReplyCreation={() => {
            setOpen(false);
            setIsReplyCreated((prev: boolean) => !prev);
            setReplyToId(""); // Reset after creation
          }}
        />
      </Modal>

      <Modal
        open={openEditReplyModal}
        onClose={() => setOpenEditReplyModal(false)}
        title={t("COMMON.UPDATE_REPLY")}
        size="md"
        footer={
          <div className="flex xss:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={handleSaveClick}
              disabled={isSubmitting}
            >
              {t("COMMON.SAVE")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setOpenEditReplyModal(false)}
              disabled={isSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <ReplyForm
          postId={postId || ""}
          replyId={selectedReplyId}
          onLoadingChange={(loading) => setIsSubmitting(loading)}
          afterReplyCreation={() => {
            setOpenEditReplyModal(false);
            refetch();
          }}
          formRef={formRef}
          validateFormRef={validateFormRef}
        />
      </Modal>

      {/* Update Confirmation Modal */}
      <Modal
        open={openConfirmModal}
        onClose={() => setOpenConfirmModal(false)}
        title={t("COMMON.UPDATE_REPLY")}
        size="sm"
        footer={
          <div className="flex xss:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={handleConfirmUpdate}
              disabled={isSubmitting}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setOpenConfirmModal(false)}
              disabled={isSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <h2 className="text-center pb-10 text-lg">
          {t("COMMON.ARE_YOU_SURE_UPDATE_REPLY")}
        </h2>
      </Modal>

      {/* Delete Confirmation Modal */}
      <Modal
        open={openDeleteConfirmModal}
        onClose={() => setOpenDeleteConfirmModal(false)}
        title={t("COMMON.DELETE_REPLY")}
        size="sm"
        footer={
          <div className="flex xss:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={handleConfirmDelete}
              disabled={deleteMutation.isPending}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setOpenDeleteConfirmModal(false)}
              disabled={deleteMutation.isPending}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <h2 className="text-center pb-10 text-lg">
          {t("COMMON.ARE_YOU_SURE_DELETE_REPLY")}
        </h2>
      </Modal>
    </>
  );
}
