package tech.easyflow.agent.runtime.media;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定期删除 Redis 凭据已过期的 MinIO 临时图片。
 */
@Component
public class AgentMediaCleanupScheduler {

    private static final Logger LOG = LoggerFactory.getLogger(AgentMediaCleanupScheduler.class);
    private final AgentMediaService mediaService;

    /**
     * 创建清理任务。
     *
     * @param mediaService Agent 媒体服务
     */
    public AgentMediaCleanupScheduler(AgentMediaService mediaService) {
        this.mediaService = mediaService;
    }

    /**
     * 清理一批过期临时图片。
     */
    @Scheduled(fixedDelayString = "${easyflow.agent.media.cleanup-interval:10m}")
    public void cleanup() {
        try {
            int cleaned = mediaService.cleanupExpired(200);
            if (cleaned > 0) {
                LOG.info("Cleaned {} expired Agent media objects", cleaned);
            }
        } catch (Exception error) {
            LOG.error("Agent media cleanup failed", error);
        }
    }
}
