"use client";

import React, { SetStateAction } from "react";
import { useTranslation } from "react-i18next";

import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";
import { TimeSlot } from "@/features/shared";
import EventCreateTimingModal from "./EventCreateTimingModal";
import EventTimeSlotTable from "./EventTimeSlotTable";
import { useEventTimeSlots } from "./useEventTimeSlots";

export type { TimeSlot };

export interface EventTimeSlotModalProps {
  startDate: string;
  endDate: string;
  startTime: string;
  endTime: string;
  participantsNeeded: number;
  setOpen: React.Dispatch<SetStateAction<boolean>>;
  disableSaveButton: boolean;
}

export default function EventTimeSlotModal({
  startDate,
  endDate,
  startTime,
  endTime,
  participantsNeeded,
  setOpen,
  disableSaveButton,
}: EventTimeSlotModalProps) {
  const { t } = useTranslation();
  const {
    opentime,
    setOpentime,
    editingTimeSlot,
    setEditingTimeSlot,
    showConfirmModal,
    setShowConfirmModal,
    confirmAction,
    setConfirmAction,
    setSlotIdToDelete,
    windowWidth,
    timeSlots,
    oppStartDate,
    oppEndDate,
    oppStartTime,
    oppEndTime,
    handleTimeSlotCreated,
    handleEditClick,
    handleDeleteClick,
    handleConfirmAction,
    groupedTimeSlots,
    isLoading,
    handleSaveTimeSlots,
  } = useEventTimeSlots({
    startDate,
    endDate,
    startTime,
    endTime,
    participantsNeeded,
    setOpen,
    disableSaveButton,
  });

  if (isLoading) {
    return <div>{t("COMMON.LOADING")}</div>;
  }

  const noTimeSlots = timeSlots.length === 0;

  return (
    <>
      <Modal
        open={opentime}
        onClose={() => {
          setOpentime(false);
          setEditingTimeSlot(null);
        }}
        title={
          editingTimeSlot
            ? t("COMMON.UPDATE_TIMING")
            : t("COMMON.CREATE_TIMING")
        }
        size="md"
      >
        <EventCreateTimingModal
          setOpenForm={handleTimeSlotCreated}
          startDate={oppStartDate}
          endDate={oppEndDate}
          startTime={oppStartTime}
          endTime={oppEndTime}
          editTimeSlot={editingTimeSlot}
          participantsNeeded={participantsNeeded}
          autoSetTimeOnClick={!editingTimeSlot}
        />
      </Modal>

      <Modal
        open={showConfirmModal}
        onClose={() => {
          setShowConfirmModal(false);
          setConfirmAction(null);
          setSlotIdToDelete(null);
        }}
        title={t("COMMON.CONFIRM_DELETE")}
        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={handleConfirmAction}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => {
                setShowConfirmModal(false);
                setConfirmAction(null);
                setSlotIdToDelete(null);
              }}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <h2 className="text-center pb-10 text-lg">
          {confirmAction === "delete"
            ? t("COMMON.CONFIRM_DELETE_TIME_SLOT")
            : t("COMMON.CONFIRM_CLEAR_TIME_SLOTS")}
        </h2>
      </Modal>

      <div className="bg-white">
        {/* Horizontally scrollable table container */}
        <div className="overflow-x-auto">
          {noTimeSlots && (
            <div className="text-center pb-5">
              <p className="text-gray-500">{t("COMMON.NO_TIME_SLOTS")}</p>
            </div>
          )}

          <EventTimeSlotTable
            groupedTimeSlots={groupedTimeSlots}
            windowWidth={windowWidth}
            onEdit={handleEditClick}
            onDelete={handleDeleteClick}
          />
        </div>

        <div className="flex xss:flex-col justify-center pt-5 xss:pt-10 w-full gap-5">
          <Button
            variant="primary"
            size="medium"
            className="xss:!w-full"
            onClick={() => setOpentime(true)}
          >
            {t("COMMON.ADD_TIMINGS")}
          </Button>
          <Button
            variant="primary"
            size="medium"
            className="xss:!w-full disabled:cursor-not-allowed disabled:opacity-50"
            onClick={handleSaveTimeSlots}
            disabled={disableSaveButton}
          >
            {t("COMMON.SAVE")}
          </Button>
        </div>
      </div>
    </>
  );
}
