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

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;

import java.util.Optional;

import org.junit.jupiter.api.Test;

/**
 * Tests for {@link EffectiveApiKeyDescriptor}.
 */
class EffectiveApiKeyDescriptorTest {

    @Test
    void fromProviderEnvVar_setsOriginAndVarName() {
        var descriptor = EffectiveApiKeyDescriptor.fromProviderEnvVar("ANTHROPIC_API_KEY");

        assertThat(descriptor.origin()).isEqualTo(ApiKeyOrigin.FROM_PROVIDER_ENV_VAR);
        assertThat(descriptor.envVarName()).contains("ANTHROPIC_API_KEY");
        assertThat(descriptor.isFromEnvironmentVariable()).isTrue();
        assertThat(descriptor.isAbsent()).isFalse();
    }

    @Test
    void fromLegacyEnvVar_setsOriginAndVarName() {
        var descriptor = EffectiveApiKeyDescriptor.fromLegacyEnvVar("OPENAI_API_KEY");

        assertThat(descriptor.origin()).isEqualTo(ApiKeyOrigin.FROM_LEGACY_ENV_VAR);
        assertThat(descriptor.envVarName()).contains("OPENAI_API_KEY");
        assertThat(descriptor.isFromEnvironmentVariable()).isTrue();
        assertThat(descriptor.isAbsent()).isFalse();
    }

    @Test
    void fromPropertyFile_setsOriginAndEmptyVarName() {
        var descriptor = EffectiveApiKeyDescriptor.fromPropertyFile();

        assertThat(descriptor.origin()).isEqualTo(ApiKeyOrigin.FROM_PROPERTY_FILE);
        assertThat(descriptor.envVarName()).isEmpty();
        assertThat(descriptor.isFromEnvironmentVariable()).isFalse();
        assertThat(descriptor.isAbsent()).isFalse();
    }

    @Test
    void absent_setsAbsentOriginAndEmptyVarName() {
        var descriptor = EffectiveApiKeyDescriptor.absent();

        assertThat(descriptor.origin()).isEqualTo(ApiKeyOrigin.ABSENT);
        assertThat(descriptor.envVarName()).isEmpty();
        assertThat(descriptor.isFromEnvironmentVariable()).isFalse();
        assertThat(descriptor.isAbsent()).isTrue();
    }

    @Test
    void nullEnvVarNameBecomesEmpty() {
        var descriptor = new EffectiveApiKeyDescriptor(ApiKeyOrigin.FROM_PROPERTY_FILE, null);
        assertThat(descriptor.envVarName()).isEqualTo(Optional.empty());
    }

    @Test
    void rejectsNullOrigin() {
        assertThatNullPointerException()
                .isThrownBy(() -> new EffectiveApiKeyDescriptor(null, Optional.empty()));
    }

    @Test
    void fromProviderEnvVar_rejectsNullName() {
        assertThatNullPointerException()
                .isThrownBy(() -> EffectiveApiKeyDescriptor.fromProviderEnvVar(null));
    }

    @Test
    void fromLegacyEnvVar_rejectsNullName() {
        assertThatNullPointerException()
                .isThrownBy(() -> EffectiveApiKeyDescriptor.fromLegacyEnvVar(null));
    }

    @Test
    void equality_basedOnAllFields() {
        var a = EffectiveApiKeyDescriptor.fromProviderEnvVar("VAR_A");
        var b = EffectiveApiKeyDescriptor.fromProviderEnvVar("VAR_A");
        var c = EffectiveApiKeyDescriptor.fromProviderEnvVar("VAR_B");

        assertThat(a).isEqualTo(b);
        assertThat(a).isNotEqualTo(c);
    }
}
