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

import java.util.Objects;

/**
 * Editable state for a provider-specific API key.
 * <p>
 * This value object keeps the editable property value separate from the later displayable
 * origin of the effective key value. The editor can therefore preserve the property text
 * while later layers determine whether the effective value comes from the file, an
 * environment variable, a compatibility fallback, or a technical default.
 *
 * @param propertyValue editable API-key property value as entered or loaded from the file
 * @param effectiveValueOrigin provenance of the effective value shown or inferred later
 * @param effectiveValueDescription human-readable provenance description for later display
 */
public record GuiProviderApiKeyState(
        String propertyValue,
        GuiValueOrigin effectiveValueOrigin,
        String effectiveValueDescription) {

    /**
     * Creates a key state with a blank property value and unresolved provenance.
     *
     * @return a new unresolved API-key state
     */
    public static GuiProviderApiKeyState unresolved() {
        return new GuiProviderApiKeyState("", GuiValueOrigin.UNKNOWN, "nicht aufgeloest");
    }

    /**
     * Creates a key state with the supplied property value and unresolved provenance.
     *
     * @param propertyValue editable property value; may be {@code null}
     * @return a new unresolved API-key state
     */
    public static GuiProviderApiKeyState unresolved(String propertyValue) {
        return new GuiProviderApiKeyState(propertyValue, GuiValueOrigin.UNKNOWN, "nicht aufgeloest");
    }

    /**
     * Creates a key state with an explicit effective-value provenance.
     *
     * @param propertyValue editable property value; may be {@code null}
     * @param effectiveValueOrigin origin of the effective value; must not be {@code null}
     * @param effectiveValueDescription human-readable provenance description; may be {@code null}
     * @return a new API-key state with the requested provenance
     */
    public static GuiProviderApiKeyState resolved(String propertyValue,
                                                  GuiValueOrigin effectiveValueOrigin,
                                                  String effectiveValueDescription) {
        return new GuiProviderApiKeyState(propertyValue, effectiveValueOrigin, effectiveValueDescription);
    }

    /**
     * Creates a new key state.
     *
     * @param propertyValue editable property value; {@code null} is converted to an empty string
     * @param effectiveValueOrigin origin of the effective value; {@code null} becomes {@link GuiValueOrigin#UNKNOWN}
     * @param effectiveValueDescription human-readable provenance description; {@code null} becomes an empty string
     */
    public GuiProviderApiKeyState {
        propertyValue = propertyValue == null ? "" : propertyValue;
        effectiveValueOrigin = Objects.requireNonNullElse(effectiveValueOrigin, GuiValueOrigin.UNKNOWN);
        effectiveValueDescription = effectiveValueDescription == null ? "" : effectiveValueDescription;
    }
}
