package com.discordrules;

import java.time.Instant;

public class RulesCache {
    private static String cachedRules = null;
    private static Instant lastFetchTime = Instant.MIN;
    private static final String FALLBACK_RULES =
        "Server Rules\n1. Be respectful to all players.\n2. No griefing or cheating.\n3. Follow staff instructions.\nPlease check Discord for the full rules list.";

    public static boolean isCacheValid() {
        if (cachedRules == null) return false;
        long cacheMinutes = ServerRulesConfig.CACHE_MINUTES.get();
        return Instant.now().isBefore(lastFetchTime.plusSeconds(cacheMinutes * 60));
    }

    public static void updateCache(String newRules) {
        if (newRules != null && !newRules.trim().isEmpty()) {
            cachedRules = newRules;
            lastFetchTime = Instant.now();
        }
    }

    public static String getRules() { return cachedRules != null ? cachedRules : FALLBACK_RULES; }
    public static void invalidate() { cachedRules = null; lastFetchTime = Instant.MIN; }
}
