package com.livingworld.data.saved;

/**
 * Immutable metadata for saved game data.
 *
 * @param schemaVersion the schema version number (must be > 0)
 * @param modVersion    the mod version string (must not be blank)
 * @param createdAt     the timestamp when the save was created
 * @param updatedAt     the timestamp when the save was last updated
 */
public record SaveMetadata(
        int schemaVersion,
        String modVersion,
        long createdAt,
        long updatedAt
) {

    /**
     * Constructs a {@link SaveMetadata} and validates all fields.
     *
     * @param schemaVersion the schema version number (must be > 0)
     * @param modVersion    the mod version string (must not be blank)
     * @param createdAt     the timestamp when the save was created
     * @param updatedAt     the timestamp when the save was last updated
     * @throws IllegalArgumentException if {@code schemaVersion} is not greater than 0
     * @throws IllegalArgumentException if {@code modVersion} is blank
     * @throws IllegalArgumentException if {@code updatedAt} is less than {@code createdAt}
     */
    public SaveMetadata {
        if (schemaVersion <= 0) {
            throw new IllegalArgumentException(
                    "schemaVersion must be greater than 0, got: " + schemaVersion);
        }

        if (modVersion == null || modVersion.isBlank()) {
            throw new IllegalArgumentException("modVersion must not be blank");
        }

        if (updatedAt < createdAt) {
            throw new IllegalArgumentException(
                    "updatedAt (" + updatedAt + ") must be >= createdAt (" + createdAt + ")");
        }
    }
}
