package org.dromara.mqtt;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class MqttPendingCommandCleanupListener {

    private final MqttCommandAckService mqttCommandAckService;

    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady(ApplicationReadyEvent event) {
        try {
            int clearedCount = mqttCommandAckService.clearPendingCommands();
            log.info("[MQTT] 应用启动清理待确认命令完成，清理数量: {}", clearedCount);
        } catch (RuntimeException e) {
            log.error("[MQTT] 应用启动清理待确认命令失败", e);
        }
    }
}
