"""
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
"""

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"
