// // utils/slotGenerator.js
// const generateSlots = ({
//   start,
//   end,
//   lunchStart,
//   lunchEnd,
//   duration,
// }) => {
//   const slots = [];

//   const toMinutes = (time) => {
//     const [h, m] = time.split(":").map(Number);
//     return h * 60 + m;
//   };

//   let current = toMinutes(start);
//   const endMinutes = toMinutes(end);
//   const lunchStartMin = toMinutes(lunchStart);
//   const lunchEndMin = toMinutes(lunchEnd);

//   while (current + duration <= endMinutes) {
//     // skip lunch
//     if (
//       current >= lunchStartMin &&
//       current < lunchEndMin
//     ) {
//       current = lunchEndMin;
//       continue;
//     }

//     const startH = String(Math.floor(current / 60)).padStart(2, "0");
//     const startM = String(current % 60).padStart(2, "0");

//     const endSlot = current + duration;
//     const endH = String(Math.floor(endSlot / 60)).padStart(2, "0");
//     const endM = String(endSlot % 60).padStart(2, "0");

//     slots.push({
//       startTime: `${startH}:${startM}`,
//       endTime: `${endH}:${endM}`,
//     });

//     current = endSlot;
//   }

//   return slots;
// };

// module.exports = generateSlots;


// const generateSlots = ({
//   start,
//   end,
//   lunchStart,
//   lunchEnd,
//   duration,
// }) => {
//   const slots = [];

//   const toMinutes = (time) => {
//     const [h, m] = time.split(":").map(Number);
//     return h * 60 + m;
//   };

//   let current = toMinutes(start);
//   const endMinutes = toMinutes(end);

//   const hasLunch = lunchStart && lunchEnd;
//   const lunchStartMin = hasLunch ? toMinutes(lunchStart) : null;
//   const lunchEndMin = hasLunch ? toMinutes(lunchEnd) : null;

//   while (current + duration <= endMinutes) {
//     if (
//       hasLunch &&
//       current >= lunchStartMin &&
//       current < lunchEndMin
//     ) {
//       current = lunchEndMin;
//       continue;
//     }

//     const startH = String(Math.floor(current / 60)).padStart(2, "0");
//     const startM = String(current % 60).padStart(2, "0");

//     const endSlot = current + duration;
//     const endH = String(Math.floor(endSlot / 60)).padStart(2, "0");
//     const endM = String(endSlot % 60).padStart(2, "0");

//     slots.push({
//       startTime: `${startH}:${startM}`,
//       endTime: `${endH}:${endM}`,
//     });

//     current = endSlot;
//   }

//   return slots;
// };
// const generateSlots = ({
//   start,
//   end,
//   lunchStart,
//   lunchEnd,
//   duration,
// }) => {
//   const slots = [];

//   const toMinutes = (time) => {
//     const [h, m] = time.split(":").map(Number);
//     return h * 60 + m;
//   };

//   const toHHMM = (mins) => {
//     const h = String(Math.floor(mins / 60)).padStart(2, "0");
//     const m = String(mins % 60).padStart(2, "0");
//     return `${h}:${m}`;
//   };

//   const startMin = toMinutes(start);
//   const endMin = toMinutes(end);

//   const lunchStartMin = lunchStart ? toMinutes(lunchStart) : null;
//   const lunchEndMin = lunchEnd ? toMinutes(lunchEnd) : null;

//   let index = 0;

//   while (true) {
//     const slotStart = startMin + index * duration;
//     const slotEnd = slotStart + duration;

//     if (slotEnd > endMin) break;

//     // STRICT LUNCH RULE
//     if (
//       lunchStartMin &&
//       !(slotEnd <= lunchStartMin || slotStart >= lunchEndMin)
//     ) {
//       index++;
//       continue;
//     }

//     slots.push({
//       startTime: toHHMM(slotStart),
//       endTime: toHHMM(slotEnd),
//     });

//     index++;
//   }

//   return slots;
// };


const generateSlots = ({
  start,
  end,
  lunchStart,
  lunchEnd,
  duration,
}) => {
  const slots = [];

  const toMin = (t) => {
    const [h, m] = t.split(":").map(Number);
    return h * 60 + m;
  };

  const toHHMM = (m) => {
    const h = String(Math.floor(m / 60)).padStart(2, "0");
    const mm = String(m % 60).padStart(2, "0");
    return `${h}:${mm}`;
  };

  let current = toMin(start);
  const endMin = toMin(end);
  const lunchS = lunchStart ? toMin(lunchStart) : null;
  const lunchE = lunchEnd ? toMin(lunchEnd) : null;

  while (current + duration <= endMin) {
    const slotStart = current;
    const slotEnd = current + duration;

    //  if slot overlaps lunch, jump to lunch end
    if (lunchS && lunchE) {
      if (slotStart < lunchE && slotEnd > lunchS) {
        current = lunchE;   // 🔥 IMPORTANT FIX
        continue;
      }
    }

    slots.push({
      startTime: toHHMM(slotStart),
      endTime: toHHMM(slotEnd),
      isBooked: false,
      bookedBy: null,
    });

    current += duration;
  }

  return slots;
};





module.exports = generateSlots;