package de.gecheckt.pdf.umbenenner.application.config.provider;

import java.util.Arrays;
import java.util.Optional;

/**
 * Supported AI provider families for the PDF renaming process.
 * <p>
 * Each constant represents a distinct API protocol family. Exactly one provider family
 * is active per application run, selected via the {@code ai.provider.active} configuration property.
 * <p>
 * The {@link #getIdentifier()} method returns the string that must appear as the value of
 * {@code ai.provider.active} to activate the corresponding provider family.
 * Use {@link #fromIdentifier(String)} to resolve a configuration string to the enum constant.
 */
public enum AiProviderFamily {

    /** OpenAI-compatible Chat Completions API – usable with OpenAI itself and compatible third-party endpoints. */
    OPENAI_COMPATIBLE("openai-compatible"),

    /** Native Anthropic Messages API for Claude models. */
    CLAUDE("claude");

    private final String identifier;

    AiProviderFamily(String identifier) {
        this.identifier = identifier;
    }

    /**
     * Returns the configuration identifier string for this provider family.
     * <p>
     * This value corresponds to valid values of the {@code ai.provider.active} property.
     *
     * @return the configuration identifier, never {@code null}
     */
    public String getIdentifier() {
        return identifier;
    }

    /**
     * Resolves a provider family from its configuration identifier string.
     * <p>
     * The comparison is case-sensitive and matches the exact identifier strings
     * defined by each constant (e.g., {@code "openai-compatible"}, {@code "claude"}).
     *
     * @param identifier the identifier as it appears in the {@code ai.provider.active} property;
     *                   {@code null} returns an empty Optional
     * @return the matching provider family, or {@link Optional#empty()} if not recognized
     */
    public static Optional<AiProviderFamily> fromIdentifier(String identifier) {
        if (identifier == null) {
            return Optional.empty();
        }
        return Arrays.stream(values())
                .filter(f -> f.identifier.equals(identifier))
                .findFirst();
    }
}
