package com.cameleer.server.core.license;

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

public record LicenseLimits(Map<String, Integer> values) {

    public LicenseLimits {
        Objects.requireNonNull(values, "values");
    }

    public static LicenseLimits defaultsOnly() {
        return new LicenseLimits(DefaultTierLimits.DEFAULTS);
    }

    public static LicenseLimits mergeOverDefaults(Map<String, Integer> overrides) {
        Map<String, Integer> merged = new LinkedHashMap<>(DefaultTierLimits.DEFAULTS);
        if (overrides != null) merged.putAll(overrides);
        return new LicenseLimits(Collections.unmodifiableMap(merged));
    }

    public int get(String key) {
        Integer v = values.get(key);
        if (v == null) {
            throw new IllegalArgumentException("Unknown license limit key: " + key);
        }
        return v;
    }

    public boolean isDefaultSourced(String key, LicenseInfo license) {
        if (license == null) return true;
        return !license.limits().containsKey(key);
    }
}
