package de.oaa.xxx.config;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class TokenBlacklistService {

    // jti -> Ablaufzeit in ms
    private final Map<String, Long> blacklist = new ConcurrentHashMap<>();

    public void blacklist(String jti, long expiryMs) {
        blacklist.put(jti, expiryMs);
    }

    public boolean isBlacklisted(String jti) {
        Long expiry = blacklist.get(jti);
        if (expiry == null) return false;
        if (System.currentTimeMillis() > expiry) {
            blacklist.remove(jti);
            return false;
        }
        return true;
    }

    @Scheduled(fixedDelay = 3_600_000)
    public void cleanup() {
        long now = System.currentTimeMillis();
        blacklist.entrySet().removeIf(e -> now > e.getValue());
    }
}
