package at.procon.dip.clustering.dto;

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

public record ClusteringEngineRequest(Map<String, Object> parameters) {

    public ClusteringEngineRequest {
        parameters = parameters == null
                ? Map.of()
                : Collections.unmodifiableMap(new LinkedHashMap<>(parameters));
    }

    public int requiredInt(String key) {
        Object value = parameters.get(key);
        if (value == null) {
            throw new IllegalArgumentException("Missing required clustering parameter: " + key);
        }
        if (value instanceof Number number) {
            return number.intValue();
        }
        return Integer.parseInt(String.valueOf(value));
    }

    public int intValue(String key, int defaultValue) {
        Object value = parameters.get(key);
        if (value == null) {
            return defaultValue;
        }
        if (value instanceof Number number) {
            return number.intValue();
        }
        return Integer.parseInt(String.valueOf(value));
    }
}
