"use client";

import { useMemo } from "react";
import { FormikHelpers } from "formik";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import * as Yup from "yup";
import i18n from "@/lib/i18n/config";
import { formatDateToYYYYMMDD } from "@/lib/helpers";
import { YupNumberOnly } from "@/features/shared/schemas";
import { useTimeSlotsStore } from "@/store/timeSlotsStore";
import type { TimeSlot } from "./EventTimeSlotModal";

import { type CreateTimingProps } from "./EventCreateTimingModal";
import { parseDate as parseCalendarDay } from "@/features/opportunities/components/timeSlotDateHelpers";

// Calendar-day parsing shared with the opportunity slot editor: a bare
// `YYYY-MM-DD` is local midnight, not UTC midnight (no day shift west of UTC).
const parseDate = parseCalendarDay;

const parseTimeString = (timeString: string) => {
  if (!timeString) return { hours: 0, minutes: 0 };
  const [hours, minutes] = timeString.split(":").map(Number);
  return { hours, minutes };
};

/**
 * The timing modal's form state: the date/time bounds it validates against and
 * the add-or-update call into the time-slot store.
 */
export function useEventCreateTiming({
  setOpenForm,
  startDate,
  endDate,
  startTime,
  endTime,
  editTimeSlot = null,
  participantsNeeded,
  autoSetTimeOnClick = true,
}: CreateTimingProps) {
  const { t } = useTranslation();
  const timeSlots = useTimeSlotsStore((s) => s.timeSlots);
  const addTimeSlot = useTimeSlotsStore((s) => s.addTimeSlot);
  const updateTimeSlot = useTimeSlotsStore((s) => s.updateTimeSlot);
  const isEditing = Boolean(editTimeSlot);

  const eventStartDate = useMemo(() => parseDate(startDate), [startDate]);
  const eventEndDate = useMemo(() => parseDate(endDate), [endDate]);
  const eventStartTime = useMemo(() => parseTimeString(startTime), [startTime]);
  const eventEndTime = useMemo(() => parseTimeString(endTime), [endTime]);

  // Participants already claimed by the other slots
  const totalNeeded = useMemo(
    () =>
      timeSlots.reduce((sum, slot) => {
        // The slot being edited doesn't count against itself
        if (isEditing && editTimeSlot && slot.id === editTimeSlot.id) return sum;
        return sum + (slot.participants_needed || 0);
      }, 0),
    [timeSlots, isEditing, editTimeSlot]
  );

  const remainingParticipants = useMemo(
    () => Math.max(0, participantsNeeded - totalNeeded),
    [participantsNeeded, totalNeeded]
  );

  let initialDate: string;
  if (editTimeSlot) {
    const editSlotDate = parseDate(editTimeSlot.date);
    // Fall back to the event's start when the stored date fell outside the range
    if (
      editSlotDate &&
      eventStartDate &&
      eventEndDate &&
      editSlotDate >= eventStartDate &&
      editSlotDate <= eventEndDate
    ) {
      initialDate = formatDateToYYYYMMDD(editTimeSlot.date);
    } else {
      initialDate = editTimeSlot.event_start_date || startDate;
    }
  } else {
    initialDate = startDate || "";
  }

  const initialValues = {
    date: initialDate,
    startTime: editTimeSlot
      ? editTimeSlot.start_time.slice(0, 5)
      : autoSetTimeOnClick
        ? startTime?.slice(0, 5)
        : "",
    endTime:
      editTimeSlot && editTimeSlot.end_time
        ? editTimeSlot.end_time.slice(0, 5)
        : autoSetTimeOnClick
          ? endTime?.slice(0, 5)
          : "",
    needed: editTimeSlot ? String(editTimeSlot.participants_needed) : "",
  };

  const withinEventTimeRange = (value?: string) => {
    if (!value || !eventStartTime || !eventEndTime) return false;
    const [hour, minute] = value.split(":").map(Number);
    const total = hour * 60 + minute;
    return (
      total >= eventStartTime.hours * 60 + eventStartTime.minutes &&
      total <= eventEndTime.hours * 60 + eventEndTime.minutes
    );
  };

  const validationSchema = Yup.object({
    date: Yup.date()
      .required(() => i18n.t("COMMON.REQUIRED.FIELD"))
      .test(
        "is-within-event-date-range",
        i18n.t("COMMON.DATE_WITHIN_RANGE"),
        function (value) {
          if (!value || !eventStartDate || !eventEndDate) return false;

          const selectedDate = new Date(value);
          selectedDate.setHours(0, 0, 0, 0);

          const startDateClone = new Date(eventStartDate);
          startDateClone.setHours(0, 0, 0, 0);
          const endDateClone = new Date(eventEndDate);
          endDateClone.setHours(0, 0, 0, 0);

          return selectedDate >= startDateClone && selectedDate <= endDateClone;
        }
      ),

    startTime: Yup.string()
      .required(() => i18n.t("COMMON.REQUIRED.FIELD"))
      .test(
        "start-time-in-event-range",
        i18n.t("COMMON.START_TIME_WITHIN_RANGE"),
        withinEventTimeRange
      ),

    endTime: Yup.string()
      .required(() => i18n.t("COMMON.REQUIRED.FIELD"))
      .test(
        "end-time-in-event-range",
        i18n.t("COMMON.END_TIME_WITHIN_RANGE"),
        withinEventTimeRange
      )
      .test(
        "end-after-start",
        i18n.t("COMMON.END_TIME_AFTER_START"),
        function (value) {
          const start = this.parent.startTime;
          if (!start || !value) return false;
          const [startHour, startMinute] = start.split(":").map(Number);
          const [endHour, endMinute] = value.split(":").map(Number);
          return endHour * 60 + endMinute > startHour * 60 + startMinute;
        }
      ),

    needed: YupNumberOnly.test(
      "max-participants",
      i18n.t("COMMON.MAX_PARTICIPANTS_EXCEEDED", {
        max: remainingParticipants,
      }),
      function (value) {
        if (!value) return false;
        return parseInt(value, 10) <= remainingParticipants;
      }
    ),
  });

  const handleSubmit = async (
    values: typeof initialValues,
    { resetForm }: FormikHelpers<typeof initialValues>
  ) => {
    try {
      const formattedDate = formatDateToYYYYMMDD(values.date);
      if (!formattedDate) {
        toast.error(t("COMMON.INVALID_DATE_FORMAT"));
        return;
      }

      const timeSlot: TimeSlot = {
        id: isEditing && editTimeSlot ? editTimeSlot.id : Date.now(),
        date: formattedDate,
        start_time: `${values.startTime}:00`,
        end_time: `${values.endTime}:00`,
        participants_needed: parseInt(values.needed, 10),
        event_start_date: startDate,
        event_end_date: endDate,
        event_start_time: startTime,
        event_end_time: endTime,
        isSaved: false,
      };

      if (isEditing && editTimeSlot) {
        updateTimeSlot(timeSlot);
        toast.success(t("COMMON.TOAST.UPDATE_TIME_SLOT_SUCCESS"));
      } else {
        addTimeSlot(timeSlot);
      }

      resetForm();
      setOpenForm();
    } catch {
      toast.error(t("COMMON.TOAST.CREATE_TIME_SLOT_FAILED"));
    }
  };

  return {
    timeSlots,
    addTimeSlot,
    updateTimeSlot,
    isEditing,
    eventStartDate,
    eventEndDate,
    eventStartTime,
    eventEndTime,
    totalNeeded,
    remainingParticipants,
    initialValues,
    withinEventTimeRange,
    validationSchema,
    handleSubmit,
  };
}
