package party.cybsec.griefdetect.core;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import party.cybsec.griefdetect.GriefDetectPlugin;
import party.cybsec.griefdetect.config.PluginConfig;

import java.util.List;

/**
 * Chat alert manager for sending detection alerts to players with permission.
 * Permission-gated and configurable with clickable teleport commands.
 */
public class ChatAlertManager {
    
    private final GriefDetectPlugin plugin;
    private final PluginConfig config;
    private final PermissionManager permissionManager;
    
    // Configuration
    private boolean enabled;
    private String format;
    
    public ChatAlertManager(GriefDetectPlugin plugin) {
        this.plugin = plugin;
        this.config = plugin.getPluginConfig();
        this.permissionManager = plugin.getPermissionManager();
        
        loadConfiguration();
    }
    
    /**
     * Load configuration settings.
     */
    private void loadConfiguration() {
        enabled = config.isChatEnabled();
        format = config.getChatFormat();
    }
    
    /**
     * Send a chat alert for a detection event.
     * 
     * @param event The detection event to report
     */
    public void sendAlert(DetectionEvent event) {
        if (!enabled) {
            return;
        }
        
        // Format the alert message
        String message = formatMessage(event);
        
        // Send to all players with permission
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (permissionManager.canReceiveAlerts(player)) {
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', message));
            }
        }
        
        // Log the same formatted message to console
        plugin.getLogger().info("Chat alert sent: " + message);
    }
    
    /**
     * Format the alert message with event details.
     */
    private String formatMessage(DetectionEvent event) {
        String detectionType = event.getDetectionType();
        String worldName = event.getWorld().getName();
        int x = event.getCenter().getBlockX();
        int y = event.getCenter().getBlockY();
        int z = event.getCenter().getBlockZ();
        int confidence = (int) event.getConfidence();
        
        // Build nearest players list
        StringBuilder playersList = new StringBuilder();
        if (!event.getNearestPlayers().isEmpty()) {
            playersList.append(" [");
            for (int i = 0; i < Math.min(event.getNearestPlayers().size(), 3); i++) {
                DetectionEvent.NearestPlayerInfo player = event.getNearestPlayers().get(i);
                if (i > 0) playersList.append(", ");
                playersList.append(player.getPlayerName())
                           .append(" (").append((int) player.getDistance()).append("m)");
            }
            if (event.getNearestPlayers().size() > 3) {
                playersList.append(", ...");
            }
            playersList.append("]");
        }
        
        return String.format(format, detectionType, worldName, x, y, z, confidence) + playersList.toString();
    }
    
    /**
     * Reload configuration.
     */
    public void reload() {
        loadConfiguration();
        plugin.getLogger().info("Chat alert configuration reloaded");
    }
}