/* eslint-disable @typescript-eslint/no-explicit-any */
import moment from "moment-timezone";

export type TimeSlot = {
  value: number;
  label: string;
  mainLabel: string;
  icon: string;
};

export function getPickupTimeSlots(
  serviceType: any,
  selectedDate: string | null
): TimeSlot[] {
  // Define all possible slots
  // const allSlots: TimeSlot[] = [
  //   {
  //     value: 1,
  //     label: "08:00 AM - 01:00 PM",
  //     mainLabel: "Morning",
  //     icon: "ri-sun-line",
  //   },
  //   {
  //     value: 2,
  //     label: "01:00 PM - 06:00 PM",
  //     mainLabel: "Afternoon",
  //     icon: "ri-sun-foggy-line",
  //   },
  //   {
  //     value: 3,
  //     label: "06:00 PM - 09:00 PM",
  //     mainLabel: "Night",
  //     icon: "ri-moon-line",
  //   },
  // ];
  const allSlots: TimeSlot[] = [
    {
      value: 1,
      label: "09:00 AM - 12:00 PM",
      mainLabel: "Morning",
      icon: "ri-sun-line",
    },
    {
      value: 3,
      label: "04:00 PM - 07:00 PM",
      mainLabel: "Evening",
      icon: "ri-cloudy-line"
    },
  ];

  const endTimes = {
    morning: 12,
    afternoon: 18,
    night: 19,
  };

  const timezone = process.env.NEXT_PUBLIC_TZ || "Asia/Bahrain";
  const now = moment.tz(timezone);
  const currentHour = now.hour();
  const currentDate = now.format("YYYY-MM-DD");

  const selected = selectedDate ? moment(selectedDate) : null;
  const isToday = selected && selected.format("YYYY-MM-DD") === currentDate;
  const isTomorrow =
    selected &&
    selected.format("YYYY-MM-DD") ===
      moment.tz(timezone).add(1, "days").format("YYYY-MM-DD");

  const slotValueToEndKey: Record<number, keyof typeof endTimes> = {
    1: "morning",
    2: "afternoon",
    3: "night",
  };

  function filterPastSlots(slots: TimeSlot[]) {
    return slots.filter((slot) => {
      const endKey = slotValueToEndKey[slot.value];
      const endHour = endTimes[endKey];
      if (typeof endHour !== "number") return false;
      return currentHour < endHour;
    });
  }

  if (serviceType?.name === "Same Day") {
    if (isToday) {
      // Only show slots whose end time is after now
      const available = filterPastSlots([allSlots[0]]);
      return available;
    } else if (isTomorrow) {
      return allSlots;
    } else {
      return allSlots;
    }
  }

  if (serviceType?.name === "Express") {
    if (isToday) {
      let available: TimeSlot[] = [];
      if (currentHour >= 0 && currentHour < endTimes?.morning) {
        available = [allSlots[0], allSlots[1]];
      } else if (
        currentHour >= endTimes?.morning &&
        currentHour < endTimes?.afternoon
      ) {
        available = [allSlots[1]];
      } else {
        available = [];
      }
      return filterPastSlots(available);
    } else if (isTomorrow) {
      return allSlots;
    } else {
      return allSlots;
    }
  }

  if (serviceType?.name === "Next Day") {
    if (isToday) {
      let available: TimeSlot[] = [];
      if (currentHour >= 0 && currentHour < endTimes?.night) {
        available = allSlots;
      } else {
        available = [];
      }
      return filterPastSlots(available);
    } else if (isTomorrow) {
      return allSlots;
    } else {
      return allSlots;
    }
  }

  // Default: all slots for unknown service types
  if (isToday) {
    return filterPastSlots(allSlots);
  }
  return allSlots;
}
