function toMinutes(value: string): number {
  const [hours, minutes] = value.split(':').map((part) => Number(part));
  return hours * 60 + minutes;
}

export function isSlotInPast(
  bookingDate: Date,
  startTime: string,
  slotDurationMinutes: number,
  now?: Date
): boolean {
  const currentTime = now ?? new Date();

  const todayStart = new Date(
    currentTime.getFullYear(),
    currentTime.getMonth(),
    currentTime.getDate()
  );

  const bookingLocalStart = new Date(
    bookingDate.getUTCFullYear(),
    bookingDate.getUTCMonth(),
    bookingDate.getUTCDate()
  );

  if (bookingLocalStart < todayStart) return true;
  if (bookingLocalStart > todayStart) return false;

  const currentMinutes = currentTime.getHours() * 60 + currentTime.getMinutes();
  const slotStartMinutes = toMinutes(startTime);
  const elapsed = currentMinutes - slotStartMinutes;

  return elapsed > slotDurationMinutes / 2;
}
