package at.procon.eventhub.processing.eventprocessing;

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

import at.procon.eventhub.processing.dto.UnifiedRuntimeProcessingApiRequest;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventPartitioningApiRequest;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventProcessingApiRequest;
import at.procon.eventhub.processing.eventprocessing.dto.RuntimeEventProcessingResultDto;
import at.procon.eventhub.processing.eventprocessing.partition.RuntimeEventPartitioningStrategy;
import at.procon.eventhub.processing.eventprocessing.profile.RuntimeEventProcessingProfile;
import at.procon.eventhub.processing.eventprocessing.profile.RuntimeEventProcessingProfileRegistry;
import at.procon.eventhub.processing.model.UnifiedEventSourceFamily;
import at.procon.eventhub.processing.model.UnifiedRuntimeProcessingRequest;
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;

class RuntimeEventProcessingServiceTest {

    @Test
    void processesThroughSelectedProfileAndListsProfiles() {
        RuntimeEventProcessingService service = new RuntimeEventProcessingService(
                new RuntimeEventProcessingProfileRegistry(List.of(new EchoProfile()))
        );
        UUID sessionId = UUID.randomUUID();
        RuntimeEventProcessingApiRequest request = new RuntimeEventProcessingApiRequest(
                "echo-profile-v1",
                new UnifiedRuntimeProcessingApiRequest(
                        sessionId,
                        List.of(),
                        null,
                        null,
                        Set.of(UnifiedEventSourceFamily.TACHOGRAPH_FILE_SESSION),
                        null,
                        null,
                        "12:DRIVER-1",
                        Set.of(),
                        false,
                        Set.of(),
                        false,
                        null,
                        null,
                        null,
                        OffsetDateTime.parse("2026-05-01T00:00:00Z"),
                        OffsetDateTime.parse("2026-05-01T01:00:00Z"),
                        true,
                        0,
                        null,
                        null
                ),
                new RuntimeEventPartitioningApiRequest(RuntimeEventPartitioningStrategy.DRIVER, null, false, null, false, null, false, null, null, null),
                Map.of()
        );

        RuntimeEventProcessingResultDto result = service.process(request);

        assertThat(result.profileKey()).isEqualTo("echo-profile-v1");
        assertThat(result.partitioningStrategy()).isEqualTo(RuntimeEventPartitioningStrategy.DRIVER);
        var profiles = service.listProfiles();
        assertThat(profiles).hasSize(1);
        assertThat(profiles.get(0).profileKey()).isEqualTo("echo-profile-v1");
    }

    private static final class EchoProfile implements RuntimeEventProcessingProfile {

        @Override
        public String profileKey() {
            return "echo-profile-v1";
        }

        @Override
        public RuntimeEventPartitioningStrategy defaultPartitioningStrategy() {
            return RuntimeEventPartitioningStrategy.DRIVER;
        }

        @Override
        public RuntimeEventProcessingResultDto process(RuntimeEventProcessingApiRequest request) {
            UnifiedRuntimeProcessingRequest runtimeRequest = request.scope().toRuntimeRequest();
            return new RuntimeEventProcessingResultDto(
                    profileKey(),
                    defaultPartitioningStrategy(),
                    runtimeRequest,
                    0,
                    0,
                    0,
                    List.of(),
                    Map.of(),
                    List.of("echo"),
                    List.of()
            );
        }
    }
}
