"""
Behavioral tests for settings/env_registry module.

Tests convenience functions for CI detection, test mode,
and rate limiting.
"""

import os
from unittest.mock import patch


class TestRegistry:
    """Tests for the global registry instance."""

    def test_registry_exists(self):
        """Global registry exists."""
        from local_deep_research.settings.env_registry import registry

        assert registry is not None

    def test_registry_is_settings_registry(self):
        """Registry is a SettingsRegistry instance."""
        from local_deep_research.settings.env_registry import registry
        from local_deep_research.settings.env_settings import SettingsRegistry

        assert isinstance(registry, SettingsRegistry)

    def test_registry_has_categories(self):
        """Registry has registered categories."""
        from local_deep_research.settings.env_registry import registry

        # Should have at least some categories from ALL_SETTINGS
        all_vars = registry.get_all_env_vars()
        assert len(all_vars) > 0


class TestGetEnvSetting:
    """Tests for get_env_setting function."""

    def test_returns_default_for_unknown_key(self):
        """Returns default for unknown setting key."""
        from local_deep_research.settings.env_registry import get_env_setting

        result = get_env_setting(
            "completely.unknown.key.xyz", default="fallback"
        )
        assert result == "fallback"

    def test_returns_none_default(self):
        """Returns None when no default specified and key not found."""
        from local_deep_research.settings.env_registry import get_env_setting

        result = get_env_setting("completely.unknown.key.xyz")
        assert result is None


class TestIsTestMode:
    """Tests for is_test_mode function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.settings.env_registry import is_test_mode

        result = is_test_mode()
        assert isinstance(result, bool)

    def test_callable_without_args(self):
        """Callable without arguments."""
        from local_deep_research.settings.env_registry import is_test_mode

        # Should not raise
        is_test_mode()


class TestIsCiEnvironment:
    """Tests for is_ci_environment function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.settings.env_registry import is_ci_environment

        result = is_ci_environment()
        assert isinstance(result, bool)

    def test_true_when_ci_set(self):
        """Returns True when CI env var is 'true'."""
        from local_deep_research.settings.env_registry import is_ci_environment

        with patch.dict(os.environ, {"CI": "true"}):
            assert is_ci_environment() is True

    def test_true_when_ci_is_1(self):
        """Returns True when CI env var is '1'."""
        from local_deep_research.settings.env_registry import is_ci_environment

        with patch.dict(os.environ, {"CI": "1"}):
            assert is_ci_environment() is True

    def test_true_when_ci_is_yes(self):
        """Returns True when CI env var is 'yes'."""
        from local_deep_research.settings.env_registry import is_ci_environment

        with patch.dict(os.environ, {"CI": "yes"}):
            assert is_ci_environment() is True

    def test_false_when_ci_is_false(self):
        """Returns False when CI env var is 'false'."""
        from local_deep_research.settings.env_registry import is_ci_environment

        with patch.dict(os.environ, {"CI": "false"}):
            assert is_ci_environment() is False

    def test_false_when_ci_not_set(self):
        """Returns False when CI env var is not set."""
        from local_deep_research.settings.env_registry import is_ci_environment

        env = os.environ.copy()
        env.pop("CI", None)
        with patch.dict(os.environ, env, clear=True):
            assert is_ci_environment() is False

    def test_case_insensitive(self):
        """CI check is case-insensitive."""
        from local_deep_research.settings.env_registry import is_ci_environment

        with patch.dict(os.environ, {"CI": "TRUE"}):
            assert is_ci_environment() is True

        with patch.dict(os.environ, {"CI": "True"}):
            assert is_ci_environment() is True


class TestIsGithubActions:
    """Tests for is_github_actions function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.settings.env_registry import is_github_actions

        result = is_github_actions()
        assert isinstance(result, bool)

    def test_true_when_set(self):
        """Returns True when GITHUB_ACTIONS is 'true'."""
        from local_deep_research.settings.env_registry import is_github_actions

        with patch.dict(os.environ, {"GITHUB_ACTIONS": "true"}):
            assert is_github_actions() is True

    def test_false_when_not_set(self):
        """Returns False when GITHUB_ACTIONS is not set."""
        from local_deep_research.settings.env_registry import is_github_actions

        env = os.environ.copy()
        env.pop("GITHUB_ACTIONS", None)
        with patch.dict(os.environ, env, clear=True):
            assert is_github_actions() is False

    def test_false_when_false(self):
        """Returns False when GITHUB_ACTIONS is 'false'."""
        from local_deep_research.settings.env_registry import is_github_actions

        with patch.dict(os.environ, {"GITHUB_ACTIONS": "false"}):
            assert is_github_actions() is False


class TestIsRateLimitingEnabled:
    """Tests for is_rate_limiting_enabled function."""

    def test_returns_bool(self):
        """Returns a boolean."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        result = is_rate_limiting_enabled()
        assert isinstance(result, bool)

    def test_enabled_by_default(self):
        """Rate limiting is enabled by default."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        env = os.environ.copy()
        env.pop("DISABLE_RATE_LIMITING", None)
        with patch.dict(os.environ, env, clear=True):
            assert is_rate_limiting_enabled() is True

    def test_disabled_when_flag_true(self):
        """Rate limiting disabled when DISABLE_RATE_LIMITING=true."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        with patch.dict(os.environ, {"DISABLE_RATE_LIMITING": "true"}):
            assert is_rate_limiting_enabled() is False

    def test_disabled_when_flag_1(self):
        """Rate limiting disabled when DISABLE_RATE_LIMITING=1."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        with patch.dict(os.environ, {"DISABLE_RATE_LIMITING": "1"}):
            assert is_rate_limiting_enabled() is False

    def test_disabled_when_flag_yes(self):
        """Rate limiting disabled when DISABLE_RATE_LIMITING=yes."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        with patch.dict(os.environ, {"DISABLE_RATE_LIMITING": "yes"}):
            assert is_rate_limiting_enabled() is False

    def test_enabled_when_flag_false(self):
        """Rate limiting stays enabled when DISABLE_RATE_LIMITING=false."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        with patch.dict(os.environ, {"DISABLE_RATE_LIMITING": "false"}):
            assert is_rate_limiting_enabled() is True

    def test_case_insensitive(self):
        """DISABLE_RATE_LIMITING check is case-insensitive."""
        from local_deep_research.settings.env_registry import (
            is_rate_limiting_enabled,
        )

        with patch.dict(os.environ, {"DISABLE_RATE_LIMITING": "TRUE"}):
            assert is_rate_limiting_enabled() is False


class TestModuleExports:
    """Tests for module __all__ exports."""

    def test_exports_registry(self):
        """Exports registry."""
        from local_deep_research.settings.env_registry import __all__

        assert "registry" in __all__

    def test_exports_get_env_setting(self):
        """Exports get_env_setting."""
        from local_deep_research.settings.env_registry import __all__

        assert "get_env_setting" in __all__

    def test_exports_is_test_mode(self):
        """Exports is_test_mode."""
        from local_deep_research.settings.env_registry import __all__

        assert "is_test_mode" in __all__

    def test_exports_is_ci_environment(self):
        """Exports is_ci_environment."""
        from local_deep_research.settings.env_registry import __all__

        assert "is_ci_environment" in __all__

    def test_exports_is_rate_limiting_enabled(self):
        """Exports is_rate_limiting_enabled."""
        from local_deep_research.settings.env_registry import __all__

        assert "is_rate_limiting_enabled" in __all__
