"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 { useUserReply } from "./useUserReply";

/**
 * The four dialogs a reply thread can open: add a reply, edit one, confirm
 * a delete, and confirm an edit.
 *
 * They are mounted together because they share one submit-in-progress flag
 * and one validation ref, which the thread owns.
 */
/*
 * `screen` is the thread hook's whole return, destructured below. `postId` is
 * the thread's own prop, so it is separate — reading it off the bag would have
 * typechecked and been undefined.
 */
export default function UserReplyModals({
  screen,
  postId,
}: {
  screen: ReturnType<typeof useUserReply>;
  postId: CommunityEntityId;
}) {
  const { t } = useTranslation();
  const {
    refetch,
    formRef,
    handleConfirmDelete,
    handleConfirmUpdate,
    handleSaveClick,
    isSubmitting,
    openConfirmModal,
    openCreateReplyOfReplyForm,
    openDeleteConfirmModal,
    openReplyForm,
    replyId,
    setIsSubmitting,
    validateFormRef,
    deleteMutation,
    setOpenReplyForm,
    setOpenCreateReplyOfReplyForm,
    setOpenDeleteConfirmModal,
    setOpenConfirmModal,
    setIsNewReplyCreated,
  } = screen;

  return (
    <>
      <Modal
        open={openReplyForm}
        onClose={() => setOpenReplyForm(false)}
        title={t("COMMON.UPDATE_REPLY")}
        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={handleSaveClick}
              disabled={isSubmitting}
            >
              {t("COMMON.SAVE")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xs:!w-full"
              onClick={() => setOpenReplyForm(false)}
              disabled={isSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <ReplyForm
          postId={postId}
          replyId={replyId}
          onLoadingChange={setIsSubmitting}
          afterReplyCreation={() => {
            setOpenReplyForm(false);
            setIsNewReplyCreated(true);
            refetch();
          }}
          formRef={formRef}
          validateFormRef={validateFormRef}
        />
      </Modal>

      <Modal
        open={openCreateReplyOfReplyForm}
        onClose={() => setOpenCreateReplyOfReplyForm(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={replyId}
          onLoadingChange={setIsSubmitting}
          afterReplyCreation={() => {
            setOpenCreateReplyOfReplyForm(false);
            setIsNewReplyCreated(true);
            refetch();
          }}
        />
      </Modal>

      <Modal
        open={openConfirmModal}
        onClose={() => setOpenConfirmModal(false)}
        title={t("COMMON.UPDATE_REPLY")}
        size="sm"
        footer={
          <div className="flex xs:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xs:!w-full"
              onClick={handleConfirmUpdate}
              disabled={isSubmitting}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xs:!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>

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