package at.procon.eventhub.processing.eventprocessing.profile;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import at.procon.eventhub.processing.dto.UnifiedRuntimeDerivedProjectionResultDto;
import at.procon.eventhub.processing.dto.UnifiedRuntimeProcessingApiRequest;
import at.procon.eventhub.processing.dto.UnifiedRuntimeDriverWorkingTimeScopeResultDto;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventPartitioningApiRequest;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventProcessingApiRequest;
import at.procon.eventhub.processing.eventprocessing.partition.RuntimeEventPartitioningStrategy;
import at.procon.eventhub.processing.model.UnifiedEventSourceFamily;
import at.procon.eventhub.processing.model.UnifiedRuntimeProcessingRequest;
import at.procon.eventhub.processing.service.RuntimeDriverWorkingTimeScopeProcessingService;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

class TachographDriverEsperRuntimeEventProcessingProfileTest {

    @Test
    void exposesDiscoveryMetadata() {
        TachographDriverEsperRuntimeEventProcessingProfile profile = new TachographDriverEsperRuntimeEventProcessingProfile(
                org.mockito.Mockito.mock(RuntimeDriverWorkingTimeScopeProcessingService.class)
        );

        assertThat(profile.profileKey()).isEqualTo("tachograph-driver-esper-v1");
        assertThat(profile.displayName()).isEqualTo("Tachograph Driver Esper Processing");
        assertThat(profile.defaultPartitioningStrategy()).isEqualTo(RuntimeEventPartitioningStrategy.DRIVER);
        assertThat(profile.supportedPartitioningStrategies()).containsExactly(RuntimeEventPartitioningStrategy.DRIVER);
        assertThat(profile.optionalParameters()).containsExactlyInAnyOrder(
                "significantDrivingMinutes",
                "minimumRestPeriodMinutes",
                "attachVehicleOnlyEvents",
                "vehicleEvidencePaddingMinutes",
                "includePartitionDebug"
        );
    }

    @Test
    void delegatesToTachographScopeServiceAndMapsPartitionResults() {
        RuntimeDriverWorkingTimeScopeProcessingService scopeService = org.mockito.Mockito.mock(RuntimeDriverWorkingTimeScopeProcessingService.class);
        TachographDriverEsperRuntimeEventProcessingProfile profile = new TachographDriverEsperRuntimeEventProcessingProfile(scopeService);

        UUID sessionId = UUID.randomUUID();
        UnifiedRuntimeProcessingApiRequest scope = new UnifiedRuntimeProcessingApiRequest(
                sessionId,
                List.of(),
                null,
                null,
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION),
                null,
                null,
                null,
                Set.of(),
                false,
                Set.of(),
                false,
                null,
                null,
                null,
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-31T23:59:59Z"),
                true,
                15,
                null,
                null
        );
        RuntimeEventProcessingApiRequest request = new RuntimeEventProcessingApiRequest(
                TachographDriverEsperRuntimeEventProcessingProfile.PROFILE_KEY,
                scope,
                new RuntimeEventPartitioningApiRequest(
                        RuntimeEventPartitioningStrategy.DRIVER,
                        Set.of("12:DRIVER-1"),
                        false,
                        Set.of(),
                        false,
                        null,
                        null,
                        null,
                        null,
                        null
                ),
                Map.of(
                        "significantDrivingMinutes", 5,
                        "minimumRestPeriodMinutes", "600",
                        "vehicleEvidencePaddingMinutes", 20,
                        "attachVehicleOnlyEvents", true,
                        "includePartitionDebug", true
                )
        );

        UnifiedRuntimeProcessingRequest processedRequest = new UnifiedRuntimeProcessingRequest(
                sessionId,
                List.of(sessionId),
                null,
                null,
                Set.of(UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION),
                null,
                null,
                "12:DRIVER-1",
                Set.of("12:DRIVER-1"),
                false,
                Set.of(),
                false,
                null,
                null,
                null,
                OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                OffsetDateTime.parse("2026-05-31T23:59:59Z"),
                true,
                15
        );
        UnifiedRuntimeDerivedProjectionResultDto driverResult = new UnifiedRuntimeDerivedProjectionResultDto(
                processedRequest,
                2,
                1,
                3,
                5,
                List.of(),
                null,
                List.of("driver processed")
        );
        when(scopeService.processScope(any(), anyBoolean()))
                .thenReturn(new UnifiedRuntimeDriverWorkingTimeScopeResultDto(
                        processedRequest,
                        5,
                        1,
                        1,
                        List.of(),
                        Map.of("12:DRIVER-1", driverResult),
                        Map.of(),
                        List.of("scope processed"),
                        List.of()
                ));

        var result = profile.process(request);

        assertThat(result.profileKey()).isEqualTo(TachographDriverEsperRuntimeEventProcessingProfile.PROFILE_KEY);
        assertThat(result.partitioningStrategy()).isEqualTo(RuntimeEventPartitioningStrategy.DRIVER);
        assertThat(result.partitionResults()).containsOnlyKeys("12:DRIVER-1");
        assertThat(result.partitionResults().get("12:DRIVER-1").partitionType()).isEqualTo("DRIVER");
        assertThat(result.partitionResults().get("12:DRIVER-1").result()).isSameAs(driverResult);

        ArgumentCaptor<UnifiedRuntimeProcessingApiRequest> captor = ArgumentCaptor.forClass(UnifiedRuntimeProcessingApiRequest.class);
        ArgumentCaptor<Boolean> debugCaptor = ArgumentCaptor.forClass(Boolean.class);
        verify(scopeService).processScope(captor.capture(), debugCaptor.capture());
        UnifiedRuntimeProcessingApiRequest delegated = captor.getValue();
        assertThat(delegated.driverKeys()).containsExactly("12:DRIVER-1");
        assertThat(delegated.significantDrivingMinutes()).isEqualTo(5);
        assertThat(delegated.minimumRestPeriodMinutes()).isEqualTo(600);
        assertThat(delegated.vehicleExpansionPaddingMinutes()).isEqualTo(20);
        assertThat(delegated.expandVehicleEvents()).isTrue();
        assertThat(debugCaptor.getValue()).isTrue();
    }
}
