"""Extra coverage tests for config/llm_config.py — context window and provider selection.

Targets uncovered branches:
- _get_context_window_for_provider() all branches
- get_selected_llm_provider()
"""

from unittest.mock import patch


MODULE = "local_deep_research.config.llm_config"
# Context-window resolution delegates to the _helpers twin, which reads
# settings via thread_settings.get_setting_from_snapshot. Patch this module
# (not MODULE) for context-window tests.
THREAD_SETTINGS = "local_deep_research.config.thread_settings"


# ===========================================================================
# _get_context_window_for_provider
# ===========================================================================


class TestGetContextWindowForProvider:
    def test_local_provider_default(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot", return_value=4096
        ):
            result = _get_context_window_for_provider("ollama")

        assert result == 4096

    def test_local_provider_custom_size(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot", return_value=8192
        ):
            result = _get_context_window_for_provider("llamacpp")

        assert result == 8192

    def test_local_provider_none_returns_default(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot", return_value=None
        ):
            result = _get_context_window_for_provider("lmstudio")

        assert result == 8192

    def test_cloud_provider_unrestricted(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot", return_value=True
        ):
            result = _get_context_window_for_provider("openai")

        assert result is None

    def test_cloud_provider_restricted_custom_size(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        call_count = 0

        def setting_side_effect(key, default=None, **kwargs):
            nonlocal call_count
            call_count += 1
            if call_count == 1:
                return False  # unrestricted = False
            return 64000  # custom window size

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot",
            side_effect=setting_side_effect,
        ):
            result = _get_context_window_for_provider("anthropic")

        assert result == 64000

    def test_cloud_provider_restricted_none_returns_default(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        call_count = 0

        def setting_side_effect(key, default=None, **kwargs):
            nonlocal call_count
            call_count += 1
            if call_count == 1:
                return False  # unrestricted = False
            return None  # no custom size

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot",
            side_effect=setting_side_effect,
        ):
            result = _get_context_window_for_provider("openrouter")

        assert result == 128000

    def test_local_provider_string_converted(self):
        from local_deep_research.config.llm_config import (
            _get_context_window_for_provider,
        )

        with patch(
            f"{THREAD_SETTINGS}.get_setting_from_snapshot",
            return_value="16384",
        ):
            result = _get_context_window_for_provider("ollama")

        assert result == 16384


# ===========================================================================
# get_selected_llm_provider
# ===========================================================================


class TestGetSelectedLlmProvider:
    def test_returns_lowercase(self):
        from local_deep_research.config.llm_config import (
            get_selected_llm_provider,
        )

        with patch(
            f"{MODULE}.get_setting_from_snapshot", return_value="OLLAMA"
        ):
            assert get_selected_llm_provider() == "ollama"

    def test_default_ollama(self):
        from local_deep_research.config.llm_config import (
            get_selected_llm_provider,
        )

        result = get_selected_llm_provider(
            settings_snapshot={"llm.provider": "anthropic"}
        )
        assert result == "anthropic"
