"use client";

import { useTranslation } from "react-i18next";
import ReopenRegistrationModal from "../ReopenRegistrationModal";
import ConfirmModal from "@/components/ui/ConfirmModal";
import { Modal } from "@/components/ui/Modal";
import DeleteOpportunityModal from "../DeleteOpportunityModal";
import MessageRegistrantsModal from "./MessageRegistrantsModal";
import type { CreatorActions } from "./useCreatorActions";

interface CreatorActionDialogsProps {
  opportunityId: string;
  actions: CreatorActions;
  /** Repost and Edit share one dialog; only the wording differs. */
  isRepostState: boolean;
  actionLabel: string;
  onChanged: () => void;
}

/**
 * The six confirmations behind the creator's icon rail.
 *
 * Every control in `CreatorActionBar` is an unlabelled glyph, so each one
 * confirms before it acts — that is what makes an unlabelled control safe. All
 * six live here rather than beside the rail so the rail reads as a list of
 * controls and this reads as a list of consequences.
 */
export default function CreatorActionDialogs({
  opportunityId,
  actions,
  isRepostState,
  actionLabel,
  onChanged,
}: CreatorActionDialogsProps) {
  const { t } = useTranslation();
  const {
    confirm,
    dismiss,
    closeMutation,
    resubmitMutation,
    certificatesMutation,
    runAndRefresh,
    sendCertificates,
    messageMutation,
    sendMessage,
    openForm,
  } = actions;

  return (
    <>
      {/* Repost only. Edit navigates straight from the rail — `isRepostState`
          is still in the condition rather than assumed, so a future caller
          that opens `"primary"` for an edit cannot silently reintroduce the
          dialog this removed. */}
      <ConfirmModal
        open={confirm === "primary" && isRepostState}
        onClose={dismiss}
        title={actionLabel}
        message={t("COMMON.ARE_YOU_SURE_REPOST_OPPORTUNITY")}
        onConfirm={openForm}
      />

      {/* Composed rather than confirmed, so it is not a `ConfirmModal`. */}
      <MessageRegistrantsModal
        open={confirm === "message"}
        onClose={dismiss}
        onSend={sendMessage}
        isPending={messageMutation.isPending}
      />

      <ConfirmModal
        open={confirm === "close"}
        onClose={dismiss}
        title={t("COMMON.CLOSE_REGISTRATION")}
        message={t("COMMON.ARE_YOU_SURE_CLOSE_REGISTRATION")}
        onConfirm={runAndRefresh(
          closeMutation,
          "COMMON.TOAST.CLOSE_REGISTRATION_SUCCESS",
          "COMMON.TOAST.CLOSE_REGISTRATION_FAILED"
        )}
        isPending={closeMutation.isPending}
      />

      <ReopenRegistrationModal id={opportunityId} open={confirm === "reopen"}
        onClose={dismiss} onChanged={onChanged} />

      <ConfirmModal
        open={confirm === "resubmit"}
        onClose={dismiss}
        title={t("COMMON.RESUBMIT_WITHOUT_EDIT")}
        message={t("COMMON.ARE_YOU_SURE_RESUBMIT")}
        onConfirm={runAndRefresh(
          resubmitMutation,
          "COMMON.TOAST.RESUBMIT_SUCCESS",
          "COMMON.TOAST.RESUBMIT_FAILED"
        )}
        isPending={resubmitMutation.isPending}
      />

      {/* The one action here that leaves the app — volunteers get mail. */}
      <ConfirmModal
        open={confirm === "certificates"}
        onClose={dismiss}
        title={t("COMMON.SEND_CERTIFICATES")}
        message={t("COMMON.ARE_YOU_SURE_SEND_CERTIFICATES")}
        onConfirm={sendCertificates}
        isPending={certificatesMutation.isPending}
      />

      <Modal
        open={confirm === "delete"}
        onClose={dismiss}
        title={t("COMMON.DELETE_OPPORTUNITY")}
        size="small"
      >
        <DeleteOpportunityModal
          navigateBackOnDelete
          opportunityId={opportunityId}
          type="volunteer"
          setOpenModal={dismiss}
          refetch={onChanged}
        />
      </Modal>
    </>
  );
}
