package com.ffii.fpsms.modules.monitoring.scheduler

import com.ffii.fpsms.modules.monitoring.service.MonitoringRetentionService
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
@ConditionalOnProperty(prefix = "fpsms.monitoring", name = ["enabled"], havingValue = "true")
class MonitoringRetentionScheduler(
    private val monitoringRetentionService: MonitoringRetentionService,
) {
    private val log = LoggerFactory.getLogger(MonitoringRetentionScheduler::class.java)

    /** Purge device/printer connectivity events older than configured retention (default 30 days). */
    @Scheduled(cron = "\${fpsms.monitoring.retention-purge-cron:0 0 3 * * *}")
    fun purgeOldEvents() {
        try {
            monitoringRetentionService.purgeOldEvents()
        } catch (e: Exception) {
            log.error("Monitoring event retention purge failed", e)
        }
    }
}
