package com.ffii.fpsms.modules.pickOrder.scheduler

import com.ffii.fpsms.modules.pickOrder.service.TruckLaneScheduleService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component

@Component
class TruckLaneScheduleScheduler(
    private val truckLaneScheduleService: TruckLaneScheduleService,
    @Value("\${truck.lane.schedule.enabled:true}") private val enabled: Boolean,
) {
    private val logger = LoggerFactory.getLogger(javaClass)

    @Scheduled(cron = "\${truck.lane.schedule.cron:0 * * * * *}")
    fun applyDueSchedules() {
        if (!enabled) return
        try {
            truckLaneScheduleService.applyDueSchedules()
        } catch (e: Exception) {
            logger.error("Truck lane schedule tick failed", e)
        }
    }

    @Scheduled(cron = "\${truck.lane.schedule.reaper.cron:0 */5 * * * *}")
    fun reaperStaleApplying() {
        if (!enabled) return
        try {
            truckLaneScheduleService.reaperStaleApplying()
        } catch (e: Exception) {
            logger.error("Truck lane schedule reaper failed", e)
        }
    }
}
