package de.gecheckt.pdf.umbenenner.application.port.out.modelcatalog;

import java.util.Objects;
import java.util.Optional;

/**
 * Carries all information needed by an {@link AiModelCatalogPort} implementation to fetch
 * the list of available models from a provider endpoint.
 * <p>
 * This record is infrastructure-neutral: it contains no HTTP client, no JSON type, and no
 * JavaFX reference. The adapter translates the values into provider-specific request structures.
 * <p>
 * The {@code apiKey} field is {@code Optional}: when the key is absent the adapter is expected
 * to perform the request without authentication and return an appropriate
 * {@link ModelCatalogResult} sub-type (typically
 * {@link ModelCatalogResult.IncompleteConfiguration}) rather than throwing an exception.
 *
 * @param providerIdentifier identifier string of the target provider family as used in the
 *                           {@code ai.provider.active} configuration property
 *                           (e.g., {@code "claude"} or {@code "openai-compatible"});
 *                           must not be {@code null}
 * @param baseUrl            optional base URL of the provider endpoint; when absent the adapter
 *                           must apply its own built-in default (e.g., for Claude)
 * @param apiKey             optional API key; when absent the adapter must not fabricate a key
 * @param timeoutSeconds     HTTP timeout in seconds; must be a positive integer
 */
public record ModelCatalogRequest(
        String providerIdentifier,
        Optional<String> baseUrl,
        Optional<String> apiKey,
        int timeoutSeconds) {

    /**
     * Creates a new model catalogue request.
     *
     * @param providerIdentifier identifier string of the target provider family; must not be {@code null}
     * @param baseUrl            optional base URL; {@code null} is treated as {@link Optional#empty()}
     * @param apiKey             optional API key; {@code null} is treated as {@link Optional#empty()}
     * @param timeoutSeconds     HTTP timeout in seconds; must be positive
     * @throws NullPointerException     if {@code providerIdentifier} is {@code null}
     * @throws IllegalArgumentException if {@code timeoutSeconds} is not positive
     */
    public ModelCatalogRequest {
        Objects.requireNonNull(providerIdentifier, "providerIdentifier must not be null");
        if (timeoutSeconds <= 0) {
            throw new IllegalArgumentException("timeoutSeconds must be positive, was: " + timeoutSeconds);
        }
        baseUrl = Objects.requireNonNullElse(baseUrl, Optional.empty());
        apiKey = Objects.requireNonNullElse(apiKey, Optional.empty());
    }
}
