package com.discordrules;

import net.minecraft.util.text.StringTextComponent;
import net.minecraft.entity.player.ServerPlayerEntity;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

public class CooldownManager {
    private static final ConcurrentHashMap<UUID, Instant> COOLDOWNS = new ConcurrentHashMap<>();

    public static boolean checkAndUpdateCooldown(ServerPlayerEntity player) {
        UUID playerId = player.getUUID();
        Instant now = Instant.now();
        int cooldownSeconds = ServerRulesConfig.COOLDOWN_SECONDS.get();
        Instant lastUsed = COOLDOWNS.get(playerId);
        if (lastUsed != null) {
            long secondsSinceLastUse = Duration.between(lastUsed, now).getSeconds();
            if (secondsSinceLastUse < cooldownSeconds) {
                long remaining = cooldownSeconds - secondsSinceLastUse;
                player.sendMessage(new StringTextComponent("\u00a7cPlease wait " + remaining + " seconds before checking the rules again."), player.getUUID());
                return false;
            }
        }
        COOLDOWNS.put(playerId, now);
        return true;
    }

    public static void removePlayer(UUID playerId) { COOLDOWNS.remove(playerId); }
}
