"""Extra coverage tests for settings/manager.py — uncovered branches.

Targets:
- default_settings: theme registry ImportError (line 389-391), file loading exception (375-379)
- set_setting: create new setting with type inference from key prefix (607-613)
- get_all_settings: env var override for defaults (667-676)
- _parse_number, _parse_json_value, _parse_multiselect edge cases
- _infer_ui_element type detection
"""

from unittest.mock import patch

import pytest

MODULE = "local_deep_research.settings.manager"


# ===========================================================================
# _parse_number
# ===========================================================================


class TestParseNumber:
    def test_integer_string(self):
        from local_deep_research.settings.manager import _parse_number

        assert _parse_number("42") == 42

    def test_float_string(self):
        from local_deep_research.settings.manager import _parse_number

        assert _parse_number("3.14") == 3.14

    def test_whole_float_returns_int(self):
        from local_deep_research.settings.manager import _parse_number

        result = _parse_number("5.0")
        assert result == 5
        assert isinstance(result, int)

    def test_invalid_raises_value_error(self):
        from local_deep_research.settings.manager import _parse_number

        with pytest.raises(ValueError):
            _parse_number("not a number")

    def test_empty_string_raises(self):
        from local_deep_research.settings.manager import _parse_number

        with pytest.raises(ValueError):
            _parse_number("")


# ===========================================================================
# _parse_json_value
# ===========================================================================


class TestParseJsonValue:
    def test_valid_json_dict(self):
        from local_deep_research.settings.manager import _parse_json_value

        assert _parse_json_value('{"key": "val"}') == {"key": "val"}

    def test_valid_json_list(self):
        from local_deep_research.settings.manager import _parse_json_value

        assert _parse_json_value("[1, 2, 3]") == [1, 2, 3]

    def test_invalid_json_returns_original(self):
        from local_deep_research.settings.manager import _parse_json_value

        assert _parse_json_value("not json") == "not json"

    def test_empty_string(self):
        from local_deep_research.settings.manager import _parse_json_value

        assert _parse_json_value("") == ""


# ===========================================================================
# _parse_multiselect
# ===========================================================================


class TestParseMultiselect:
    def test_list_passthrough(self):
        from local_deep_research.settings.manager import _parse_multiselect

        assert _parse_multiselect(["a", "b"]) == ["a", "b"]

    def test_csv_string(self):
        from local_deep_research.settings.manager import _parse_multiselect

        assert _parse_multiselect("a,b,c") == ["a", "b", "c"]

    def test_csv_with_whitespace(self):
        from local_deep_research.settings.manager import _parse_multiselect

        assert _parse_multiselect(" a , b , c ") == ["a", "b", "c"]

    def test_empty_string(self):
        from local_deep_research.settings.manager import _parse_multiselect

        result = _parse_multiselect("")
        assert isinstance(result, list)

    def test_json_list_string(self):
        from local_deep_research.settings.manager import _parse_multiselect

        result = _parse_multiselect('["x", "y"]')
        assert result == ["x", "y"]


# ===========================================================================
# _infer_ui_element
# ===========================================================================


class TestInferUiElement:
    def test_bool_returns_checkbox(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(True) == "checkbox"

    def test_int_returns_number(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(42) == "number"

    def test_float_returns_number(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(3.14) == "number"

    def test_list_returns_json(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(["a", "b"]) == "json"

    def test_dict_returns_json(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element({"k": "v"}) == "json"

    def test_string_returns_text(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element("hello") == "text"

    def test_none_returns_text(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(None) == "text"

    def test_existing_element_preserved(self):
        from local_deep_research.settings.manager import _infer_ui_element

        assert _infer_ui_element(True, "select") == "select"


# ===========================================================================
# _filter_setting_columns
# ===========================================================================


class TestFilterSettingColumns:
    def test_filters_to_valid_columns(self):
        from local_deep_research.settings.manager import _filter_setting_columns

        data = {
            "key": "test.key",
            "value": "val",
            "invalid_column": "should be removed",
        }
        result = _filter_setting_columns(data)
        assert "key" in result
        assert "value" in result
        # invalid_column should not be in result if it's not a Setting column


# ===========================================================================
# get_typed_setting_value — edge cases
# ===========================================================================


class TestGetTypedSettingValue:
    def test_checkbox_true_string(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        assert get_typed_setting_value("test", "true", "checkbox") is True

    def test_checkbox_false_string(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        assert get_typed_setting_value("test", "false", "checkbox") is False

    def test_number_element(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        assert get_typed_setting_value("test", "42", "number") == 42

    def test_text_element(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        assert get_typed_setting_value("test", "hello", "text") == "hello"

    def test_multiselect_csv(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        result = get_typed_setting_value("test", "a,b,c", "multiselect")
        assert result == ["a", "b", "c"]

    def test_json_element(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        result = get_typed_setting_value("test", '{"k": "v"}', "json")
        assert result == {"k": "v"}

    def test_default_when_none(self):
        from local_deep_research.settings.manager import get_typed_setting_value

        result = get_typed_setting_value("test", None, "text", default="def")
        assert result == "def"


# ===========================================================================
# check_env_setting
# ===========================================================================


class TestCheckEnvSetting:
    def test_env_var_found(self):
        from local_deep_research.settings.manager import check_env_setting

        with patch.dict("os.environ", {"LDR_LLM_PROVIDER": "anthropic"}):
            result = check_env_setting("llm.provider")
        assert result == "anthropic"

    def test_env_var_not_found(self):
        from local_deep_research.settings.manager import check_env_setting

        result = check_env_setting("nonexistent.setting.key")
        assert result is None

    def test_key_conversion(self):
        """Keys like 'search.tool' become 'LDR_SEARCH_TOOL'."""
        from local_deep_research.settings.manager import check_env_setting

        with patch.dict("os.environ", {"LDR_SEARCH_TOOL": "searxng"}):
            result = check_env_setting("search.tool")
        assert result == "searxng"
