using ElevatorDiag;

namespace ElevatorDiag.Tests;

public class ElevatorTests
{
    [Fact]
    public void RequestFloor_MovesToFloor()
    {
        var elevator = new Elevator();
        elevator.RequestFloor(3);
        
        // Floor 1 to 3 at 0.5/tick = 4 ticks to move
        // T1: Pos 1.5, T2: Pos 2.0, T3: Pos 2.5, T4: Pos 3.0 (DoorOpen)
        for(int i = 0; i < 4; i++) elevator.Update();

        Assert.Equal(3, elevator.CurrentFloor);
        Assert.Equal(ElevatorState.DoorOpen, elevator.State);
    }

    [Fact]
    public void Fault_StopsMovement()
    {
        var elevator = new Elevator();
        elevator.RequestFloor(5);
        elevator.Update(); // Start moving (Pos 1.5)
        
        elevator.TriggerFault("Emergency Stop");
        var posBefore = elevator.CurrentPosition;
        
        elevator.Update();
        
        Assert.Equal(ElevatorState.Faulted, elevator.State);
        Assert.Equal(posBefore, elevator.CurrentPosition);
    }

    [Fact]
    public void Priority_HandlesMultipleRequests()
    {
        var elevator = new Elevator();
        // Requests 5 then 2. From 1, it should go 1 -> 2 -> 5.
        elevator.RequestFloor(5);
        elevator.RequestFloor(2);

        // Tick 1: Moving to 2 (Pos 1.5)
        elevator.Update();
        // Tick 2: Arrives at 2 (Pos 2.0, DoorOpen)
        elevator.Update();
        
        Assert.Equal(2, elevator.CurrentFloor);
        Assert.Equal(ElevatorState.DoorOpen, elevator.State);
    }

    [Fact]
    public void Diagnostics_ReportsCorrectState()
    {
        var elevator = new Elevator();
        var diag = new ElevatorDiagnostics(elevator);
        
        elevator.TriggerFault("Sensor Failure");
        var health = diag.GetHealth();
        
        Assert.False(health.IsHealthy);
        Assert.Equal("Sensor Failure", health.ActiveFault);
    }
}
