namespace ReactorMaintenance.Simulation.Tests;

public sealed class SimulationEngineTests
{
    [Fact]
    public void NetworkPropagationSuppliesConsumerServicesAndReadiesReactor()
    {
        var level = BuildReadyLevel();

        var next = m_Engine.AdvanceTurn(level);
        var consumer = next.GetProp(new(3, 3));

        Assert.Equal(EConsumerServiceState.Producing, consumer.FuelServiceState);
        Assert.Equal(EConsumerServiceState.Producing, consumer.CoolantServiceState);
        Assert.Equal(EConsumerServiceState.Producing, consumer.ElectricityServiceState);
        Assert.Equal(ELevelState.Ready, next.Global.LevelState);
        Assert.Contains(next.Forecasts, forecast => forecast.Kind == EForecastKind.ReactorReady);
    }

    [Fact]
    public void ReactorNeedsPositiveFlowOnlyForNetworksBeneathControl()
    {
        var level = BuildReadyLevel();
        level = level.SetUnderground(new(5, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });

        var next = m_Engine.AdvanceTurn(level);

        Assert.NotEqual(ELevelState.Ready, next.Global.LevelState);
    }

    [Fact]
    public void ReactorActivatesOnlyAtReadyControl()
    {
        var level = m_Engine.AdvanceTurn(BuildReadyLevel()) with {
            Robot = new() { Position = new(5, 3) }
        };

        var activated = m_Engine.ActivateReactor(level);

        Assert.Equal(ELevelState.Won, activated.Global.LevelState);
        Assert.True(activated.Reactors[0].Activated);
    }

    [Fact]
    public void DisabledConsumerReportsDisabledOnlyForNetworksBeneathIt()
    {
        var level = LevelState.Create("Disabled", 6, 6);
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
        level = level.SetProp(new(2, 2), new() { Type = EPropType.Consumer, SwitchState = EPropSwitchState.Disabled });

        var next = m_Engine.AdvanceTurn(level);
        var consumer = next.GetProp(new(2, 2));

        Assert.Equal(EConsumerServiceState.Disabled, consumer.FuelServiceState);
        Assert.Equal(EConsumerServiceState.Unknown, consumer.CoolantServiceState);
        Assert.Equal(EConsumerServiceState.Unknown, consumer.ElectricityServiceState);
        Assert.Equal(EConsumerServiceState.Disabled, consumer.ServiceState);
    }

    [Fact]
    public void MovementIsQuickAndDoesNotResolveSimulationStep()
    {
        var level = LevelState.Create("Quick", 6, 6) with {
            Robot = new() { Position = new(1, 1) }
        };

        var next = m_Engine.MoveRobot(level, new(2, 1));

        Assert.Equal(new(2, 1), next.Robot.Position);
        Assert.Equal(0, next.Global.Turn);
    }

    [Fact]
    public void DoorInteractionIsLengthyAndResolvesSimulationStep()
    {
        var level = DoorLevel();
        level = level with { Robot = new() { Position = new(3, 2) } };

        var next = m_Engine.InteractProp(level);

        Assert.Equal(EDoorState.Open, next.GetProp(new(3, 2)).DoorState);
        Assert.Equal(1, next.Global.Turn);
    }

    [Fact]
    public void ClosedInferredDoorBlocksAdjacentHeatFlow()
    {
        var level = DoorLevel();
        level = level.SetSurface(new(3, 2), new() { Heat = 8 });

        var next = m_Engine.AdvanceTurn(level);

        Assert.Equal(0, next.GetSurface(new(4, 2)).Heat);
    }

    [Fact]
    public void StructuralIntegrityCreatesLeakWhenWeakCellHasPositivePressure()
    {
        var level = LevelState.Create("Integrity leak", 6, 6);
        level = AddLine(level, ECarrierType.Fuel, new(1, 2), new(2, 2));
        level = level.SetProp(new(1, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, level.GetUnderground(new(2, 2), ECarrierType.Fuel) with {
            StructuralIntegrity = Balancing.Current.StructuralIntegrityLeakThreshold
        });

        var next = m_Engine.AdvanceTurn(level);

        Assert.Equal(EUndergroundState.Leaking, next.GetUnderground(new(2, 2), ECarrierType.Fuel).State);
        Assert.Contains(next.Leaks, leak => leak.Carrier == ECarrierType.Fuel && leak.UndergroundPosition == new GridPosition(2, 2));
        Assert.True(next.GetSurface(new(2, 2)).Fuel > 0);
    }

    [Fact]
    public void HighPressureWorsensNonMaxStructuralIntegrity()
    {
        var level = LevelState.Create("Integrity damage", 6, 6);
        level = AddLine(level, ECarrierType.Fuel, new(1, 2), new(2, 2));
        level = level.SetProp(new(1, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, level.GetUnderground(new(2, 2), ECarrierType.Fuel) with {
            StructuralIntegrity = Balancing.Current.MaxStructuralIntegrity - 1
        });

        var next = m_Engine.AdvanceTurn(level);

        Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).StructuralIntegrity < Balancing.Current.MaxStructuralIntegrity - 1);
    }

