package com.ai.trackwalker.capability.uav;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class MissionStateMachineTest {
    @Test
    void supportsNormalFlightLifecycle() {
        assertThat(MissionStateMachine.canTransition("READY", "DISPATCHING")).isTrue();
        assertThat(MissionStateMachine.canTransition("DISPATCHED", "FLYING")).isTrue();
        assertThat(MissionStateMachine.canTransition("FLYING", "PAUSED")).isTrue();
        assertThat(MissionStateMachine.canTransition("PAUSED", "FLYING")).isTrue();
        assertThat(MissionStateMachine.canTransition("FLYING", "RETURNING")).isTrue();
        assertThat(MissionStateMachine.canTransition("RETURNING", "COMPLETED")).isTrue();
    }

    @Test
    void rejectsUnsafeTransitionAndUnknownCommand() {
        assertThat(MissionStateMachine.canTransition("COMPLETED", "FLYING")).isFalse();
        assertThat(MissionStateMachine.canTransition("READY", "COMPLETED")).isFalse();
        assertThatThrownBy(() -> MissionStateMachine.targetForCommand("FLYING", "UNSAFE_COMMAND"))
                .isInstanceOf(IllegalArgumentException.class);
    }

    @Test
    void taskDispatchRequiresApprovedPublishedRouteAndNoExistingMission() {
        assertThat(InspectionTaskWorkflow.canDispatch("created", "APPROVED", true, true, false)).isTrue();
        assertThat(InspectionTaskWorkflow.canDispatch("created", "DRAFT", true, true, false)).isFalse();
        assertThat(InspectionTaskWorkflow.canDispatch("created", "APPROVED", false, false, false)).isFalse();
        assertThat(InspectionTaskWorkflow.canDispatch("dispatched", "APPROVED", true, true, true)).isFalse();
    }

    @Test
    void taskStatusFollowsMissionLifecycle() {
        assertThat(InspectionTaskWorkflow.taskStatusForMission("DISPATCHED")).isEqualTo("dispatched");
        assertThat(InspectionTaskWorkflow.taskStatusForMission("PAUSED")).isEqualTo("flying");
        assertThat(InspectionTaskWorkflow.taskStatusForMission("COMPLETED")).isEqualTo("data_uploading");
        assertThat(InspectionTaskWorkflow.taskStatusForMission("ABORTED")).isEqualTo("failed");
    }
}
