"use client";

import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";
import { useTranslation } from "react-i18next";
import CommunityFilterModal, {
  CommunityFiltersData,
} from "./CommunityFilterModal";
import CreatePostModal from "./CreatePostModal";

/**
 * The two dialogs the post toolbar opens: filter, and create-a-post.
 *
 * `opencreatepost` stays in the parent because the "Create post" button that
 * sets it lives in the toolbar, not in here.
 */
export default function PostModals({
  openfilter,
  setOpenfilter,
  onApply,
  initialValues,
  clearFiltersKey,
  setClearFiltersKey,
  isFilterDirty,
  setIsFilterDirty,
  opencreatepost,
  setcreatepost,
  isSubmitting,
  setIsSubmitting,
  createPostFormRef,
  refetch,
}: {
  openfilter: boolean;
  setOpenfilter: React.Dispatch<React.SetStateAction<boolean>>;
  onApply: (
    filters: {
      name: string;
      startDate: string;
      endDate: string;
      type: string;
      tags?: string[];
    },
    isClear?: boolean
  ) => void;
  initialValues?: CommunityFiltersData;
  clearFiltersKey: number;
  setClearFiltersKey: React.Dispatch<React.SetStateAction<number>>;
  isFilterDirty: boolean;
  setIsFilterDirty: (dirty: boolean) => void;
  opencreatepost: boolean;
  setcreatepost: React.Dispatch<React.SetStateAction<boolean>>;
  isSubmitting: boolean;
  setIsSubmitting: (loading: boolean) => void;
  createPostFormRef: React.RefObject<HTMLFormElement | null>;
  refetch: () => void;
}) {
  const { t } = useTranslation();

  return (
    <>
      <Modal
        open={openfilter}
        onClose={() => setOpenfilter(false)}
        title={t("COMMON.FILTER")}
        compactFilter
        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={() => {
                const form = document.querySelector("form") as HTMLFormElement;
                if (form) form.requestSubmit();
              }}
              disabled={!isFilterDirty}
            >
              {t("COMMON.APPLY")}
            </Button>{" "}
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => {
                onApply(
                  {
                    name: "",
                    startDate: "",
                    endDate: "",
                    type: "",
                    tags: [],
                  },
                  true
                );
                setClearFiltersKey((prev) => prev + 1);
              }}
            >
              {t("COMMON.CLEAR")}
            </Button>{" "}
          </div>
        }
      >
        <CommunityFilterModal
          key={clearFiltersKey}
          onApply={onApply}
          initialValues={initialValues}
          onDirtyChange={setIsFilterDirty}
        />
      </Modal>

      <Modal
        open={opencreatepost}
        onClose={() => setcreatepost(false)}
        title={t("COMMON.CREATE_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"
              // The modal's own form, not the first <form> on the page — the
              // search bar behind the modal can be that one.
              onClick={() => createPostFormRef.current?.requestSubmit()}
              disabled={isSubmitting}
              loading={isSubmitting}
            >
              {t("COMMON.SUBMIT")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setcreatepost(false)}
              disabled={isSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>{" "}
          </div>
        }
      >
        <CreatePostModal
          onLoadingChange={(loading) => setIsSubmitting(loading)}
          formRef={createPostFormRef}
          afterPostCreation={() => {
            setcreatepost(false);
          }}
          refetch={refetch}
        />
      </Modal>
    </>
  );
}
