package com.livingworld.core.simulation;

import com.livingworld.regions.Region;
import com.livingworld.regions.RegionFlags;
import com.livingworld.regions.RegionMetrics;

/**
 * Utility class for computing update priority scores based on region ecosystem state.
 * 
 * <p>This calculator uses a deterministic formula that considers player activity,
 * ecosystem events, pollution, soil quality, resource depletion, and recovery pressure.</p>
 */
public final class RegionPriorityCalculator {

    private static final int BASE_PRIORITY = 5;
    private static final int PLAYER_ACTIVITY_BONUS = 100;
    private static final int ACTIVE_ECOSYSTEM_EVENT_BONUS = 75;
    private static final int HIGH_POLLUTION_BONUS = 50;
    private static final int LOW_SOIL_QUALITY_BONUS = 50;

    /**
     * Calculates the update priority for a region based on its ecosystem state.
     * 
     * <p>Priority score formula:
     *   base = 5
     *   + hasPlayerActivity ? 100 : 0
     *   + hasActiveEcosystemEvent ? 75 : 0
     *   + isHighPollution ? 50 : 0
     *   + isLowSoilQuality ? 50 : 0
     *   + pollutionScore / 2
     *   + resourceDepletion / 3
     *   + recoveryPressure / 4</p>
     * 
     * @param region the region to calculate priority for (must not be null)
     * @return the computed integer priority score
     */
    public static int calculatePriority(Region region) {
        if (region == null) {
            throw new IllegalArgumentException("region must not be null");
        }

        RegionMetrics metrics = region.getMetrics();
        RegionFlags flags = region.getFlags();

        int priority = BASE_PRIORITY;

        // Discrete bonuses for flagged conditions
        if (flags.isHasPlayerActivity()) {
            priority += PLAYER_ACTIVITY_BONUS;
        }
        if (flags.isHasActiveEcosystemEvent()) {
            priority += ACTIVE_ECOSYSTEM_EVENT_BONUS;
        }
        if (flags.isHasHighPollution()) {
            priority += HIGH_POLLUTION_BONUS;
        }
        if (flags.isHasLowSoilQuality()) {
            priority += LOW_SOIL_QUALITY_BONUS;
        }

        // Continuous bonuses from metrics
        priority += (int) (metrics.getPollutionScore() / 2.0);
        priority += (int) (metrics.getResourceDepletion() / 3.0);
        priority += (int) (metrics.getRecoveryPressure() / 4.0);

        return priority;
    }

    /**
     * Private constructor to prevent instantiation.
     */
    private RegionPriorityCalculator() {
        throw new UnsupportedOperationException("Utility class cannot be instantiated");
    }
}
