"use client";

import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/Button";
import { Modal } from "@/components/ui/Modal";
import EventTimeSlotModal from "@/features/events/components/EventTimeSlotModal";
import EventCreateTimingModal from "@/features/events/components/EventCreateTimingModal";
import type { LearnServeFormValues } from "./learnServeFormTypes";

interface LearnServeScheduleModalsProps {
  values: LearnServeFormValues;

  /** The per-day timings editor. */
  open: boolean;
  onCloseTimeSlotModal: () => void;
  setOpen: React.Dispatch<React.SetStateAction<boolean>>;

  /** The "this changes the schedule — rebuild the timings?" prompt. */
  showConfirmModal: boolean;
  onConfirmAction: () => void;

  /**
   * The add-timing dialog, reachable from inside the timings editor.
   *
   * Only shown *before* the opportunity exists — once it does, the page-level
   * `LearnServeFormModals` copy takes over, because timings then attach to a
   * real record.
   */
  opentime: boolean;
  hasOpportunityId: boolean;
  onCloseCreateTiming: () => void;

  onDismissConfirm: () => void;

  /** True while a save is in flight, so the editor cannot be reopened mid-save. */
  disableSaveButton: boolean;
}

/**
 * The three dialogs raised from inside the form's own render tree, all about
 * the opportunity's schedule.
 *
 * Distinct from `LearnServeFormModals`, which sits outside `<Formik>` and is
 * driven by page state: these read the form's current values, so they have to
 * live where those values are in scope.
 */
export default function LearnServeScheduleModals({
  values,
  open,
  onCloseTimeSlotModal,
  setOpen,
  showConfirmModal,
  onConfirmAction,
  onDismissConfirm,
  opentime,
  hasOpportunityId,
  onCloseCreateTiming,
  disableSaveButton,
}: LearnServeScheduleModalsProps) {
  const { t } = useTranslation();

  return (
    <>
        <Modal
          open={open}
          onClose={onCloseTimeSlotModal}
          title={t("COMMON.TIMINGS")}
          size="md"
        >
          <EventTimeSlotModal
            startDate={values.startDate}
            disableSaveButton={disableSaveButton}
            endDate={values.endDate}
            startTime={values.startTime}
            endTime={values.endTime}
            participantsNeeded={Number(values.participantsNeeded)}
            setOpen={setOpen}
          />
        </Modal>

        <Modal
          open={opentime && !hasOpportunityId}
          onClose={onCloseCreateTiming}
          title={t("COMMON.CREATE_TIMING")}
          size="md"
        >
          <EventCreateTimingModal
            setOpenForm={onCloseCreateTiming}
            startDate={values.startDate}
            endDate={values.endDate}
            startTime={values.startTime}
            endTime={values.endTime}
            participantsNeeded={Number(values.participantsNeeded)}
          />
        </Modal>

        <Modal
          open={showConfirmModal}
          onClose={onDismissConfirm}
          title={t("COMMON.PROCEED_WITHOUT_SAVING_TITLE")}
          size="sm"
          footer={
            <div className="flex xss:flex-col justify-center w-full gap-5">
              <Button
                variant="primary"
                size="medium"
                className="xss:!w-full"
                onClick={onConfirmAction}
              >
                {t("COMMON.CONFIRM")}
              </Button>
              <Button
                variant="secondary"
                size="medium"
                className="xss:!w-full"
                onClick={onDismissConfirm}
              >
                {t("COMMON.CANCEL")}
              </Button>
            </div>
          }
        >
          <h2 className="text-center pb-10 text-lg">
            {t("COMMON.PROCEED_WITHOUT_SAVING")}
          </h2>
        </Modal>
    </>
  );
}
