package com.livingworld.climate;

import com.livingworld.regions.RegionCoordinate;
import java.util.HashSet;
import java.util.Set;

/**
 * An active multi-region climate event (drought, wildfire, or flood).
 * Events spread to adjacent regions over time and resolve when conditions improve.
 */
public final class ClimateEvent {

    private final ClimateEventType type;
    private final RegionCoordinate epicenter;
    private final long startTick;
    private double severity;
    private final Set<RegionCoordinate> affectedRegions = new HashSet<>();
    private boolean resolved;
    private int ticksActive;

    public ClimateEvent(ClimateEventType type, RegionCoordinate epicenter, long startTick, double severity) {
        this.type      = type;
        this.epicenter = epicenter;
        this.startTick = startTick;
        this.severity  = severity;
        this.resolved  = false;
        this.ticksActive = 0;
        this.affectedRegions.add(epicenter);
    }

    public ClimateEventType getType()                        { return type; }
    public RegionCoordinate getEpicenter()                   { return epicenter; }
    public long getStartTick()                               { return startTick; }
    public double getSeverity()                              { return severity; }
    public Set<RegionCoordinate> getAffectedRegions()        { return affectedRegions; }
    public boolean isResolved()                              { return resolved; }
    public int getTicksActive()                              { return ticksActive; }

    public void setSeverity(double severity)                 { this.severity = Math.max(0, Math.min(1, severity)); }
    public void resolve()                                    { this.resolved = true; }
    public void addAffectedRegion(RegionCoordinate coord)    { affectedRegions.add(coord); }
    public void incrementTick()                              { ticksActive++; }

    @Override
    public String toString() {
        return type.displayName() + " at " + epicenter + " (tick " + ticksActive
                + ", severity=" + String.format("%.2f", severity)
                + ", regions=" + affectedRegions.size() + ")";
    }
}
