package at.procon.eventhub.processing.eventprocessing.module.epl;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public record RuntimeEplModuleDefinition(
        String moduleKey,
        Map<String, Map<String, Object>> eventTypes,
        List<String> eplResources,
        List<String> outputStatementNames,
        List<RuntimeEplInputEventStream> inputStreams
) {
    public RuntimeEplModuleDefinition {
        if (moduleKey == null || moduleKey.isBlank()) {
            throw new IllegalArgumentException("moduleKey must not be blank");
        }
        moduleKey = moduleKey.trim();
        eventTypes = eventTypes == null ? Map.of() : immutableNestedMap(eventTypes);
        eplResources = eplResources == null ? List.of() : List.copyOf(eplResources);
        outputStatementNames = outputStatementNames == null ? List.of() : List.copyOf(outputStatementNames);
        inputStreams = inputStreams == null ? List.of() : List.copyOf(inputStreams);
    }

    private static Map<String, Map<String, Object>> immutableNestedMap(Map<String, Map<String, Object>> source) {
        LinkedHashMap<String, Map<String, Object>> copy = new LinkedHashMap<>();
        source.forEach((key, value) -> copy.put(key, value == null ? Map.of() : Collections.unmodifiableMap(new LinkedHashMap<>(value))));
        return Collections.unmodifiableMap(copy);
    }
}
