package at.procon.eventhub.processing.model;

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

import java.time.OffsetDateTime;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class UnifiedRuntimeProcessingRequestTest {

    @Test
    void buildsDriverScopedRuntimeRequest() {
        UnifiedRuntimeProcessingRequest request = UnifiedRuntimeProcessingRequest.forDriver(
                "default",
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_DB, UnifiedEventSourceFamily.YELLOWFOX_DB),
                "DRIVER:42",
                "AT",
                "1234567890123457",
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z")
        );

        assertThat(request.tenantKey()).isEqualTo("default");
        assertThat(request.sourceFamilies()).containsExactlyInAnyOrder(
                UnifiedEventSourceFamily.TACHOGRAPH_DB,
                UnifiedEventSourceFamily.YELLOWFOX_DB
        );
        assertThat(request.driverSourceEntityId()).isEqualTo("DRIVER:42");
        assertThat(request.driverCardNation()).isEqualTo("AT");
        assertThat(request.driverCardNumber()).isEqualTo("12345678901234");
        assertThat(request.eventBackend()).isEqualTo(UnifiedRuntimeEventBackend.SOURCE_DB);
        assertThat(request.expandVehicleEvents()).isTrue();
        assertThat(request.vehicleOccurredFrom()).isEqualTo(OffsetDateTime.parse("2026-05-01T00:00:00Z"));
    }

    @Test
    void canDisableVehicleExpansionExplicitly() {
        UnifiedRuntimeProcessingRequest request = UnifiedRuntimeProcessingRequest.forDriver(
                "default",
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_DB),
                "DRIVER:42",
                null,
                null,
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z"),
                false,
                30
        );

        assertThat(request.expandVehicleEvents()).isFalse();
        assertThat(request.vehicleExpansionPaddingMinutes()).isEqualTo(30);
    }

    @Test
    void canBuildEventHubBackedRuntimeRequest() {
        UnifiedRuntimeProcessingRequest request = UnifiedRuntimeProcessingRequest.forDriverFromEventHub(
                "default",
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_DB, UnifiedEventSourceFamily.YELLOWFOX_DB),
                "DRIVER:42",
                null,
                null,
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z"),
                true,
                15
        );

        assertThat(request.eventBackend()).isEqualTo(UnifiedRuntimeEventBackend.EVENTHUB_DB);
        assertThat(request.vehicleExpansionPaddingMinutes()).isEqualTo(15);
    }

    @Test
    void canBuildFileSessionRuntimeRequest() {
        UUID sessionId = UUID.randomUUID();
        UnifiedRuntimeProcessingRequest request = UnifiedRuntimeProcessingRequest.forTachographFileSession(
                sessionId,
                "12:123",
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z"),
                true,
                10
        );

        assertThat(request.sessionId()).isEqualTo(sessionId);
        assertThat(request.driverKey()).isEqualTo("12:123");
        assertThat(request.sourceFamilies()).containsExactly(UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION);
        assertThat(request.eventBackend()).isEqualTo(UnifiedRuntimeEventBackend.SOURCE_DB);
    }

    @Test
    void rejectsRequestWithoutDriverSelector() {
        assertThatThrownBy(() -> new UnifiedRuntimeProcessingRequest(
                null,
                "default",
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_DB),
                UnifiedRuntimeEventBackend.SOURCE_DB,
                null,
                null,
                null,
                null,
                null,
                null,
                true,
                0
        )).isInstanceOf(IllegalArgumentException.class)
                .hasMessageContaining("At least one driver selector");
    }
}
