package fr.bglands.client.protocol;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public record UiSubmit(
        String instanceId,
        String submitToken,
        String requestId,
        Map<String, String> values
) {
    public UiSubmit {
        requireUuid(instanceId, "instanceId");
        requireUuid(submitToken, "submitToken");
        requireUuid(requestId, "requestId");
        Objects.requireNonNull(values, "values");
        values = Map.copyOf(new LinkedHashMap<>(values));
    }

    public static UiSubmit create(String instanceId, String submitToken, Map<String, String> values) {
        return new UiSubmit(instanceId, submitToken, UUID.randomUUID().toString(), values);
    }

    private static void requireUuid(String value, String field) {
        Objects.requireNonNull(value, field);
        try {
            UUID.fromString(value);
        } catch (IllegalArgumentException exception) {
            throw new IllegalArgumentException(field + " must be a UUID", exception);
        }
    }
}
