"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 CreatePostModal from "./CreatePostModal";
import ReplyForm from "./ReplyForm";

/**
 * The reply and edit-post dialogs on a post's own page.
 *
 * Both are mounted together because they share the submit-in-progress and
 * validation plumbing the page owns.
 */
export default function CommunityDetailModals({
  open,
  setOpen,
  opencreatepost,
  setcreatepost,
  isSubmitting,
  setIsSubmitting,
  refetch,
  postId,
  setIsReplyCreated,
  handleSaveClick,
  isPostSubmitting,
  setIsPostSubmitting,
  formRef,
  validateFormRef,
}: {
  open: boolean;
  setOpen: (open: boolean) => void;
  opencreatepost: boolean;
  setcreatepost: (open: boolean) => void;
  isSubmitting: boolean;
  setIsSubmitting: (value: boolean) => void;
  id: string;
  refetch: () => void;
  postId: CommunityEntityId;
  setIsReplyCreated: React.Dispatch<React.SetStateAction<boolean>>;
  handleSaveClick: (event: React.MouseEvent) => void | Promise<void>;
  isPostSubmitting: boolean;
  setIsPostSubmitting: (value: boolean) => void;
  /* The edit-post form's own refs, separate from the reply form's. */
  formRef: React.RefObject<HTMLFormElement | null>;
  validateFormRef: React.RefObject<(() => Promise<boolean>) | null>;
}) {
  const { t } = useTranslation();

  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>
        }
      >
        <ReplyForm
          postId={postId}
          onLoadingChange={(loading) => setIsSubmitting(loading)}
          afterReplyCreation={() => {
            setOpen(false);
            setIsReplyCreated((prev) => !prev); // Toggle to trigger re-render
            refetch(); // Update post data including reply count
          }}
        />
      </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}
            >
              {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={postId}
          formRef={formRef}
          validateFormRef={validateFormRef}
          key={opencreatepost ? "open" : "closed"} // Force re-render on open
        />
      </Modal>
    </>
  );
}
