package p.packer.models;

import java.util.*;

public record PackerAssetCacheEntry(
        int assetId,
        String contractFingerprint,
        List<PackerFileCacheEntry> files) {

    public PackerAssetCacheEntry {
        if (assetId <= 0) {
            throw new IllegalArgumentException("assetId must be positive");
        }
        contractFingerprint = contractFingerprint == null || contractFingerprint.isBlank()
                ? null
                : contractFingerprint.trim();
        files = List.copyOf(Objects.requireNonNull(files, "files"));
    }

    public Optional<PackerFileCacheEntry> findFile(String relativePath) {
        final String safeRelativePath = Objects.requireNonNull(relativePath, "relativePath").trim().replace('\\', '/');
        return files.stream()
                .filter(file -> file.relativePath().equals(safeRelativePath))
                .findFirst();
    }
}
