package de.fete.application.service;

import de.fete.domain.port.out.EventRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/** Scheduled job that deletes events whose expiry date is in the past. */
@Component
public class ExpiredEventCleanupJob {

  private static final Logger log = LoggerFactory.getLogger(ExpiredEventCleanupJob.class);

  private final EventRepository eventRepository;

  /** Creates a new cleanup job with the given event repository. */
  public ExpiredEventCleanupJob(EventRepository eventRepository) {
    this.eventRepository = eventRepository;
  }

  /** Runs daily at 03:00 and deletes all expired events. */
  @Scheduled(cron = "0 0 3 * * *")
  @Transactional
  public void deleteExpiredEvents() {
    int deleted = eventRepository.deleteExpired();
    log.info("Expired event cleanup: deleted {} event(s)", deleted);
  }
}
