"""High-value tests for settings/env_definitions/bootstrap.py.

Tests the BOOTSTRAP_SETTINGS list: SecretSetting, StringSetting,
BooleanSetting, PathSetting instances, their keys, defaults, and types.
"""

import pytest

from local_deep_research.settings.env_definitions.bootstrap import (
    BOOTSTRAP_SETTINGS,
)
from local_deep_research.settings.env_settings import (
    BooleanSetting,
    StringSetting,
    PathSetting,
    SecretSetting,
    SettingsRegistry,
)


def _find(key: str):
    """Return the setting object with the given key."""
    for s in BOOTSTRAP_SETTINGS:
        if s.key == key:
            return s
    raise KeyError(f"Setting {key!r} not found in BOOTSTRAP_SETTINGS")


class TestBootstrapRegistry:
    """Tests for BOOTSTRAP_SETTINGS structure."""

    def test_setting_count(self):
        """BOOTSTRAP_SETTINGS has the expected number of entries."""
        assert len(BOOTSTRAP_SETTINGS) == 8

    def test_all_keys_start_with_bootstrap(self):
        """Every key uses the 'bootstrap.' prefix."""
        for s in BOOTSTRAP_SETTINGS:
            assert s.key.startswith("bootstrap."), f"{s.key} missing prefix"

    def test_register_all_settings(self):
        """All settings can be registered under the bootstrap category."""
        registry = SettingsRegistry()
        registry.register_category("bootstrap", BOOTSTRAP_SETTINGS)
        for s in BOOTSTRAP_SETTINGS:
            assert registry.is_env_only(s.key)


class TestEncryptionKey:
    """Tests for bootstrap.encryption_key."""

    def test_is_secret_setting(self):
        s = _find("bootstrap.encryption_key")
        assert isinstance(s, SecretSetting)

    def test_not_required(self):
        """encryption_key is not required (allow_unencrypted may be True)."""
        s = _find("bootstrap.encryption_key")
        assert s.required is False


class TestSecretKey:
    """Tests for bootstrap.secret_key."""

    def test_is_secret_setting(self):
        s = _find("bootstrap.secret_key")
        assert isinstance(s, SecretSetting)


class TestDatabaseUrl:
    """Tests for bootstrap.database_url."""

    def test_is_string_setting(self):
        s = _find("bootstrap.database_url")
        assert isinstance(s, StringSetting)


class TestAllowUnencrypted:
    """Tests for bootstrap.allow_unencrypted."""

    def test_is_boolean_setting(self):
        s = _find("bootstrap.allow_unencrypted")
        assert isinstance(s, BooleanSetting)

    def test_default_is_false(self, monkeypatch):
        """Default value is False (require encryption)."""
        s = _find("bootstrap.allow_unencrypted")
        monkeypatch.delenv(s.env_var, raising=False)
        if s.deprecated_env_var:
            monkeypatch.delenv(s.deprecated_env_var, raising=False)
        assert s.get_value() is False

    def test_deprecated_alias(self):
        """Has deprecated env var LDR_ALLOW_UNENCRYPTED."""
        s = _find("bootstrap.allow_unencrypted")
        assert s.deprecated_env_var == "LDR_ALLOW_UNENCRYPTED"

    def test_set_to_true(self, monkeypatch):
        """Can be set to True via env var."""
        s = _find("bootstrap.allow_unencrypted")
        monkeypatch.setenv(s.env_var, "true")
        assert s.get_value() is True

    def test_set_to_false(self, monkeypatch):
        """Can be set to False via env var."""
        s = _find("bootstrap.allow_unencrypted")
        monkeypatch.setenv(s.env_var, "false")
        assert s.get_value() is False


class TestPathSettings:
    """Tests for bootstrap path settings."""

    @pytest.mark.parametrize(
        "key",
        [
            "bootstrap.data_dir",
            "bootstrap.config_dir",
            "bootstrap.log_dir",
        ],
    )
    def test_is_path_setting(self, key):
        s = _find(key)
        assert isinstance(s, PathSetting)

    @pytest.mark.parametrize(
        "key",
        [
            "bootstrap.data_dir",
            "bootstrap.config_dir",
            "bootstrap.log_dir",
        ],
    )
    def test_create_if_missing_is_true(self, key):
        """Path settings should create directories if missing."""
        s = _find(key)
        assert s.create_if_missing is True


class TestEnableFileLogging:
    """Tests for bootstrap.enable_file_logging."""

    def test_is_boolean_setting(self):
        s = _find("bootstrap.enable_file_logging")
        assert isinstance(s, BooleanSetting)

    def test_default_is_false(self, monkeypatch):
        """Default value is False."""
        s = _find("bootstrap.enable_file_logging")
        monkeypatch.delenv(s.env_var, raising=False)
        assert s.get_value() is False

    def test_set_to_true(self, monkeypatch):
        """Can be enabled via env var."""
        s = _find("bootstrap.enable_file_logging")
        monkeypatch.setenv(s.env_var, "true")
        assert s.get_value() is True
