package de.gecheckt.pdf.umbenenner.adapter.in.gui.editor;

import de.gecheckt.pdf.umbenenner.application.config.provider.AiProviderFamily;
import javafx.util.StringConverter;

/**
 * A JavaFX {@link StringConverter} that maps {@link AiProviderFamily} constants to
 * German display labels and back.
 * <p>
 * Used by the provider selection {@code ComboBox} to show human-readable German names
 * while keeping the underlying model type-safe. The reverse conversion
 * ({@link #fromString(String)}) supports the same label strings produced by
 * {@link #toString(AiProviderFamily)} so that a ComboBox configured as non-editable
 * can still convert its selected text back to the enum constant when needed.
 * <p>
 * Returns {@code null} for inputs that do not match any known constant to signal an
 * unrecognised display label.
 */
public final class AiProviderFamilyStringConverter extends StringConverter<AiProviderFamily> {

    /**
     * Creates a new converter instance.
     */
    public AiProviderFamilyStringConverter() {
        // Default constructor.
    }

    /**
     * Returns the German display label for the given provider family.
     *
     * @param family the provider family to convert; may be {@code null}
     * @return the German display label, or an empty string when {@code family} is {@code null}
     */
    @Override
    public String toString(AiProviderFamily family) {
        if (family == null) {
            return "";
        }
        return switch (family) {
            case CLAUDE -> "Claude";
            case OPENAI_COMPATIBLE -> "OpenAI-kompatibel";
        };
    }

    /**
     * Resolves a German display label back to its {@link AiProviderFamily} constant.
     *
     * @param label the display label as produced by {@link #toString(AiProviderFamily)};
     *              may be {@code null}
     * @return the matching constant, or {@code null} when the label is not recognised
     */
    @Override
    public AiProviderFamily fromString(String label) {
        if (label == null) {
            return null;
        }
        return switch (label) {
            case "Claude" -> AiProviderFamily.CLAUDE;
            case "OpenAI-kompatibel" -> AiProviderFamily.OPENAI_COMPATIBLE;
            default -> null;
        };
    }
}
