"""Extra coverage tests for config/thread_settings.py — untested branches."""

from unittest.mock import patch

import pytest

from local_deep_research.config.thread_settings import (
    NoSettingsContextError,
    get_bool_setting_from_snapshot,
    get_setting_from_snapshot,
)

MODULE = "local_deep_research.config.thread_settings"


class TestGetSettingFromSnapshotChildKeys:
    def test_child_keys_built_into_dict(self):
        """When snapshot has 'parent.child' keys, they're assembled into a dict."""
        snapshot = {
            "search.engine.web.searxng.url": "http://localhost:8888",
            "search.engine.web.searxng.enabled": True,
        }
        result = get_setting_from_snapshot(
            "search.engine.web.searxng",
            settings_snapshot=snapshot,
        )
        assert isinstance(result, dict)
        assert result["url"] == "http://localhost:8888"
        assert result["enabled"] is True

    def test_child_keys_with_value_dict_format(self):
        """Child keys in {'value': x, 'ui_element': y} format are extracted."""
        snapshot = {
            "llm.provider.model": {"value": "gpt-4", "ui_element": "text"},
        }
        result = get_setting_from_snapshot(
            "llm.provider",
            settings_snapshot=snapshot,
        )
        assert isinstance(result, dict)
        assert result["model"] == "gpt-4"

    def test_child_keys_with_simplified_format(self):
        """Child keys as raw values (not dicts) pass through directly."""
        snapshot = {
            "app.settings.theme": "dark",
            "app.settings.lang": "en",
        }
        result = get_setting_from_snapshot(
            "app.settings",
            settings_snapshot=snapshot,
        )
        assert result == {"theme": "dark", "lang": "en"}

    def test_direct_key_takes_priority(self):
        """Direct key match is returned before child key search."""
        snapshot = {
            "key": "direct_value",
            "key.child": "child_value",
        }
        result = get_setting_from_snapshot("key", settings_snapshot=snapshot)
        assert result == "direct_value"


class TestGetSettingFromSnapshotValueExtraction:
    def test_dict_with_value_key_extracted(self):
        snapshot = {"my.setting": {"value": 42, "ui_element": "number"}}
        result = get_setting_from_snapshot(
            "my.setting", settings_snapshot=snapshot
        )
        assert result == 42

    def test_dict_without_value_key_returned_as_is(self):
        snapshot = {"my.setting": {"custom_field": "data"}}
        result = get_setting_from_snapshot(
            "my.setting", settings_snapshot=snapshot
        )
        assert result == {"custom_field": "data"}

    def test_plain_value_returned_directly(self):
        snapshot = {"my.setting": "plain_string"}
        result = get_setting_from_snapshot(
            "my.setting", settings_snapshot=snapshot
        )
        assert result == "plain_string"


class TestGetSettingFromSnapshotFallbacks:
    def test_settings_context_fallback(self):
        """When snapshot doesn't have key, thread context is checked."""
        mock_ctx = type(
            "Ctx", (), {"get_setting": lambda s, k, d: "from_ctx"}
        )()

        with patch(f"{MODULE}._thread_local") as mock_tl:
            mock_tl.settings_context = mock_ctx
            result = get_setting_from_snapshot("missing.key")

        assert result == "from_ctx"

    def test_default_returned_when_nothing_found(self):
        result = get_setting_from_snapshot(
            "nonexistent.key",
            default="fallback",
            settings_snapshot={},
        )
        assert result == "fallback"

    def test_raises_when_no_default_and_nothing_found(self):
        with pytest.raises(NoSettingsContextError):
            get_setting_from_snapshot(
                "nonexistent.key",
                settings_snapshot={},
            )


class TestGetBoolSettingFromSnapshot:
    def test_true_string(self):
        snapshot = {"feature.enabled": "true"}
        assert (
            get_bool_setting_from_snapshot(
                "feature.enabled", settings_snapshot=snapshot
            )
            is True
        )

    def test_false_string(self):
        snapshot = {"feature.enabled": "false"}
        assert (
            get_bool_setting_from_snapshot(
                "feature.enabled", settings_snapshot=snapshot
            )
            is False
        )

    def test_default_when_missing(self):
        assert (
            get_bool_setting_from_snapshot(
                "missing.key", default=True, settings_snapshot={}
            )
            is True
        )

    def test_integer_one(self):
        snapshot = {"flag": 1}
        assert (
            get_bool_setting_from_snapshot("flag", settings_snapshot=snapshot)
            is True
        )

    def test_integer_zero(self):
        snapshot = {"flag": 0}
        assert (
            get_bool_setting_from_snapshot("flag", settings_snapshot=snapshot)
            is False
        )

    def test_bool_passthrough(self):
        snapshot = {"flag": True}
        assert (
            get_bool_setting_from_snapshot("flag", settings_snapshot=snapshot)
            is True
        )

    def test_yes_string(self):
        snapshot = {"flag": "yes"}
        assert (
            get_bool_setting_from_snapshot("flag", settings_snapshot=snapshot)
            is True
        )

    def test_on_string(self):
        snapshot = {"flag": "on"}
        assert (
            get_bool_setting_from_snapshot("flag", settings_snapshot=snapshot)
            is True
        )
