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

import at.procon.eventhub.processing.dto.UnifiedRuntimeProcessingApiRequest;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public record RuntimeEventProcessingApiRequest(
        String profileKey,
        UnifiedRuntimeProcessingApiRequest scope,
        RuntimeEventPartitioningApiRequest partitioning,
        Map<String, Object> parameters
) {
    public RuntimeEventProcessingApiRequest {
        if (profileKey == null || profileKey.isBlank()) {
            throw new IllegalArgumentException("profileKey must not be blank");
        }
        profileKey = profileKey.trim();
        if (scope == null) {
            throw new IllegalArgumentException("scope must not be null");
        }
        partitioning = partitioning == null
                ? new RuntimeEventPartitioningApiRequest(null, null, null, null, null, null, null, null, null)
                : partitioning;
        parameters = parameters == null
                ? Map.of()
                : Collections.unmodifiableMap(new LinkedHashMap<>(parameters));
    }

    public static RuntimeEventProcessingApiRequest tachographDriverEsper(UnifiedRuntimeProcessingApiRequest scope) {
        return new RuntimeEventProcessingApiRequest(
                "tachograph-driver-esper-v1",
                scope,
                new RuntimeEventPartitioningApiRequest(
                        at.procon.eventhub.processing.eventprocessing.partition.RuntimeEventPartitioningStrategy.DRIVER,
                        null,
                        scope != null ? scope.includeAllDrivers() : null,
                        scope != null ? scope.driverKeys() : null,
                        scope != null ? scope.includeAllDrivers() : null,
                        scope != null ? scope.vehicleKeys() : null,
                        scope != null ? scope.includeAllVehicles() : null,
                        null,
                        scope != null ? scope.vehicleExpansionPaddingMinutes() : null
                ),
                Map.of()
        );
    }
}
