package com.cameleer.server.app.analytics;

import com.cameleer.server.app.storage.ClickHouseUsageTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

public class UsageFlushScheduler {

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

    private final ClickHouseUsageTracker tracker;

    public UsageFlushScheduler(ClickHouseUsageTracker tracker) {
        this.tracker = tracker;
    }

    @Scheduled(fixedDelayString = "${cameleer.usage.flush-interval-ms:5000}")
    public void flush() {
        try {
            tracker.flush();
        } catch (Exception e) {
            log.warn("Usage event flush failed: {}", e.getMessage());
        }
    }
}
