"use client";

import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import RegistrationsFilterModal from "./RegistrationsFilterModal";
import VolunteerRoleModal from "./VolunteerRoleModal";
import AttendanceUndoModal from "./AttendanceUndoModal";
import AddVolunteersModal from "./AddVolunteersModal";
import UnregisterVolunteerModal from "./UnregisterVolunteerModal";

interface VolunteerListModalsProps {
  opportunityId: string | undefined;

  /** The role filter. */
  filterOpen: boolean;
  setFilterOpen: (open: boolean) => void;
  filters: { roles?: number[] };
  onApplyFilters: (filters: { roles?: string[] }) => void;
  onClearFilters: () => void;

  /** The team/role editor, whose open state lives in a store. */
  roleModalState: boolean;
  onRoleModalClose: () => void;
  onCloseRoleModal: () => void;
  rolesRefetch: () => void;
  setMismatchChecker: (
    ref: { checkParticipantsMismatch: () => boolean } | null
  ) => void;
  showMismatchModal: boolean;
  setShowMismatchModal: React.Dispatch<React.SetStateAction<boolean>>;

  /** Undoing an attendance mark. */
  pendingUndo: { volunteerName: string } | null;
  onCloseUndo: () => void;
  onConfirmUndo: () => void;
  isConfirmingUndo: boolean;

  /** Adding and removing volunteers; both invalidate the whole list. */
  showVolunteersModal: boolean;
  setShowVolunteersModal: (open: boolean) => void;
  showDeleteModal: boolean;
  setShowDeleteModal: (open: boolean) => void;
  deletingVolunteerId: number | null;
  onListChanged: () => void;
}

/**
 * Every dialog the volunteering registrations screen can open.
 *
 * Adding a volunteer and unregistering one both reset the list to page one
 * rather than patching it: either changes who is on which page.
 */
export default function VolunteerListModals({
  opportunityId,
  filterOpen,
  setFilterOpen,
  filters,
  onApplyFilters,
  onClearFilters,
  roleModalState,
  onRoleModalClose,
  onCloseRoleModal,
  rolesRefetch,
  setMismatchChecker,
  showMismatchModal,
  setShowMismatchModal,
  pendingUndo,
  onCloseUndo,
  onConfirmUndo,
  isConfirmingUndo,
  showVolunteersModal,
  setShowVolunteersModal,
  showDeleteModal,
  setShowDeleteModal,
  deletingVolunteerId,
  onListChanged,
}: VolunteerListModalsProps) {
  const { t } = useTranslation();

  return (
    <>
      <RegistrationsFilterModal
        open={filterOpen}
        onClose={() => setFilterOpen(false)}
        opportunityId={opportunityId}
        currentFilters={filters}
        onApply={onApplyFilters}
        onClear={onClearFilters}
      />

      <Modal
        open={roleModalState}
        onClose={onRoleModalClose}
        title={t("COMMON.ROLE")}
        size="md"
      >
        <VolunteerRoleModal
          opportunityId={opportunityId || ""}
          dropdwonRefetch={rolesRefetch}
          setMismatchChecker={setMismatchChecker}
          showMismatchModal={showMismatchModal}
          setShowMismatchModal={setShowMismatchModal}
          onclose={onCloseRoleModal}
        />
      </Modal>

      <AttendanceUndoModal
        pendingUndo={pendingUndo}
        onClose={onCloseUndo}
        onConfirm={onConfirmUndo}
        isConfirming={isConfirmingUndo}
      />

      <AddVolunteersModal
        open={showVolunteersModal}
        onClose={() => setShowVolunteersModal(false)}
        opportunityId={opportunityId}
        onRegistered={onListChanged}
      />

      <UnregisterVolunteerModal
        open={showDeleteModal}
        onClose={() => setShowDeleteModal(false)}
        opportunityId={opportunityId}
        volunteerId={deletingVolunteerId}
        onUnregistered={onListChanged}
      />
    </>
  );
}
