package de.assecutor.votianlt.messaging.delivery;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Scheduled tasks for message delivery retry and cleanup.
 */
@Component
@Slf4j
public class RetryScheduler {

    private final MessageDeliveryService deliveryService;

    public RetryScheduler(MessageDeliveryService deliveryService) {
        this.deliveryService = deliveryService;
    }

    /**
     * Retry pending deliveries every 30 seconds (configurable)
     */
    @Scheduled(fixedDelayString = "${app.messaging.delivery.retry-interval-seconds:30}000")
    public void retryPendingDeliveries() {
        try {
            log.debug("[RetryScheduler] Starting retry task");
            deliveryService.retryPendingDeliveries();
        } catch (Exception e) {
            log.error("[RetryScheduler] Error in retry task: {}", e.getMessage(), e);
        }
    }

    /**
     * Cleanup old deliveries every hour (configurable)
     */
    @Scheduled(fixedDelayString = "${app.messaging.delivery.cleanup-interval-minutes:60}000")
    public void cleanupOldDeliveries() {
        try {
            log.debug("[RetryScheduler] Starting cleanup task");
            deliveryService.cleanupOldDeliveries();
        } catch (Exception e) {
            log.error("[RetryScheduler] Error in cleanup task: {}", e.getMessage(), e);
        }
    }
}

