import log from "../config/log";
import moment from "moment";

const getScheduledDate = (
  frequency: number,
  sendingTime: string
) => {
  const now = moment();
  const [hours, minutes, seconds = "0"] = sendingTime.split(":");

  const scheduledTime = moment()
    .hours(Number(hours))
    .minutes(Number(minutes))
    .seconds(Number(seconds))
    .milliseconds(0);

  switch (frequency) {
    case 1: {
      // Daily
      if (scheduledTime.isSameOrBefore(now)) {
        now.add(1, "day");
      }
      return now.format("YYYY-MM-DD");
    }

    case 2: {
      // // Weekly: next occurrence, 7 days after today if time passed
      // if (scheduledTime.isSameOrBefore(now)) {
      //   now.add(7, "days");
      // }
      // return now.format("YYYY-MM-DD");
      // Weekly: Sunday at sendingTime
      const time = {
        hour: Number(hours),
        minute: Number(minutes),
        second: Number(seconds),
        millisecond: 0,
      };
      const scheduledTime = now.clone().endOf("isoWeek").set(time);

      if (scheduledTime.isSameOrBefore(now)) {
        scheduledTime.add(1, "week");
      }

      return scheduledTime.format("YYYY-MM-DD");
    }

    case 3: {
      // Monthly: last day of this month, or next month if time passed
      const endOfMonth = now.clone().endOf("month");

      if (
        endOfMonth.clone().set({
          hour: Number(hours),
          minute: Number(minutes),
          second: Number(seconds),
          millisecond: 0,
        }).isSameOrBefore(now)
      ) {
        endOfMonth.add(1, "month").endOf("month");
      }

      return endOfMonth.format("YYYY-MM-DD");
    }

    default:
      throw new Error("Invalid frequency");
  }
};

export const getNextEligibleDate = (
  frequency: number,
  sendingTime: string,
  scheduledDate?: string | null
): string => {
  const now = moment();
  const [hours, minutes, seconds = 0] = sendingTime.split(":").map(Number);

  const setSendingTime = (date: moment.Moment) =>
    date.clone().set({
      hour: hours,
      minute: minutes,
      second: seconds,
      millisecond: 0,
    });

  switch (frequency) {
    case 1: {
      // Daily
      let nextDate = setSendingTime(now);

      if (nextDate.isSameOrBefore(now)) {
        nextDate.add(1, "day");
      }

      return nextDate.format("YYYY-MM-DD");
    }

    case 2: {
      // // Weekly: preserve the original scheduled weekday
      // const targetDay = scheduledDate
      //   ? moment(scheduledDate).day()
      //   : now.day();

      // let nextDate = now.clone().day(targetDay);
      // nextDate = setSendingTime(nextDate);

      // if (nextDate.isSameOrBefore(now)) {
      //   nextDate.add(1, "week");
      // }

      // return nextDate.format("YYYY-MM-DD");

      // Weekly: use scheduledDate if provided; otherwise current week-end
      let nextDate = scheduledDate
        ? moment(scheduledDate, "YYYY-MM-DD", true)
        : now.clone().endOf("isoWeek");

      if (!nextDate.isValid()) {
        throw new Error("Invalid scheduled date");
      }

      nextDate = setSendingTime(nextDate);

      if (nextDate.isSameOrBefore(now)) {
        nextDate = setSendingTime(
          nextDate.clone().add(1, "week").endOf("isoWeek")
        );
      }

      return nextDate.format("YYYY-MM-DD");

    }

    case 3: {
      // Monthly: last day of the month
      let nextDate = setSendingTime(now.clone().endOf("month"));

      if (nextDate.isSameOrBefore(now)) {
        nextDate = setSendingTime(
          now.clone().add(1, "month").endOf("month")
        );
      }

      return nextDate.format("YYYY-MM-DD");
    }

    default:
      throw new Error("Invalid frequency");
  }
};

export default getScheduledDate;
