"""
Behavioral tests for embeddings_config module.

Tests embedding provider availability checks and configuration.
"""

import pytest


class TestValidEmbeddingProviders:
    """Tests for VALID_EMBEDDING_PROVIDERS constant."""

    def test_is_list(self):
        """VALID_EMBEDDING_PROVIDERS is a list."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        assert isinstance(VALID_EMBEDDING_PROVIDERS, list)

    def test_includes_sentence_transformers(self):
        """Includes sentence_transformers provider."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        assert "sentence_transformers" in VALID_EMBEDDING_PROVIDERS

    def test_includes_ollama(self):
        """Includes ollama provider."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        assert "ollama" in VALID_EMBEDDING_PROVIDERS

    def test_includes_openai(self):
        """Includes openai provider."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        assert "openai" in VALID_EMBEDDING_PROVIDERS

    def test_all_values_are_strings(self):
        """All values are strings."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        for provider in VALID_EMBEDDING_PROVIDERS:
            assert isinstance(provider, str)

    def test_all_values_are_lowercase(self):
        """All values are lowercase."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
        )

        for provider in VALID_EMBEDDING_PROVIDERS:
            assert provider == provider.lower()


class TestIsSentenceTransformersAvailable:
    """Tests for is_sentence_transformers_available function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.embeddings.embeddings_config import (
            is_sentence_transformers_available,
        )

        result = is_sentence_transformers_available()
        assert isinstance(result, bool)

    def test_callable_without_arguments(self):
        """Can be called without arguments."""
        from local_deep_research.embeddings.embeddings_config import (
            is_sentence_transformers_available,
        )

        # Should not raise
        is_sentence_transformers_available()


class TestIsOllamaEmbeddingsAvailable:
    """Tests for is_ollama_embeddings_available function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.embeddings.embeddings_config import (
            is_ollama_embeddings_available,
        )

        result = is_ollama_embeddings_available()
        assert isinstance(result, bool)

    def test_callable_without_arguments(self):
        """Can be called without arguments."""
        from local_deep_research.embeddings.embeddings_config import (
            is_ollama_embeddings_available,
        )

        # Should not raise
        is_ollama_embeddings_available()

    def test_accepts_settings_snapshot(self):
        """Accepts settings_snapshot parameter."""
        from local_deep_research.embeddings.embeddings_config import (
            is_ollama_embeddings_available,
        )

        # Should not raise
        is_ollama_embeddings_available(settings_snapshot={})


class TestIsOpenAIEmbeddingsAvailable:
    """Tests for is_openai_embeddings_available function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.embeddings.embeddings_config import (
            is_openai_embeddings_available,
        )

        result = is_openai_embeddings_available()
        assert isinstance(result, bool)

    def test_callable_without_arguments(self):
        """Can be called without arguments."""
        from local_deep_research.embeddings.embeddings_config import (
            is_openai_embeddings_available,
        )

        # Should not raise
        is_openai_embeddings_available()

    def test_accepts_settings_snapshot(self):
        """Accepts settings_snapshot parameter."""
        from local_deep_research.embeddings.embeddings_config import (
            is_openai_embeddings_available,
        )

        # Should not raise
        is_openai_embeddings_available(settings_snapshot={})


class TestGetAvailableEmbeddingProviders:
    """Tests for get_available_embedding_providers function."""

    def test_returns_dict(self):
        """Returns a dictionary."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        result = get_available_embedding_providers()
        assert isinstance(result, dict)

    def test_callable_without_arguments(self):
        """Can be called without arguments."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        # Should not raise
        get_available_embedding_providers()

    def test_accepts_settings_snapshot(self):
        """Accepts settings_snapshot parameter."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        # Should not raise
        get_available_embedding_providers(settings_snapshot={})

    def test_keys_are_strings(self):
        """Dict keys are strings."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        result = get_available_embedding_providers()
        for key in result.keys():
            assert isinstance(key, str)

    def test_values_are_strings(self):
        """Dict values are strings (display names)."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        result = get_available_embedding_providers()
        for value in result.values():
            assert isinstance(value, str)

    def test_keys_are_valid_providers(self):
        """Dict keys are valid provider names."""
        from local_deep_research.embeddings.embeddings_config import (
            VALID_EMBEDDING_PROVIDERS,
            get_available_embedding_providers,
        )

        result = get_available_embedding_providers()
        for key in result.keys():
            assert key in VALID_EMBEDDING_PROVIDERS


class TestGetEmbeddings:
    """Tests for get_embeddings function validation."""

    def test_raises_for_invalid_provider(self):
        """Raises ValueError for invalid provider."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with pytest.raises(ValueError) as exc_info:
            get_embeddings(provider="invalid_provider")
        assert "Invalid embedding provider" in str(exc_info.value)

    def test_error_message_includes_valid_providers(self):
        """Error message includes list of valid providers."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with pytest.raises(ValueError) as exc_info:
            get_embeddings(provider="invalid_provider")
        assert "sentence_transformers" in str(exc_info.value)

    def test_accepts_lowercase_provider(self):
        """Normalizes provider to lowercase."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        # This should at least get past validation (may fail for other reasons)
        # Testing that "OPENAI" normalizes to "openai"
        try:
            get_embeddings(provider="  OPENAI  ")
        except ValueError as e:
            # Should not be "Invalid embedding provider" error
            assert "Invalid embedding provider" not in str(e)
        except Exception:
            # Other errors are fine (e.g., missing API key)
            pass


class TestGetProviderClasses:
    """Tests for _get_provider_classes function."""

    def test_returns_dict(self):
        """Returns a dictionary."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result = _get_provider_classes()
        assert isinstance(result, dict)

    def test_has_sentence_transformers(self):
        """Has sentence_transformers key."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result = _get_provider_classes()
        assert "sentence_transformers" in result

    def test_has_ollama(self):
        """Has ollama key."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result = _get_provider_classes()
        assert "ollama" in result

    def test_has_openai(self):
        """Has openai key."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result = _get_provider_classes()
        assert "openai" in result

    def test_values_are_classes(self):
        """Values are class types."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result = _get_provider_classes()
        for key, value in result.items():
            assert isinstance(value, type), f"{key} is not a class"

    def test_caches_result(self):
        """Caches result on subsequent calls."""
        from local_deep_research.embeddings.embeddings_config import (
            _get_provider_classes,
        )

        result1 = _get_provider_classes()
        result2 = _get_provider_classes()
        assert result1 is result2
