package com.xfestudio.xfeservermanager.core.menu;

import java.time.Instant;
import java.util.Objects;
import java.util.UUID;

/** Metadata and bounded bytes for an administrator-uploaded menu image. */
public record MenuImageAsset(UUID id, String name, String mimeType, int width, int height,
                             String sha256, byte[] content, String createdBy, Instant createdAt) {
    public static final int MAX_BYTES = 256 * 1024;
    public static final int MAX_DIMENSION = 1_024;

    public MenuImageAsset {
        Objects.requireNonNull(id, "id");
        name = Objects.toString(name, "").strip();
        if (name.isEmpty() || name.length() > 128 || name.codePoints().anyMatch(Character::isISOControl)) {
            throw new IllegalArgumentException("image name must contain 1-128 printable characters");
        }
        mimeType = Objects.toString(mimeType, "").strip().toLowerCase(java.util.Locale.ROOT);
        if (!mimeType.equals("image/png") && !mimeType.equals("image/jpeg")) {
            throw new IllegalArgumentException("menu images must be PNG or JPEG");
        }
        if (width < 1 || height < 1 || width > MAX_DIMENSION || height > MAX_DIMENSION) {
            throw new IllegalArgumentException("menu image dimensions are out of range");
        }
        sha256 = Objects.toString(sha256, "").strip().toLowerCase(java.util.Locale.ROOT);
        if (!sha256.matches("[0-9a-f]{64}")) throw new IllegalArgumentException("image sha256 is invalid");
        content = Objects.requireNonNull(content, "content").clone();
        if (content.length == 0 || content.length > MAX_BYTES) throw new IllegalArgumentException("menu image is too large");
        createdBy = Objects.toString(createdBy, "").strip();
        if (createdBy.isEmpty() || createdBy.length() > 128) throw new IllegalArgumentException("image creator is invalid");
        Objects.requireNonNull(createdAt, "createdAt");
    }

    @Override
    public byte[] content() {
        return content.clone();
    }
}
