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.UUID;
import org.junit.jupiter.api.Test;

class UnifiedDriverEventsRequestTest {

    @Test
    void buildsFileSessionRequest() {
        UUID sessionId = UUID.randomUUID();

        UnifiedDriverEventsRequest request = UnifiedDriverEventsRequest.forTachographFileSession(
                sessionId,
                " 12:123 ",
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z")
        );

        assertThat(request.sourceFamily()).isEqualTo(UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION);
        assertThat(request.sessionId()).isEqualTo(sessionId);
        assertThat(request.driverKey()).isEqualTo("12:123");
        assertThat(request.tenantKey()).isNull();
    }

    @Test
    void buildsTachographDbDriverRequest() {
        UnifiedDriverEventsRequest request = UnifiedDriverEventsRequest.forTachographDbDriver(
                " default ",
                " DRIVER:42 ",
                "at",
                " 1234567890123457 ",
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-02T00:00:00Z")
        );

        assertThat(request.sourceFamily()).isEqualTo(UnifiedEventSourceFamily.TACHOGRAPH_DB);
        assertThat(request.tenantKey()).isEqualTo("default");
        assertThat(request.driverSourceEntityId()).isEqualTo("DRIVER:42");
        assertThat(request.driverCardNation()).isEqualTo("AT");
        assertThat(request.driverCardNumber()).isEqualTo("12345678901234");
        assertThat(request.hasDriverSelector()).isTrue();
        assertThat(request.hasVehicleSelector()).isFalse();
    }

    @Test
    void buildsYellowFoxVehicleRequest() {
        UnifiedDriverEventsRequest request = UnifiedDriverEventsRequest.forYellowFoxDbVehicle(
                "default",
                "VEHICLE:99",
                "wdb123",
                "de",
                "W-123AB",
                null,
                null
        );

        assertThat(request.sourceFamily()).isEqualTo(UnifiedEventSourceFamily.YELLOWFOX_DB);
        assertThat(request.vehicleSourceEntityId()).isEqualTo("VEHICLE:99");
        assertThat(request.vin()).isEqualTo("WDB123");
        assertThat(request.registrationNation()).isEqualTo("DE");
        assertThat(request.registrationNumber()).isEqualTo("W-123AB");
        assertThat(request.hasVehicleSelector()).isTrue();
    }

    @Test
    void rejectsDbRequestWithoutSubjectSelector() {
        assertThatThrownBy(() -> new UnifiedDriverEventsRequest(
                UnifiedEventSourceFamily.TACHOGRAPH_DB,
                null,
                null,
                null,
                "default",
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        )).isInstanceOf(IllegalArgumentException.class)
                .hasMessageContaining("At least one driver or vehicle selector");
    }
}