    [Fact]
    public void RepairingLeakRestoresStructuralIntegrity()
    {
        var level = LevelState.Create("Repair", 6, 6);
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() {
            State = EUndergroundState.Leaking,
            Amount = 5,
            Intensity = 5,
            StructuralIntegrity = 0
        }) with {
            Robot = new() { Position = new(2, 2) },
            Leaks = [new() { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
        };

        var next = m_Engine.InteractLeak(level, ECarrierType.Fuel, false);

        Assert.True(next.Leaks[0].Repaired);
        Assert.Equal(EUndergroundState.Intact, next.GetUnderground(new(2, 2), ECarrierType.Fuel).State);
        Assert.Equal(Balancing.Current.MaxStructuralIntegrity, next.GetUnderground(new(2, 2), ECarrierType.Fuel).StructuralIntegrity);
    }

    [Fact]
    public void ElementRemedyClearsHazardAndBlocksImmediateReentry()
    {
        var level = LevelState.Create("Remedy", 6, 6);
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Leaking, Amount = 5, Intensity = 5 });
        level = level.SetSurface(new(2, 2), new() { Fuel = 5 }) with {
            Robot = new() { Position = new(2, 2), FuelNeutralizers = 1 },
            Leaks = [new() { Carrier = ECarrierType.Fuel, UndergroundPosition = new(2, 2), AccessPosition = new(2, 2) }]
        };

        var next = m_Engine.InteractLeak(level, ECarrierType.Fuel, true);

