package com.ai.trackwalker.uavaccess.mission;

import com.ai.trackwalker.uavaccess.config.UavAccessProperties;
import com.ai.trackwalker.uavaccess.event.OutboxEventService;
import com.ai.trackwalker.uavaccess.flighthub.FlightHubOpenApiClient;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.server.ResponseStatusException;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

class MissionServiceSafetyTest {
    @Test
    void rejectsMissionCreationBeforeAnyExternalOrDatabaseCallWhenFlightControlIsDisabled() {
        UavAccessProperties properties = new UavAccessProperties();
        properties.getFlighthub().setFlightControlEnabled(false);
        FlightHubOpenApiClient client = mock(FlightHubOpenApiClient.class);
        JdbcTemplate jdbc = mock(JdbcTemplate.class);
        OutboxEventService outbox = mock(OutboxEventService.class);
        MissionService service = new MissionService(properties, client, jdbc, outbox);

        ResponseStatusException exception = assertThrows(
                ResponseStatusException.class,
                () -> service.dispatch(new MissionDispatchRequest(
                        "connection-1",
                        "mission-1",
                        "task-1",
                        "route-version-1",
                        "DJI-DEVICE-SN",
                        "DJI-DOCK-SN",
                        "safety-gate-test",
                        null,
                        "dispatch-task-1",
                        Map.of()
                ))
        );

        assertEquals(409, exception.getStatusCode().value());
        assertTrue(exception.getReason().contains("FLIGHTHUB_FLIGHT_CONTROL_ENABLED"));
        verifyNoInteractions(client, jdbc, outbox);
    }
}
