package com.livingworld.modules.vegetation;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class VegetationRegionDataTest {

    @Test
    void defaultsAreHealthyMixedVegetation() {
        VegetationRegionData d = VegetationRegionData.defaults();
        assertEquals(50.0, d.getGrassPressure(),  1e-9);
        assertEquals(30.0, d.getFlowerPressure(), 1e-9);
        assertEquals(30.0, d.getShrubPressure(),  1e-9);
        assertEquals(40.0, d.getTreePressure(),   1e-9);
        assertEquals(5.0,  d.getDeadVegetation(), 1e-9);
    }

    @Test
    void reduceFromLoggingDecreasesTressAndShrubs() {
        VegetationRegionData d = VegetationRegionData.defaults();
        d.reduceFromLogging(20.0);
        assertTrue(d.getTreePressure()   < 40.0, "tree pressure should drop");
        assertTrue(d.getShrubPressure()  < 30.0, "shrub pressure should drop");
        assertTrue(d.getDeadVegetation() > 5.0,  "dead vegetation should increase");
    }

    @Test
    void reduceFromLoggingByZeroChangesNothing() {
        VegetationRegionData d = VegetationRegionData.defaults();
        d.reduceFromLogging(0.0);
        assertEquals(40.0, d.getTreePressure(),   1e-9);
        assertEquals(30.0, d.getShrubPressure(),  1e-9);
        assertEquals(5.0,  d.getDeadVegetation(), 1e-9);
    }

    @Test
    void reduceFromLoggingNegativeThrows() {
        assertThrows(IllegalArgumentException.class,
                () -> VegetationRegionData.defaults().reduceFromLogging(-1.0));
    }

    @Test
    void recoverIncreasesAllLivingPressures() {
        VegetationRegionData d = new VegetationRegionData(10.0, 5.0, 5.0, 5.0, 30.0);
        d.recover(10.0);
        assertTrue(d.getGrassPressure()  > 10.0, "grass should increase");
        assertTrue(d.getFlowerPressure() > 5.0,  "flowers should increase");
        assertTrue(d.getShrubPressure()  > 5.0,  "shrubs should increase");
        assertTrue(d.getTreePressure()   > 5.0,  "trees should increase");
        assertTrue(d.getDeadVegetation() < 30.0, "dead vegetation should decrease");
    }

    @Test
    void recoverNegativeThrows() {
        assertThrows(IllegalArgumentException.class,
                () -> VegetationRegionData.defaults().recover(-1.0));
    }

    @Test
    void constructorClampsAbove100() {
        VegetationRegionData d = new VegetationRegionData(200.0, 200.0, 200.0, 200.0, 200.0);
        assertEquals(100.0, d.getGrassPressure(),  1e-9);
        assertEquals(100.0, d.getFlowerPressure(), 1e-9);
        assertEquals(100.0, d.getShrubPressure(),  1e-9);
        assertEquals(100.0, d.getTreePressure(),   1e-9);
        assertEquals(100.0, d.getDeadVegetation(), 1e-9);
    }

    @Test
    void constructorClampsBelowZero() {
        VegetationRegionData d = new VegetationRegionData(-1.0, -1.0, -1.0, -1.0, -1.0);
        assertEquals(0.0, d.getGrassPressure(),  1e-9);
        assertEquals(0.0, d.getFlowerPressure(), 1e-9);
        assertEquals(0.0, d.getShrubPressure(),  1e-9);
        assertEquals(0.0, d.getTreePressure(),   1e-9);
        assertEquals(0.0, d.getDeadVegetation(), 1e-9);
    }

    @Test
    void copyIsIndependent() {
        VegetationRegionData original = VegetationRegionData.defaults();
        VegetationRegionData copy = original.copy();
        copy.reduceFromLogging(40.0);
        assertEquals(40.0, original.getTreePressure(), 1e-9);
    }

    @Test
    void settersClampValues() {
        VegetationRegionData d = VegetationRegionData.defaults();
        d.setGrassPressure(-5.0);
        d.setDeadVegetation(999.0);
        assertEquals(0.0,   d.getGrassPressure(),  1e-9);
        assertEquals(100.0, d.getDeadVegetation(), 1e-9);
    }
}
