"use client";

import React from "react";
import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";
import ReplyForm from "./ReplyForm";
import CreatePostModal from "./CreatePostModal";
import InnerPostConfirmModals from "./InnerPostConfirmModals";
import { useInnerPost } from "./useInnerPost";
import type { PostData } from "./InnerPost";

/**
 * The dialogs a post card can open: reply, edit (contact lives in `ContactAuthorButton`), and the two
 * confirmations.
 *
 * Mounted together because they share the submit-in-progress flag and the
 * validation ref the card owns.
 */
export default function InnerPostModals({
  screen,
  data,
  refetch,
}: {
  /* The card hook's whole return, destructured below. */
  screen: ReturnType<typeof useInnerPost>;
  /* The card's own props: not on the bag. */
  data: PostData["data"];
  refetch: () => void;
}) {
  const { t } = useTranslation();
  const {
    openReplyForm,
    setOpenReplyForm,
    opencreatepost,
    setcreatepost,
    isSubmittingReply,
    setIsSubmittingReply,
    isPostSubmitting,
    setIsPostSubmitting,
    formRef,
    validateFormRef,
    handleSaveClick,
    handleReplyCreated,
  } = screen;

  return (
    <>
      <Modal
        open={openReplyForm}
        onClose={() => setOpenReplyForm(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={isSubmittingReply}
              loading={isSubmittingReply}
            >
              {t("COMMON.SUBMIT")}
            </Button>
          </div>
        }
      >
        <ReplyForm
          postId={data?.id}
          onLoadingChange={(loading) => setIsSubmittingReply(loading)}
          afterReplyCreation={() => {
            setOpenReplyForm(false);
            handleReplyCreated();
          }}
        />
      </Modal>

      <Modal
        open={opencreatepost}
        onClose={() => setcreatepost(false)}
        title={t("COMMON.UPDATE_POST")}
        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={isPostSubmitting}
              loading={isPostSubmitting}
            >
              {t("COMMON.SAVE")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setcreatepost(false)}
              disabled={isPostSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <CreatePostModal
          onLoadingChange={(loading) => setIsPostSubmitting(loading)}
          afterPostCreation={() => {
            setcreatepost(false);
          }}
          refetch={refetch}
          id={data?.id}
          formRef={formRef}
          validateFormRef={validateFormRef}
        />
      </Modal>

      <InnerPostConfirmModals screen={screen} />
    </>
  );
}
