"use client";

import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import CreateVolunteerRoleModal from "./CreateVolunteerRoleModal";
import DeleteVolunteerRoleModal from "./DeleteVolunteerRoleModal";
import ParticipantsMismatchModal from "./ParticipantsMismatchModal";

interface VolunteerRoleDialogsProps {
  opportunityId: string;
  openForm: boolean;
  setOpenForm: (open: boolean) => void;
  /* Role ids are integers on `VolunteerOpportunityRoleResource`. */
  editRoleId: number | null;
  openDeleteModal: boolean;
  setOpenDeleteModal: (open: boolean) => void;
  deleteRoleId: number | null;
  refetch: () => void;
  /** Shown in the create form's heading. */
  participantsStillNeeded: number;
  dropdwonRefetch?: () => void;
  showMismatchModal: boolean;
  setShowMismatchModal: (open: boolean) => void;
  onclose: () => void;
  stopNavigationBlock: () => Promise<void>;
}

/**
 * The three dialogs this editor can open: add/edit a role, confirm a deletion,
 * and the participants-mismatch warning that blocks leaving.
 */
export default function VolunteerRoleDialogs({
  opportunityId,
  openForm,
  setOpenForm,
  editRoleId,
  openDeleteModal,
  setOpenDeleteModal,
  deleteRoleId,
  refetch,
  participantsStillNeeded,
  dropdwonRefetch,
  showMismatchModal,
  setShowMismatchModal,
  onclose,
  stopNavigationBlock,
}: VolunteerRoleDialogsProps) {
  const { t } = useTranslation();

  return (
    <>
      <Modal
        open={openForm}
        onClose={() => setOpenForm(false)}
        title={t(editRoleId ? "COMMON.EDIT.ROLE" : "COMMON.ADD.ROLE")}
        size="md"
      >
        <CreateVolunteerRoleModal
          setOpenForm={() => setOpenForm(false)}
          oppurtunityId={opportunityId}
          refetch={refetch}
          roleId={editRoleId}
          dropdownRefetch={dropdwonRefetch}
          participantsStillNeeded={participantsStillNeeded}
        />
      </Modal>

      <Modal
        size="small"
        open={openDeleteModal}
        onClose={() => setOpenDeleteModal(false)}
        title={t("COMMON.DELETE_CONFIRMATION_ROLE")}
      >
        <DeleteVolunteerRoleModal
          setOpenForm={() => setOpenDeleteModal(false)}
          refetch={refetch}
          roleId={deleteRoleId}
          dropdownRefetch={dropdwonRefetch}
        />
      </Modal>

      <Modal
        size="small"
        open={showMismatchModal}
        onClose={() => setShowMismatchModal?.(false)}
        title={t("COMMON.PARTICIPANTS_MISMATCH")}
      >
        <ParticipantsMismatchModal
          onClose={() => setShowMismatchModal?.(false)}
          id={Number(opportunityId)}
          roleModalClose={onclose}
          refetch={refetch}
          stopNavigationBlock={stopNavigationBlock}
        />
      </Modal>
    </>
  );
}
