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

import at.procon.eventhub.processing.eventprocessing.partition.RuntimeEventPartitioningStrategy;
import at.procon.eventhub.processing.model.UnifiedDiscoveredVehicleRef;
import at.procon.eventhub.processing.eventprocessing.plan.RuntimeProcessingExecutionResultDto;
import at.procon.eventhub.processing.model.UnifiedRuntimeProcessingRequest;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public record RuntimeEventProcessingResultDto(
        String profileKey,
        RuntimeEventPartitioningStrategy partitioningStrategy,
        UnifiedRuntimeProcessingRequest request,
        int inputEventCount,
        int selectedPartitionCount,
        int discoveredVehicleCount,
        List<UnifiedDiscoveredVehicleRef> discoveredVehicles,
        Map<String, RuntimeEventProcessingPartitionResultDto> partitionResults,
        List<String> notes,
        List<String> warnings
) {
    public RuntimeEventProcessingResultDto {
        discoveredVehicles = discoveredVehicles == null ? List.of() : List.copyOf(discoveredVehicles);
        partitionResults = partitionResults == null ? Map.of() : Collections.unmodifiableMap(new LinkedHashMap<>(partitionResults));
        notes = notes == null ? List.of() : List.copyOf(notes);
        warnings = warnings == null ? List.of() : List.copyOf(warnings);
    }

    public static RuntimeEventProcessingResultDto fromExecution(
            RuntimeProcessingExecutionResultDto result,
            String legacyProfileKey
    ) {
        if (result == null) {
            throw new IllegalArgumentException("result must not be null");
        }
        return new RuntimeEventProcessingResultDto(
                legacyProfileKey == null || legacyProfileKey.isBlank() ? result.processingPlanKey() : legacyProfileKey,
                result.partitioningStrategy(),
                result.request(),
                result.inputEventCount(),
                result.selectedPartitionCount(),
                result.discoveredVehicleCount(),
                result.discoveredVehicles(),
                result.partitionResults(),
                result.notes(),
                result.warnings()
        );
    }

}
