import type { EntityManager } from '@mikro-orm/core';
import { Schedule } from '../modules/restaurants/entities/schedule.entity';
import type { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
import { timeSlots } from './data/schedules.data';

export class SchedulesSeeder {
  async run(em: EntityManager, restaurants: Restaurant[]): Promise<void> {
    for (const restaurant of restaurants) {
      if (!restaurant) continue;

      // Create schedules for each day (0 = Sunday, 6 = Saturday)
      for (let weekDay = 0; weekDay <= 6; weekDay++) {
        // Create 3 schedules per day
        for (const timeSlot of timeSlots) {
          const existing = await em.findOne(Schedule, {
            restId: restaurant.id,
            weekDay,
            openTime: timeSlot.openTime,
            closeTime: timeSlot.closeTime,
          });

          if (!existing) {
            const schedule = em.create(Schedule, {
              restId: restaurant.id,
              weekDay,
              openTime: timeSlot.openTime,
              closeTime: timeSlot.closeTime,
              isActive: true,
            });
            em.persist(schedule);
          }
        }
      }
    }

    await em.flush();
  }
}
