package com.dora.schedule;

import com.dora.config.JwtConfig;
import com.dora.service.JwtTokenManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Token清理定时任务
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TokenCleanupScheduler {
    
    private final JwtTokenManager jwtTokenManager;
    private final JwtConfig jwtConfig;
    
    /**
     * 定时清理过期的token
     * 每小时执行一次
     */
    @Scheduled(fixedRateString = "#{@jwtConfig.cleanupInterval}")
    public void cleanupExpiredTokens() {
        try {
            // 计算过期时间点（当前时间 - JWT过期时间）
            long expiredBeforeTime = System.currentTimeMillis() - jwtConfig.getExpiration();
            
            log.info("开始清理过期token，过期时间点: {}", expiredBeforeTime);
            
            jwtTokenManager.cleanupExpiredTokens(expiredBeforeTime);
            
            log.info("过期token清理完成");
            
        } catch (Exception e) {
            log.error("定时清理过期token失败", e);
        }
    }
}
