"""
Tests for get_setting_from_snapshot() child key search logic
in config/thread_settings.py.

Tests cover:
- Exact key match (full and simplified format)
- Child key aggregation (prefix.suffix → {suffix: value})
- Multiple child keys
- NoSettingsContextError when no default and no match
- Default returned when key missing
- Interaction with check_fallback_llm
"""

import pytest


class TestGetSettingFromSnapshotExactMatch:
    """Tests for exact key matching in get_setting_from_snapshot()."""

    def test_exact_key_full_format(self):
        """Exact key match with full format dict extracts typed value."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"search.engine": {"value": "google", "ui_element": "text"}}
        result = get_setting_from_snapshot(
            "search.engine", settings_snapshot=snapshot
        )
        assert result == "google"

    def test_exact_key_simplified_format(self):
        """Exact key match with simplified value returns value directly."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"search.engine": "google"}
        result = get_setting_from_snapshot(
            "search.engine", settings_snapshot=snapshot
        )
        assert result == "google"

    def test_exact_key_integer_value(self):
        """Exact key with integer value works."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"max_results": 50}
        result = get_setting_from_snapshot(
            "max_results", settings_snapshot=snapshot
        )
        assert result == 50

    def test_exact_key_boolean_value(self):
        """Exact key with boolean value works."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"enabled": True}
        result = get_setting_from_snapshot(
            "enabled", settings_snapshot=snapshot
        )
        assert result is True


class TestGetSettingFromSnapshotChildKeys:
    """Tests for child key aggregation logic."""

    def test_single_child_key(self):
        """Single child key builds a dict with just that suffix."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"search.engine": "google"}
        result = get_setting_from_snapshot("search", settings_snapshot=snapshot)
        assert isinstance(result, dict)
        assert result["engine"] == "google"

    def test_multiple_child_keys(self):
        """Multiple child keys are aggregated into one dict."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {
            "search.engine": "google",
            "search.timeout": 30,
            "search.retries": 3,
        }
        result = get_setting_from_snapshot("search", settings_snapshot=snapshot)
        assert result == {"engine": "google", "timeout": 30, "retries": 3}

    def test_child_keys_full_format(self):
        """Child keys with full format dicts extract values."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {
            "search.engine": {"value": "bing", "ui_element": "text"},
        }
        result = get_setting_from_snapshot("search", settings_snapshot=snapshot)
        assert isinstance(result, dict)
        assert result["engine"] == "bing"

    def test_no_child_keys_match(self):
        """No child keys match → falls through to default/error."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {"other.key": "value"}
        result = get_setting_from_snapshot(
            "search", default="fallback", settings_snapshot=snapshot
        )
        assert result == "fallback"

    def test_child_keys_not_confused_with_partial_prefix(self):
        """Key 'search_extra.foo' should NOT match prefix 'search.'."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        snapshot = {
            "search_extra.engine": "nope",
            "search.engine": "yes",
        }
        result = get_setting_from_snapshot("search", settings_snapshot=snapshot)
        assert result == {"engine": "yes"}


class TestGetSettingFromSnapshotDefaults:
    """Tests for default values and error raising."""

    def test_default_returned_when_key_missing(self):
        """When key not in snapshot and default given, return default."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        result = get_setting_from_snapshot(
            "nonexistent", default=42, settings_snapshot={"other": "val"}
        )
        assert result == 42

    def test_no_settings_context_error_no_default(self):
        """No snapshot match, no context, no default → NoSettingsContextError."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
            NoSettingsContextError,
        )

        with pytest.raises(NoSettingsContextError):
            get_setting_from_snapshot(
                "missing_key", settings_snapshot={"other": "val"}
            )

    def test_none_default_still_raises(self):
        """default=None (the default) still raises NoSettingsContextError."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
            NoSettingsContextError,
        )

        with pytest.raises(NoSettingsContextError):
            get_setting_from_snapshot(
                "missing_key", default=None, settings_snapshot={"other": "val"}
            )

    def test_empty_snapshot_with_default(self):
        """Empty snapshot with default returns the default."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        result = get_setting_from_snapshot(
            "key", default="default_val", settings_snapshot={}
        )
        assert result == "default_val"

    def test_none_snapshot_with_default(self):
        """None snapshot with default returns the default."""
        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        result = get_setting_from_snapshot(
            "key", default="fallback", settings_snapshot=None
        )
        assert result == "fallback"


class TestGetSettingFromSnapshotFallbackLlm:
    """Tests for check_fallback_llm behavior."""

    def test_fallback_llm_env_returns_default(self, monkeypatch):
        """With LDR_USE_FALLBACK_LLM set, returns default."""
        monkeypatch.setenv("LDR_USE_FALLBACK_LLM", "1")
        monkeypatch.delenv("LDR_TESTING_WITH_MOCKS", raising=False)

        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        result = get_setting_from_snapshot(
            "missing",
            default="fallback_val",
            settings_snapshot={"other": "x"},
            check_fallback_llm=True,
        )
        assert result == "fallback_val"

    def test_testing_with_mocks_skips_fallback(self, monkeypatch):
        """With both LDR_USE_FALLBACK_LLM and LDR_TESTING_WITH_MOCKS, doesn't use fallback path."""
        monkeypatch.setenv("LDR_USE_FALLBACK_LLM", "1")
        monkeypatch.setenv("LDR_TESTING_WITH_MOCKS", "1")

        from local_deep_research.config.thread_settings import (
            get_setting_from_snapshot,
        )

        # Should not short-circuit to fallback — should return default via normal path
        result = get_setting_from_snapshot(
            "missing",
            default="normal_default",
            settings_snapshot={"other": "x"},
            check_fallback_llm=True,
        )
        assert result == "normal_default"
