"""
Tests for checkbox settings save behavior.

Verifies that checkbox settings (booleans) can be correctly saved as both
true and false values, and that the value persists correctly.
"""

from tests.test_utils import add_src_to_path

add_src_to_path()


class TestCheckboxBooleanSave:
    """Test that boolean settings can be saved as true and false."""

    def test_parse_boolean_false_string(self):
        """parse_boolean should return False for 'false' string."""
        from local_deep_research.settings.manager import parse_boolean

        assert parse_boolean("false") is False
        assert parse_boolean("False") is False
        assert parse_boolean("FALSE") is False

    def test_parse_boolean_true_string(self):
        """parse_boolean should return True for 'true' string."""
        from local_deep_research.settings.manager import parse_boolean

        assert parse_boolean("true") is True
        assert parse_boolean("True") is True
        assert parse_boolean("TRUE") is True

    def test_parse_boolean_actual_booleans(self):
        """parse_boolean should handle actual boolean values."""
        from local_deep_research.settings.manager import parse_boolean

        assert parse_boolean(True) is True
        assert parse_boolean(False) is False

    def test_get_typed_setting_value_checkbox_false(self):
        """get_typed_setting_value should convert 'false' to False for checkbox."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Test with string "false" (as sent by hidden input)
        result = get_typed_setting_value(
            key="app.allow_registrations",
            value="false",
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result is False, (
            f"Expected False, got {result} (type: {type(result)})"
        )

    def test_get_typed_setting_value_checkbox_true(self):
        """get_typed_setting_value should convert 'true' to True for checkbox."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Test with string "true" (as sent by checkbox)
        result = get_typed_setting_value(
            key="app.allow_registrations",
            value="true",
            ui_element="checkbox",
            default=False,
            check_env=False,
        )
        assert result is True, (
            f"Expected True, got {result} (type: {type(result)})"
        )

    def test_get_typed_setting_value_checkbox_boolean_false(self):
        """get_typed_setting_value should preserve False boolean for checkbox."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Test with actual False boolean (as sent by AJAX)
        result = get_typed_setting_value(
            key="app.allow_registrations",
            value=False,
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result is False, (
            f"Expected False, got {result} (type: {type(result)})"
        )

    def test_get_typed_setting_value_checkbox_boolean_true(self):
        """get_typed_setting_value should preserve True boolean for checkbox."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Test with actual True boolean (as sent by AJAX)
        result = get_typed_setting_value(
            key="app.allow_registrations",
            value=True,
            ui_element="checkbox",
            default=False,
            check_env=False,
        )
        assert result is True, (
            f"Expected True, got {result} (type: {type(result)})"
        )


class TestCheckboxFormDataHandling:
    """Test how form data is processed for checkbox settings."""

    def test_form_data_false_string_is_converted(self):
        """Form data with 'false' string should be converted to False boolean."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Simulate what happens when hidden fallback sends "false"
        form_value = "false"  # This is what the hidden input sends
        result = get_typed_setting_value(
            key="app.debug",
            value=form_value,
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result is False
        assert isinstance(result, bool)

    def test_ajax_json_false_is_preserved(self):
        """AJAX JSON with false boolean should be preserved as False."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Simulate what happens when AJAX sends JSON with false
        json_value = False  # This is what checkbox.checked returns
        result = get_typed_setting_value(
            key="app.debug",
            value=json_value,
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result is False
        assert isinstance(result, bool)


class TestCheckboxMissingValue:
    """Test handling of missing checkbox values (key not in form data)."""

    def test_missing_checkbox_value_none_returns_default(self):
        """When checkbox value is None, default value is returned."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # When value is None, get_typed_setting_value returns the default
        # This is correct for settings that weren't included in form data
        result = get_typed_setting_value(
            key="app.debug",
            value=None,
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        # With value=None, the function returns the default
        assert result is True

    def test_parse_boolean_none_is_false(self):
        """parse_boolean treats None as False (HTML semantics)."""
        from local_deep_research.settings.manager import parse_boolean

        # parse_boolean directly returns False for None
        assert parse_boolean(None) is False

    def test_empty_string_is_false(self):
        """Empty string checkbox value should be treated as False."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        result = get_typed_setting_value(
            key="app.debug",
            value="",
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result is False


class TestAllowRegistrationsSetting:
    """Specific tests for the app.allow_registrations setting."""

    def test_allow_registrations_default_value(self):
        """app.allow_registrations should have correct default in settings."""
        import json
        from pathlib import Path

        # Read the default settings file
        defaults_path = (
            Path(__file__).parent.parent.parent
            / "src"
            / "local_deep_research"
            / "defaults"
            / "default_settings.json"
        )
        if defaults_path.exists():
            with open(defaults_path) as f:
                defaults = json.load(f)

            setting = defaults.get("app.allow_registrations", {})
            assert setting.get("ui_element") == "checkbox"
            assert setting.get("value") is True  # Default should be True

    def test_allow_registrations_can_be_set_false(self):
        """app.allow_registrations should be able to be set to False."""
        from local_deep_research.settings.manager import (
            get_typed_setting_value,
        )

        # Test setting to false via AJAX (boolean)
        result_ajax = get_typed_setting_value(
            key="app.allow_registrations",
            value=False,
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result_ajax is False

        # Test setting to false via POST (string)
        result_post = get_typed_setting_value(
            key="app.allow_registrations",
            value="false",
            ui_element="checkbox",
            default=True,
            check_env=False,
        )
        assert result_post is False
