"""Tests for server_config.py uncovered branches."""

import json
from unittest.mock import patch

from local_deep_research.web.server_config import (
    _DEFAULTS,
    _load_legacy_config,
    has_legacy_customizations,
    load_server_config,
)


class TestHasLegacyCustomizations:
    def test_no_file_returns_false(self, tmp_path):
        assert has_legacy_customizations(tmp_path / "missing.json") is False

    def test_invalid_json_returns_false(self, tmp_path):
        p = tmp_path / "bad.json"
        p.write_text("not json{{{", encoding="utf-8")
        assert has_legacy_customizations(p) is False

    def test_non_dict_json_returns_false(self, tmp_path):
        p = tmp_path / "list.json"
        p.write_text("[1,2,3]", encoding="utf-8")
        assert has_legacy_customizations(p) is False

    def test_all_defaults_returns_false(self, tmp_path):
        p = tmp_path / "config.json"
        p.write_text(json.dumps(_DEFAULTS), encoding="utf-8")
        assert has_legacy_customizations(p) is False

    def test_non_default_value_returns_true(self, tmp_path):
        p = tmp_path / "config.json"
        data = {"port": 9999}
        p.write_text(json.dumps(data), encoding="utf-8")
        assert has_legacy_customizations(p) is True

    def test_unrecognized_key_returns_true(self, tmp_path):
        p = tmp_path / "config.json"
        data = {"unknown_key": "value"}
        p.write_text(json.dumps(data), encoding="utf-8")
        assert has_legacy_customizations(p) is True

    def test_empty_dict_returns_false(self, tmp_path):
        p = tmp_path / "config.json"
        p.write_text("{}", encoding="utf-8")
        assert has_legacy_customizations(p) is False


class TestLoadLegacyConfig:
    def test_no_file_returns_empty(self, tmp_path):
        with patch(
            "local_deep_research.web.server_config.get_server_config_path",
            return_value=tmp_path / "missing.json",
        ):
            assert _load_legacy_config() == {}

    def test_invalid_json_returns_empty(self, tmp_path):
        p = tmp_path / "bad.json"
        p.write_text("{broken", encoding="utf-8")
        with patch(
            "local_deep_research.web.server_config.get_server_config_path",
            return_value=p,
        ):
            assert _load_legacy_config() == {}

    def test_non_dict_returns_empty(self, tmp_path):
        p = tmp_path / "list.json"
        p.write_text('"just a string"', encoding="utf-8")
        with patch(
            "local_deep_research.web.server_config.get_server_config_path",
            return_value=p,
        ):
            assert _load_legacy_config() == {}

    def test_recognized_keys_extracted(self, tmp_path):
        p = tmp_path / "config.json"
        p.write_text(
            json.dumps({"host": "localhost", "port": 8080}), encoding="utf-8"
        )
        with patch(
            "local_deep_research.web.server_config.get_server_config_path",
            return_value=p,
        ):
            result = _load_legacy_config()
            assert result["host"] == "localhost"
            assert result["port"] == 8080

    def test_unrecognized_keys_ignored(self, tmp_path):
        p = tmp_path / "config.json"
        p.write_text(
            json.dumps({"host": "localhost", "bogus_key": "val"}),
            encoding="utf-8",
        )
        with patch(
            "local_deep_research.web.server_config.get_server_config_path",
            return_value=p,
        ):
            result = _load_legacy_config()
            assert "host" in result
            assert "bogus_key" not in result


class TestLoadServerConfig:
    def test_returns_all_expected_keys(self):
        with patch(
            "local_deep_research.web.server_config._load_legacy_config",
            return_value={},
        ):
            config = load_server_config()
            for key in _DEFAULTS:
                assert key in config

    def test_defaults_when_no_legacy_no_env(self):
        with (
            patch(
                "local_deep_research.web.server_config._load_legacy_config",
                return_value={},
            ),
            patch.dict("os.environ", {}, clear=False),
        ):
            # Remove any LDR_ env vars
            import os

            env_clean = {
                k: v for k, v in os.environ.items() if not k.startswith("LDR_")
            }
            with patch.dict("os.environ", env_clean, clear=True):
                config = load_server_config()
                assert config["port"] in (5000, _DEFAULTS["port"])

    def test_unrecognized_allow_registrations_env_defaults_false(self):
        with (
            patch(
                "local_deep_research.web.server_config._load_legacy_config",
                return_value={},
            ),
            patch.dict(
                "os.environ",
                {"LDR_APP_ALLOW_REGISTRATIONS": "nein"},
                clear=False,
            ),
        ):
            config = load_server_config()
            assert config["allow_registrations"] is False

    def test_recognized_allow_registrations_env_passes(self):
        with (
            patch(
                "local_deep_research.web.server_config._load_legacy_config",
                return_value={},
            ),
            patch.dict(
                "os.environ",
                {"LDR_APP_ALLOW_REGISTRATIONS": "true"},
                clear=False,
            ),
        ):
            config = load_server_config()
            # Should be True (recognized value)
            assert config["allow_registrations"] is True

    def test_legacy_unrecognized_allow_registrations_string(self):
        with (
            patch(
                "local_deep_research.web.server_config._load_legacy_config",
                return_value={"allow_registrations": "disabled"},
            ),
            patch.dict("os.environ", {}, clear=False),
        ):
            import os

            env_clean = {
                k: v
                for k, v in os.environ.items()
                if k != "LDR_APP_ALLOW_REGISTRATIONS"
            }
            with patch.dict("os.environ", env_clean, clear=True):
                config = load_server_config()
                assert config["allow_registrations"] is False
