"use client";

import { useTranslation } from "react-i18next";
import { Modal } from "@/components/ui/Modal";
import DeleteOpportunityModal from "@/features/opportunities/components/DeleteOpportunityModal";
import { type OpportunityData } from "./profileVolunteerCardTypes";

/** The state the confirm dialog is driven by. */
export interface DeleteOpportunityInfo {
  isOpen: boolean;
  opportunityId?: string;
  /* Matches the dialog's own prop. Both titles are nullable columns, so
     "present" does not mean "non-null" — and the dialog never renders them
     anyway (see `DeleteOpportunityModal`). */
  opportunityTitle?: { title_en: string | null; title_ar: string | null };
}

/**
 * Confirm-delete for an opportunity in a profile's list.
 *
 * Which endpoint the delete hits depends on the opportunity's model, so the
 * type is resolved from the list rather than carried in the dialog's state.
 */
export default function ProfileDeleteOpportunityModal({
  deleteModalInfo,
  setDeleteModalInfo,
  opportunities,
  refetch,
}: {
  deleteModalInfo: DeleteOpportunityInfo;
  setDeleteModalInfo: (info: DeleteOpportunityInfo) => void;
  opportunities: OpportunityData[];
  refetch: () => void;
}) {
  const { t } = useTranslation();

  if (!deleteModalInfo.isOpen) return null;

  return (
      <Modal
        title={t("COMMON.DELETE_OPPORTUNITY")}
        open={deleteModalInfo.isOpen}
        onClose={() => setDeleteModalInfo({ isOpen: false })}
        size="small"
        position="default"
      >
        <DeleteOpportunityModal
          opportunityId={deleteModalInfo.opportunityId!}
          type={(() => {
            const opp = opportunities.find(
              (o) => String(o.id) === deleteModalInfo.opportunityId
            );
            return opp?.opportunity_type === "learn_serve_opportunity"
              ? "learnserve"
              : "volunteer";
          })()}
          setOpenModal={() => setDeleteModalInfo({ isOpen: false })}
          refetch={() => {
            refetch();
          }}
          opportunityTitle={deleteModalInfo.opportunityTitle}
        />
      </Modal>
  );
}
