package com.r35157.nenjim.service.journal.model;

import java.util.List;
import java.util.Objects;

/** Artifact-wide hard rules and selection hints in one Journal snapshot. */
public record JournalPolicy(
        List<PolicyRule> recommended,
        List<PolicyRule> discouraged,
        List<PolicyRule> blacklist) {
    public JournalPolicy {
        recommended = copyRules(recommended, "recommended");
        discouraged = copyRules(discouraged, "discouraged");
        blacklist = copyRules(blacklist, "blacklist");
        validateRecommendations(recommended);
    }

    public static JournalPolicy empty() {
        return new JournalPolicy(List.of(), List.of(), List.of());
    }

    private static List<PolicyRule> copyRules(List<PolicyRule> rules, String name) {
        Objects.requireNonNull(rules, name);
        rules.forEach(rule -> Objects.requireNonNull(rule, name + " rule"));
        return List.copyOf(rules);
    }

    private static void validateRecommendations(List<PolicyRule> recommendations) {
        for (PolicyRule recommendation : recommendations) {
            if (recommendation.versions().expressions().size() != 1
                    || !recommendation.versions().expressions().getFirst().isExact()) {
                throw new IllegalArgumentException(
                        "RECOMMENDED must identify exactly one exact major.minor.patch release");
            }
        }
    }
}
