"""
Tests for uncovered code paths in embeddings_config.py.

Targets:
- get_embeddings: invalid provider, None provider from settings, provider normalization
- get_embedding_function: returns callable
- get_available_embedding_providers: with mocked availability checks
- _get_provider_classes: lazy loading and caching
"""

from unittest.mock import Mock, patch

import pytest

MODULE = "local_deep_research.embeddings.embeddings_config"


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

    def test_invalid_provider_raises(self):
        """Invalid provider raises ValueError."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with pytest.raises(ValueError, match="Invalid embedding provider"):
            get_embeddings(provider="nonexistent_provider")

    def test_provider_normalization(self):
        """Provider string is normalized (stripped, lowered)."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with patch(f"{MODULE}._get_provider_classes") as mock_classes:
            mock_provider = Mock()
            mock_provider.create_embeddings.return_value = Mock()
            mock_classes.return_value = {
                "sentence_transformers": mock_provider,
                "ollama": Mock(),
                "openai": Mock(),
            }

            get_embeddings(provider='  "sentence_transformers"  ')

            mock_provider.create_embeddings.assert_called_once()

    def test_provider_from_settings_snapshot(self):
        """Provider is read from settings when not specified."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with patch(
            f"{MODULE}.get_setting_from_snapshot", return_value="ollama"
        ):
            with patch(f"{MODULE}._get_provider_classes") as mock_classes:
                mock_provider = Mock()
                mock_provider.create_embeddings.return_value = Mock()
                mock_classes.return_value = {
                    "sentence_transformers": Mock(),
                    "ollama": mock_provider,
                    "openai": Mock(),
                }

                get_embeddings(
                    settings_snapshot={
                        "embeddings.provider": "ollama",
                        "search.tool": "searxng",
                    }
                )

                mock_provider.create_embeddings.assert_called_once()

    def test_model_passed_to_provider(self):
        """Model name is passed through to provider."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embeddings,
        )

        with patch(f"{MODULE}._get_provider_classes") as mock_classes:
            mock_provider = Mock()
            mock_provider.create_embeddings.return_value = Mock()
            mock_classes.return_value = {
                "sentence_transformers": mock_provider,
                "ollama": Mock(),
                "openai": Mock(),
            }

            get_embeddings(
                provider="sentence_transformers", model="all-MiniLM-L6-v2"
            )

            mock_provider.create_embeddings.assert_called_once_with(
                model="all-MiniLM-L6-v2", settings_snapshot=None
            )


class TestGetEmbeddingFunction:
    """Tests for get_embedding_function."""

    def test_returns_callable(self):
        """Returns embed_documents method from embeddings object."""
        from local_deep_research.embeddings.embeddings_config import (
            get_embedding_function,
        )

        with patch(f"{MODULE}.get_embeddings") as mock_get:
            mock_embeddings = Mock()
            mock_embeddings.embed_documents = Mock(return_value=[[0.1, 0.2]])
            mock_get.return_value = mock_embeddings

            fn = get_embedding_function(provider="sentence_transformers")

            assert callable(fn)
            result = fn(["test text"])
            assert result == [[0.1, 0.2]]


class TestGetAvailableProviders:
    """Tests for get_available_embedding_providers."""

    def test_all_available(self):
        """Returns all providers when all are available."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        with patch(
            f"{MODULE}.is_sentence_transformers_available", return_value=True
        ):
            with patch(
                f"{MODULE}.is_ollama_embeddings_available", return_value=True
            ):
                with patch(
                    f"{MODULE}.is_openai_embeddings_available",
                    return_value=True,
                ):
                    providers = get_available_embedding_providers()

        assert "sentence_transformers" in providers
        assert "ollama" in providers
        assert "openai" in providers

    def test_none_available(self):
        """Returns empty dict when no providers available."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        with patch(
            f"{MODULE}.is_sentence_transformers_available", return_value=False
        ):
            with patch(
                f"{MODULE}.is_ollama_embeddings_available", return_value=False
            ):
                with patch(
                    f"{MODULE}.is_openai_embeddings_available",
                    return_value=False,
                ):
                    providers = get_available_embedding_providers()

        assert providers == {}

    def test_partial_availability(self):
        """Returns only available providers."""
        from local_deep_research.embeddings.embeddings_config import (
            get_available_embedding_providers,
        )

        with patch(
            f"{MODULE}.is_sentence_transformers_available", return_value=True
        ):
            with patch(
                f"{MODULE}.is_ollama_embeddings_available", return_value=False
            ):
                with patch(
                    f"{MODULE}.is_openai_embeddings_available",
                    return_value=True,
                ):
                    providers = get_available_embedding_providers()

        assert "sentence_transformers" in providers
        assert "ollama" not in providers
        assert "openai" in providers


class TestProviderAvailabilityChecks:
    """Tests for individual provider availability checks."""

    def test_sentence_transformers_available(self):
        """is_sentence_transformers_available delegates to provider."""
        from local_deep_research.embeddings.embeddings_config import (
            is_sentence_transformers_available,
        )

        with patch(f"{MODULE}._get_provider_classes") as mock_classes:
            mock_st = Mock()
            mock_st.is_available.return_value = True
            mock_classes.return_value = {"sentence_transformers": mock_st}

            assert is_sentence_transformers_available() is True
            mock_st.is_available.assert_called_once()

    def test_ollama_available_with_snapshot(self):
        """is_ollama_embeddings_available passes settings_snapshot."""
        from local_deep_research.embeddings.embeddings_config import (
            is_ollama_embeddings_available,
        )

        snapshot = {"ollama.url": "http://localhost:11434"}

        with patch(f"{MODULE}._get_provider_classes") as mock_classes:
            mock_ollama = Mock()
            mock_ollama.is_available.return_value = False
            mock_classes.return_value = {"ollama": mock_ollama}

            result = is_ollama_embeddings_available(snapshot)

            assert result is False
            mock_ollama.is_available.assert_called_once_with(snapshot)

    def test_openai_available(self):
        """is_openai_embeddings_available delegates to provider."""
        from local_deep_research.embeddings.embeddings_config import (
            is_openai_embeddings_available,
        )

        with patch(f"{MODULE}._get_provider_classes") as mock_classes:
            mock_openai = Mock()
            mock_openai.is_available.return_value = True
            mock_classes.return_value = {"openai": mock_openai}

            assert is_openai_embeddings_available() is True
