import i18n from "@/lib/i18n/config";
import * as Yup from "yup";

interface ClockTime {
  hours: number;
  minutes: number;
}

/**
 * A time slot must sit inside its opportunity's window.
 *
 * The rule worth reading twice is in `startTime`: **only the boundary days are
 * constrained by the opportunity's clock.** An opportunity running 09:00–17:00
 * from Monday to Friday accepts an 08:00 slot on Wednesday — the 09:00 is when
 * *Monday* starts, not a daily opening time. Applying it to every day would
 * silently reject most of the schedule.
 *
 * Validation messages follow the current interface language.
 */
export function buildTimeSlotValidationSchema({
  opportunityStartDate,
  opportunityEndDate,
  oppStartTime,
  oppEndTime,
  startTime,
  endTime,
}: {
  opportunityStartDate: Date | null;
  opportunityEndDate: Date | null;
  oppStartTime: ClockTime;
  oppEndTime: ClockTime;
  startTime?: string;
  endTime?: string;
}) {
  return Yup.object({
    date: Yup.date()
      .required(() => i18n.t("COMMON.REQUIRED.FIELD"))
      .test(
        "is-within-opportunity-range",
        () => i18n.t("COMMON.DATE_WITHIN_RANGE"),
        function (value) {
          if (!value || !opportunityStartDate || !opportunityEndDate) {
            return false;
          }

          const selectedDate = new Date(value);
          selectedDate.setHours(0, 0, 0, 0);
          const startDateClone = new Date(opportunityStartDate);
          startDateClone.setHours(0, 0, 0, 0);
          const endDateClone = new Date(opportunityEndDate);
          endDateClone.setHours(0, 0, 0, 0);

          return selectedDate >= startDateClone && selectedDate <= endDateClone;
        }
      ),
    startTime: Yup.string()
      .required(() => i18n.t("COMMON.REQUIRED.FIELD"))
      .test(
        "is-within-opportunity-time-range",
        () => i18n.t("COMMON.START_TIME_WITHIN_RANGE"),
        function (value) {
          if (!value || !startTime || !endTime) return false;

          const selectedDate = this.parent.date
            ? new Date(this.parent.date)
            : null;
          if (!selectedDate || !opportunityStartDate || !opportunityEndDate) {
            return false;
          }

          const [hours, minutes] = value.split(":").map(Number);
          const totalMinutesSelected = hours * 60 + minutes;

          // Only the boundary days are constrained by the opportunity's clock
          if (
            selectedDate.toDateString() === opportunityStartDate.toDateString()
          ) {
            return (
              totalMinutesSelected >=
              oppStartTime.hours * 60 + oppStartTime.minutes
            );
          }

          if (
            selectedDate.toDateString() === opportunityEndDate.toDateString()
          ) {
            return (
              totalMinutesSelected <=
              oppEndTime.hours * 60 + oppEndTime.minutes
            );
          }

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