"use client";

import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import { Modal } from "@/components/ui/Modal";
import CreateTiming from "./CreateTiming";
import UpdateLearnServeConfirmModal from "./UpdateLearnServeConfirmModal";
import type { OpportunityDetails } from "./learnServeFormTypes";

interface LearnServeFormModalsProps {
  /** Add-timings dialog, opened once the opportunity itself exists. */
  opentime: boolean;
  opportunityId: number | null;
  opportunityDetails: OpportunityDetails;
  onCloseTimeModal: () => void;
  onTimingsChanged: () => void;

  /** Editing a schedule can orphan existing slots; this confirms dropping them. */
  confirmModalOpen: boolean;
  onConfirmDeleteTimeSlots: () => void;
  onCancelDeleteTimeSlots: () => void;

  /** Final "are you sure" before an edit goes back for re-approval. */
  showUpdateConfirmModal: boolean;
  hasPendingFormData: boolean;
  onDismissUpdateConfirm: () => void;
  onUpdateConfirm: () => Promise<void>;
  opportunityTitle?: { title_en: string | null; title_ar: string | null };
}

/**
 * The three dialogs the development-opportunity form can raise, none of which
 * belong to the form's own field tree.
 *
 * They are siblings of the `<Formik>` block rather than children of it: each is
 * driven by page state, not by a field, and two of them are about the
 * opportunity *record* rather than the values being edited.
 */
export default function LearnServeFormModals({
  opentime,
  opportunityId,
  opportunityDetails,
  onCloseTimeModal,
  onTimingsChanged,
  confirmModalOpen,
  onConfirmDeleteTimeSlots,
  onCancelDeleteTimeSlots,
  showUpdateConfirmModal,
  hasPendingFormData,
  onDismissUpdateConfirm,
  onUpdateConfirm,
  opportunityTitle,
}: LearnServeFormModalsProps) {
  const { t } = useTranslation();

  return (
    <>
      <Modal
        open={opentime && Boolean(opportunityId)}
        onClose={onCloseTimeModal}
        title={t("COMMON.CREATE_TIMING")}
        size="md"
      >
        {opportunityId ? (
          <CreateTiming
            opportunityId={opportunityId}
            setOpenForm={onCloseTimeModal}
            refetch={onTimingsChanged}
            startDate={opportunityDetails.startDate}
            endDate={opportunityDetails.endDate}
            startTime={opportunityDetails.startTime}
            endTime={opportunityDetails.endTime}
          />
        ) : (
          <div className="p-4 text-center">
            {t("COMMON.NO_OPPORTUNITY_CREATED")}
          </div>
        )}
      </Modal>

      <Modal
        open={confirmModalOpen}
        onClose={onCancelDeleteTimeSlots}
        title={t("COMMON.CONFIRM_DELETE_TIMESLOTS")}
        size="sm"
        footer={
          <div className="flex justify-center w-full gap-5">
            <Button
              variant="secondary"
              size="medium"
              onClick={onCancelDeleteTimeSlots}
            >
              {t("COMMON.CANCEL")}
            </Button>
            <Button
              variant="primary"
              size="medium"
              onClick={onConfirmDeleteTimeSlots}
            >
              {t("COMMON.CONFIRM")}
            </Button>
          </div>
        }
      >
        <div className="p-4 text-center">
          {t("COMMON.CONFIRM_DELETE_TIMESLOTS_MESSAGE")}
        </div>
      </Modal>

      <Modal
        open={showUpdateConfirmModal}
        onClose={onDismissUpdateConfirm}
        title={t("COMMON.UPDATE_OPPORTUNITY")}
        size="sm"
      >
        {hasPendingFormData && (
          <UpdateLearnServeConfirmModal
            setOpenModal={onDismissUpdateConfirm}
            onConfirm={onUpdateConfirm}
            opportunityTitle={opportunityTitle}
          />
        )}
      </Modal>

    </>
  );
}
