"""Tests for InMemorySettingsManager._get_typed_value.

This method dispatches value type conversion based on ui_element type.
Critical for ensuring settings are stored with correct types. Edge
cases around type conversion failures are untested.
"""

import pytest

from local_deep_research.api.settings_utils import InMemorySettingsManager


@pytest.fixture
def manager():
    """Create an InMemorySettingsManager instance."""
    return InMemorySettingsManager()


class TestGetTypedValueText:
    """Text type conversion — string passthrough."""

    def test_string_passthrough(self, manager):
        result = manager._get_typed_value({"ui_element": "text"}, "hello")
        assert result == "hello"

    def test_int_converted_to_string(self, manager):
        result = manager._get_typed_value({"ui_element": "text"}, 42)
        assert result == "42"


class TestGetTypedValueCheckbox:
    """Checkbox type conversion — uses parse_boolean."""

    def test_true_string(self, manager):
        result = manager._get_typed_value({"ui_element": "checkbox"}, "true")
        assert result is True

    def test_false_string(self, manager):
        result = manager._get_typed_value({"ui_element": "checkbox"}, "false")
        assert result is False

    def test_bool_passthrough(self, manager):
        result = manager._get_typed_value({"ui_element": "checkbox"}, True)
        assert result is True

    def test_zero_is_false(self, manager):
        result = manager._get_typed_value({"ui_element": "checkbox"}, 0)
        assert result is False


class TestGetTypedValueNumber:
    """Number type conversion — uses _parse_number."""

    def test_integer_string(self, manager):
        result = manager._get_typed_value({"ui_element": "number"}, "42")
        assert result == 42
        assert isinstance(result, int)

    def test_float_string(self, manager):
        result = manager._get_typed_value({"ui_element": "number"}, "3.14")
        assert result == 3.14
        assert isinstance(result, float)

    def test_int_passthrough(self, manager):
        result = manager._get_typed_value({"ui_element": "number"}, 42)
        assert result == 42

    def test_float_whole_number_becomes_int(self, manager):
        result = manager._get_typed_value({"ui_element": "number"}, "5.0")
        assert result == 5
        assert isinstance(result, int)


class TestGetTypedValueRange:
    """Range type (slider) — same behavior as number."""

    def test_range_like_number(self, manager):
        result = manager._get_typed_value({"ui_element": "range"}, "0.7")
        assert result == 0.7


class TestGetTypedValueSelect:
    """Select type — string passthrough."""

    def test_string_passthrough(self, manager):
        result = manager._get_typed_value({"ui_element": "select"}, "option_a")
        assert result == "option_a"


class TestGetTypedValueMultiselect:
    """Multiselect type — uses _parse_multiselect."""

    def test_list_passthrough(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "multiselect"}, ["a", "b"]
        )
        assert result == ["a", "b"]

    def test_json_string_parsed(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "multiselect"}, '["x","y"]'
        )
        assert result == ["x", "y"]

    def test_comma_separated_parsed(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "multiselect"}, "a,b,c"
        )
        assert result == ["a", "b", "c"]


class TestGetTypedValueJSON:
    """JSON type — value passed through as-is."""

    def test_dict_passthrough(self, manager):
        val = {"key": "value"}
        result = manager._get_typed_value({"ui_element": "json"}, val)
        assert result == val

    def test_list_passthrough(self, manager):
        val = [1, 2, 3]
        result = manager._get_typed_value({"ui_element": "json"}, val)
        assert result == val


class TestGetTypedValuePassword:
    """Password type — string passthrough."""

    def test_password_string(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "password"}, "secret123"
        )
        assert result == "secret123"


class TestGetTypedValueUnknownType:
    """Unknown ui_element types — value returned as-is."""

    def test_unknown_type_returns_value(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "custom_widget"}, "val"
        )
        assert result == "val"

    def test_missing_ui_element_defaults_to_text(self, manager):
        result = manager._get_typed_value({}, "val")
        assert result == "val"


class TestGetTypedValueConversionFailure:
    """Type conversion failures — value returned as-is."""

    def test_non_numeric_for_number(self, manager):
        result = manager._get_typed_value(
            {"ui_element": "number"}, "not_a_number"
        )
        assert result == "not_a_number"

    def test_none_for_number(self, manager):
        result = manager._get_typed_value({"ui_element": "number"}, None)
        assert result is None
