package party.cybsec.griefdetect.core;

import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import party.cybsec.griefdetect.GriefDetectPlugin;

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

/**
 * Permission system for GriefDetect with caching for performance.
 * Manages plugin-specific permissions and bypass capabilities.
 */
public class PermissionManager {
    
    private final GriefDetectPlugin plugin;
    
    // Permission node constants
    public static final String ADMIN = "griefdetect.admin";
    public static final String RELOAD = "griefdetect.reload";
    public static final String ALERTS = "griefdetect.alerts";
    public static final String BYPASS = "griefdetect.bypass";
    
    // Permission cache for performance
    private final Map<UUID, Map<String, Boolean>> permissionCache;
    
    public PermissionManager(GriefDetectPlugin plugin) {
        this.plugin = plugin;
        this.permissionCache = new ConcurrentHashMap<>();
    }
    
    /**
     * Check if a player has a specific permission.
     * Uses caching for performance.
     * OPs automatically have all permissions.
     * 
     * @param player The player to check
     * @param permission The permission node to check
     * @return true if player has permission, false otherwise
     */
    public boolean hasPermission(Player player, String permission) {
        // OPs have all permissions
        if (player.isOp()) {
            return true;
        }
        
        UUID playerId = player.getUniqueId();
        
        // Check cache first
        if (permissionCache.containsKey(playerId) && permissionCache.get(playerId).containsKey(permission)) {
            return permissionCache.get(playerId).get(permission);
        }
        
        // Check actual permission
        boolean hasPermission = player.hasPermission(permission);
        
        // Update cache
        permissionCache.computeIfAbsent(playerId, k -> new HashMap<>()).put(permission, hasPermission);
        
        return hasPermission;
    }
    
    /**
     * Check if a player can bypass grief detection.
     * 
     * @param player The player to check
     * @return true if player bypasses detection, false otherwise
     */
    public boolean canBypass(Player player) {
        return hasPermission(player, BYPASS);
    }
    
    /**
     * Check if a player can receive alerts.
     * 
     * @param player The player to check
     * @return true if player can receive alerts, false otherwise
     */
    public boolean canReceiveAlerts(Player player) {
        return hasPermission(player, ALERTS);
    }
    
    /**
     * Check if a player can reload the plugin.
     * 
     * @param player The player to check
     * @return true if player can reload, false otherwise
     */
    public boolean canReload(Player player) {
        return hasPermission(player, RELOAD);
    }
    
    /**
     * Check if a player is an admin.
     * 
     * @param player The player to check
     * @return true if player is admin, false otherwise
     */
    public boolean isAdmin(Player player) {
        return hasPermission(player, ADMIN);
    }
    
    /**
     * Clear permission cache for a specific player.
     * Useful when permissions change.
     * 
     * @param player The player whose cache to clear
     */
    public void clearCache(Player player) {
        permissionCache.remove(player.getUniqueId());
    }
    
    /**
     * Clear all permission cache.
     * Useful on plugin reload.
     */
    public void clearAllCache() {
        permissionCache.clear();
    }
}