"""Tests for consolidated UI element-to-type mapping (PR #1968).

Verifies:
- Single canonical UI_ELEMENT_TO_SETTING_TYPE in settings.manager
- InMemorySettingsManager and SettingsManager reuse the canonical mapping
- Mapping covers expected UI elements
"""

from local_deep_research.settings.manager import UI_ELEMENT_TO_SETTING_TYPE


class TestCanonicalMapping:
    """Verify the canonical mapping exists and is complete."""

    def test_mapping_has_expected_elements(self):
        expected = {
            "text",
            "json",
            "password",
            "select",
            "number",
            "range",
            "checkbox",
        }
        assert expected.issubset(set(UI_ELEMENT_TO_SETTING_TYPE.keys()))

    def test_text_is_str(self):
        assert UI_ELEMENT_TO_SETTING_TYPE["text"] is str

    def test_number_converts_to_numeric(self):
        """number handler should convert string to numeric type."""
        number_handler = UI_ELEMENT_TO_SETTING_TYPE["number"]
        result = number_handler("42")
        assert result == 42

    def test_checkbox_converts_to_bool(self):
        """checkbox handler should convert strings to bool."""
        checkbox_handler = UI_ELEMENT_TO_SETTING_TYPE["checkbox"]
        assert checkbox_handler("true") is True
        assert checkbox_handler("false") is False

    def test_json_is_identity(self):
        """json type should return the value as-is."""
        json_handler = UI_ELEMENT_TO_SETTING_TYPE["json"]
        test_val = {"key": "value"}
        assert json_handler(test_val) is test_val


class TestMappingReuse:
    """Verify both managers reuse the canonical mapping."""

    def test_in_memory_manager_uses_canonical(self):
        """settings_utils imports the canonical mapping from manager."""
        from local_deep_research.api import settings_utils

        assert (
            settings_utils.UI_ELEMENT_TO_SETTING_TYPE
            is UI_ELEMENT_TO_SETTING_TYPE
        )

    def test_settings_manager_in_canonical_module(self):
        """SettingsManager lives in the same module as the canonical mapping."""
        from local_deep_research.settings.manager import SettingsManager

        assert SettingsManager is not None


class TestUnifiedSettingTypeEnum:
    """Verify that DB and web layers share one canonical SettingType (issue #2079)."""

    def test_single_canonical_enum(self):
        """Both imports resolve to the exact same class object."""
        from local_deep_research.database.models.settings import (
            SettingType as DbSettingType,
        )
        from local_deep_research.web.models.settings import (
            SettingType as WebSettingType,
        )

        assert DbSettingType is WebSettingType

    def test_setting_type_is_str_enum(self):
        """SettingType inherits from str for JSON serialization."""
        from local_deep_research.database.models.settings import SettingType

        assert issubclass(SettingType, str)

    def test_pydantic_model_accepts_setting_type(self):
        """BaseSetting Pydantic model works with the canonical enum."""
        from local_deep_research.database.models.settings import SettingType
        from local_deep_research.web.models.settings import BaseSetting

        setting = BaseSetting(
            key="test.key",
            value="test",
            type=SettingType.APP,
            name="Test",
        )
        assert setting.type is SettingType.APP
