package com.cameleer.server.core.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

/**
 * Merges global (enforced) sensitive keys with per-app additions.
 * Union-only: per-app can add keys, never remove global keys.
 * Case-insensitive deduplication, preserves first-seen casing.
 */
public final class SensitiveKeysMerger {

    private SensitiveKeysMerger() {}

    /**
     * @param global  enforced global keys (null = not configured)
     * @param perApp  per-app additional keys (null = none)
     * @return merged list, or null if both inputs are null
     */
    public static List<String> merge(List<String> global, List<String> perApp) {
        if (global == null && perApp == null) {
            return null;
        }

        TreeSet<String> seen = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        List<String> result = new ArrayList<>();

        if (global != null) {
            for (String key : global) {
                if (seen.add(key)) {
                    result.add(key);
                }
            }
        }
        if (perApp != null) {
            for (String key : perApp) {
                if (seen.add(key)) {
                    result.add(key);
                }
            }
        }
        return result;
    }
}
