package p.packer.models;

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

public record PackerRegistryState(
        int schemaVersion,
        int nextAssetId,
        List<PackerRegistryEntry> assets) {

    public PackerRegistryState {
        if (schemaVersion <= 0) {
            throw new IllegalArgumentException("schemaVersion must be positive");
        }
        if (nextAssetId <= 0) {
            throw new IllegalArgumentException("nextAssetId must be positive");
        }
        assets = List.copyOf(Objects.requireNonNull(assets, "assets"));
    }

    public PackerRegistryState withAssets(List<PackerRegistryEntry> entries, int nextId) {
        return new PackerRegistryState(
                schemaVersion,
                nextId,
                entries.stream()
                        .sorted(Comparator.comparingInt(PackerRegistryEntry::assetId))
                        .toList());
    }
}