        Assert.Equal(0, next.GetSurface(new(2, 2)).Fuel);
        Assert.True(next.GetSurface(new(2, 2)).FuelBlockTurns > 0);
        Assert.Equal(0, next.Robot.FuelNeutralizers);
    }

    [Fact]
    public void HeatShieldPreventsRobotHeatLoss()
    {
        var level = LevelState.Create("Heat shield", 6, 6);
        level = level.SetSurface(new(2, 2), new() { Heat = Balancing.Current.RobotHeatSafetyThreshold }) with {
            Robot = new() { Position = new(2, 2), HeatImmunitySteps = 1 }
        };

        var next = m_Engine.AdvanceTurn(level);

        Assert.NotEqual(ELevelState.Lost, next.Global.LevelState);
    }

    [Fact]
    public void JunctionRatioSplitsFlowAcrossInferredOutgoingBranches()
    {
        var level = BuildJunctionLevel(2);

        var next = m_Engine.AdvanceTurn(level);

        Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount > 0);
        Assert.Equal(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount, next.GetUnderground(new(3, 3), ECarrierType.Fuel).Amount);
        Assert.True(next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount < next.GetUnderground(new(2, 3), ECarrierType.Fuel).Amount);
    }

    [Fact]
    public void JunctionZeroWeightBranchReceivesNoIntentionalOutflow()
    {
        var level = BuildJunctionLevel(0);

        var next = m_Engine.AdvanceTurn(level);

        Assert.Equal(0, next.GetUnderground(new(2, 2), ECarrierType.Fuel).Amount);
        Assert.True(next.GetUnderground(new(3, 3), ECarrierType.Fuel).Amount > 0);
    }

    [Fact]
    public void ValidatorRejectsAmbiguousJunctionSourceBranches()
    {
        var level = BuildJunctionLevel(2);
        level = level.SetProp(new(3, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });

        var report = new LevelValidator().Validate(level);

        Assert.False(report.IsValid);
        Assert.Contains(report.Errors, error => error.Message.Contains("Ambiguous junction flow", StringComparison.Ordinal));
    }

    [Fact]
    public void ValidatorRejectsInvalidDoorGeometryAndWallHazards()
    {
        var level = LevelState.Create("Invalid", 6, 6);
        level = level.SetProp(new(2, 2), new() { Type = EPropType.Door });
        level = level.SetTerrain(new(4, 4), ECellTerrain.Wall);
        level = level with { Surface = level.Surface.ToArray() };
        level.Surface[level.Index(new(4, 4))] = new() { Heat = 1 };

        var report = new LevelValidator().Validate(level);

        Assert.False(report.IsValid);
        Assert.Contains(report.Errors, error => error.Message.Contains("Door must be surrounded", StringComparison.Ordinal));
        Assert.Contains(report.Errors, error => error.Message.Contains("Wall cell", StringComparison.Ordinal));
    }

    [Fact]
    public void LevelSerializationRoundTripsCurrentSchemaOnly()
    {
        var level = BuildReadyLevel();

        var json = LevelSerializer.Serialize(level);
        var loaded = LevelSerializer.Deserialize(json);

        Assert.Contains("\"Version\": 3", json);
        Assert.Equal(level.Name, loaded.Name);
        Assert.Equal(EPropType.Consumer, loaded.GetProp(new(3, 3)).Type);
        Assert.Equal(level.RequiredFuelConsumers, loaded.RequiredFuelConsumers);
    }

    [Fact]
    public void LevelSerializationRoundTripsPropDoorsAndElectricityLeakFaces()
    {
        var level = BuildReadyLevel();
        level = level.SetTerrain(new(6, 4), ECellTerrain.Wall);
        level = level.SetUnderground(new(6, 4), ECarrierType.Electricity, new() { State = EUndergroundState.Leaking }) with {
            Leaks = [new() { Carrier = ECarrierType.Electricity, UndergroundPosition = new(6, 4), AccessPosition = new(6, 3) }]
        };
        level = DoorLevel(level);

        var loaded = LevelSerializer.Deserialize(LevelSerializer.Serialize(level));

        Assert.Equal(EPropType.Door, loaded.GetProp(new(3, 2)).Type);
        Assert.Single(loaded.Leaks);
        Assert.Equal(new(6, 3), loaded.Leaks[0].AccessPosition);
    }

    [Fact]
    public void LevelSerializationRejectsOldSchema()
    {
        var json = """
                   {
                     "Version": 2,
                     "Level": {}
                   }
                   """;

        var exception = Assert.Throws<InvalidOperationException>(() => LevelSerializer.Deserialize(json));

        Assert.Contains("Unsupported level file version 2", exception.Message);
    }

    private static LevelState BuildReadyLevel()
    {
        var level = LevelState.Create("Ready", 8, 7);
        level = AddLine(level, ECarrierType.Fuel, new(2, 3), new(3, 3));
        level = AddLine(level, ECarrierType.Coolant, new(2, 3), new(3, 3));
        level = AddLine(level, ECarrierType.Electricity, new(2, 3), new(3, 3));
        level = level.SetProp(new(2, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
        level = level.SetProp(new(2, 2), new() { Type = EPropType.Flow, Carrier = ECarrierType.Coolant });
        level = level.SetUnderground(new(2, 2), ECarrierType.Coolant, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(new(2, 3), ECarrierType.Coolant, new() { State = EUndergroundState.Intact });
        level = level.SetProp(new(2, 4), new() { Type = EPropType.Flow, Carrier = ECarrierType.Electricity });
        level = level.SetUnderground(new(2, 4), ECarrierType.Electricity, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(new(2, 3), ECarrierType.Electricity, new() { State = EUndergroundState.Intact });
        level = level.SetProp(new(3, 3), new() { Type = EPropType.Consumer });
        level = level.SetProp(new(5, 3), new() { Type = EPropType.ReactorControl, ReactorId = 1 });
        return level with {
            Robot = new() { Position = new(5, 3) },
            Reactors = [new() { ReactorId = 1, ControlPosition = new(5, 3) }]
        };
    }

    private static LevelState DoorLevel(LevelState? seed = null)
    {
        var level = seed ?? LevelState.Create("Door", 6, 6);
        level = level.SetTerrain(new(3, 1), ECellTerrain.Wall);
        level = level.SetTerrain(new(3, 3), ECellTerrain.Wall);
        return level.SetProp(new(3, 2), new() { Type = EPropType.Door, DoorState = EDoorState.Closed });
    }

    private static LevelState AddLine(LevelState level, ECarrierType carrier, GridPosition a, GridPosition b)
    {
        level = level.SetUnderground(a, carrier, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(b, carrier, new() { State = EUndergroundState.Intact });
        return level;
    }

    private static LevelState BuildJunctionLevel(int mode)
    {
        var level = LevelState.Create("Junction", 6, 6);
        level = level.SetUnderground(new(1, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(new(2, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(new(2, 2), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
        level = level.SetUnderground(new(3, 3), ECarrierType.Fuel, new() { State = EUndergroundState.Intact });
        level = level.SetProp(new(1, 3), new() { Type = EPropType.Flow, Carrier = ECarrierType.Fuel });
        return level.SetProp(new(2, 3), new() { Type = EPropType.Junction, Carrier = ECarrierType.Fuel, JunctionMode = mode });
    }

    private readonly SimulationEngine m_Engine = new();
}