package party.cybsec.griefdetect.core;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.UUID;

/**
 * Standardized detection event emitted by modules.
 * Contains all necessary information for confidence calculation and reporting.
 */
public class DetectionEvent {
    
    private final String detectionType;
    private final World world;
    private final Location center;
    private final int minX, minY, minZ;
    private final int maxX, maxY, maxZ;
    private final int blockCount;
    private final double confidence;
    private final long timestamp;
    private final List<NearestPlayerInfo> nearestPlayers;
    
    public DetectionEvent(String detectionType, World world, Location center,
                         int minX, int minY, int minZ,
                         int maxX, int maxY, int maxZ,
                         int blockCount, double confidence,
                         List<NearestPlayerInfo> nearestPlayers) {
        this.detectionType = detectionType;
        this.world = world;
        this.center = center;
        this.minX = minX;
        this.minY = minY;
        this.minZ = minZ;
        this.maxX = maxX;
        this.maxY = maxY;
        this.maxZ = maxZ;
        this.blockCount = blockCount;
        this.confidence = confidence;
        this.timestamp = System.currentTimeMillis();
        this.nearestPlayers = nearestPlayers;
    }
    
    // Getters
    public String getDetectionType() { return detectionType; }
    public World getWorld() { return world; }
    public Location getCenter() { return center; }
    public int getMinX() { return minX; }
    public int getMinY() { return minY; }
    public int getMinZ() { return minZ; }
    public int getMaxX() { return maxX; }
    public int getMaxY() { return maxY; }
    public int getMaxZ() { return maxZ; }
    public int getBlockCount() { return blockCount; }
    public double getConfidence() { return confidence; }
    public long getTimestamp() { return timestamp; }
    public List<NearestPlayerInfo> getNearestPlayers() { return nearestPlayers; }
    
    /**
     * Get the bounding box size in blocks.
     */
    public int getBoundingBoxSize() {
        return (maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1);
    }
    
    /**
     * Get the expansion radius from center to farthest point.
     */
    public double getExpansionRadius() {
        double dx = Math.max(Math.abs(center.getBlockX() - minX), Math.abs(center.getBlockX() - maxX));
        double dy = Math.max(Math.abs(center.getBlockY() - minY), Math.abs(center.getBlockY() - maxY));
        double dz = Math.max(Math.abs(center.getBlockZ() - minZ), Math.abs(center.getBlockZ() - maxZ));
        return Math.sqrt(dx * dx + dy * dy + dz * dz);
    }
    
    /**
     * Generate a teleport command for the center location.
     */
    public String getTeleportCommand() {
        return String.format("/tp %d %d %d", center.getBlockX(), center.getBlockY(), center.getBlockZ());
    }
    
    /**
     * Information about nearest players for reporting.
     */
    public static class NearestPlayerInfo {
        private final UUID playerId;
        private final String playerName;
        private final double distance;
        private final boolean isSurvival;
        
        public NearestPlayerInfo(UUID playerId, String playerName, double distance, boolean isSurvival) {
            this.playerId = playerId;
            this.playerName = playerName;
            this.distance = distance;
            this.isSurvival = isSurvival;
        }
        
        public UUID getPlayerId() { return playerId; }
        public String getPlayerName() { return playerName; }
        public double getDistance() { return distance; }
        public boolean isSurvival() { return isSurvival; }
    }
}