package p.packer.models;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public record PackerRuntimeWalkFile(
        String relativePath,
        String mimeType,
        long size,
        long lastModified,
        String fingerprint,
        Optional<byte[]> contentBytes,
        Map<String, Object> metadata,
        List<PackerDiagnostic> diagnostics) {

    public PackerRuntimeWalkFile {
        relativePath = Objects.requireNonNull(relativePath, "relativePath");
        mimeType = mimeType == null || mimeType.isBlank() ? null : mimeType;
        if (size < 0L) {
            throw new IllegalArgumentException("size must be non-negative");
        }
        if (lastModified < 0L) {
            throw new IllegalArgumentException("lastModified must be non-negative");
        }
        fingerprint = fingerprint == null || fingerprint.isBlank() ? null : fingerprint;
        final Optional<byte[]> safeContentBytes = Objects.requireNonNull(contentBytes, "contentBytes");
        contentBytes = safeContentBytes.map(byte[]::clone);
        metadata = Map.copyOf(new LinkedHashMap<>(Objects.requireNonNull(metadata, "metadata")));
        diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
    }
}
