"""
Tests for SQLCipher missing scenario.

Verifies that users get helpful error messages when SQLCipher is not installed
and that the LDR_ALLOW_UNENCRYPTED workaround works.
"""

import os
import pytest
from unittest.mock import patch

from tests.test_utils import add_src_to_path

add_src_to_path()


class TestSQLCipherMissing:
    """Test behavior when SQLCipher is not available."""

    def test_error_message_mentions_sqlcipher(self):
        """Error message should mention SQLCipher so users know what's missing."""
        from src.local_deep_research.database.encrypted_db import (
            DatabaseManager,
        )

        old_value = os.environ.pop("LDR_ALLOW_UNENCRYPTED", None)

        try:
            with patch(
                "src.local_deep_research.database.encrypted_db.get_sqlcipher_module"
            ) as mock_get:
                mock_get.side_effect = ImportError(
                    "No module named 'sqlcipher3'"
                )

                with pytest.raises(RuntimeError) as exc_info:
                    DatabaseManager()

                error_msg = str(exc_info.value)
                assert "SQLCipher" in error_msg, (
                    f"Error should mention SQLCipher. Got: {error_msg}"
                )
        finally:
            if old_value is not None:
                os.environ["LDR_ALLOW_UNENCRYPTED"] = old_value

    def test_error_message_mentions_workaround(self):
        """Error message should mention LDR_ALLOW_UNENCRYPTED workaround."""
        from src.local_deep_research.database.encrypted_db import (
            DatabaseManager,
        )

        old_value = os.environ.pop("LDR_ALLOW_UNENCRYPTED", None)

        try:
            with patch(
                "src.local_deep_research.database.encrypted_db.get_sqlcipher_module"
            ) as mock_get:
                mock_get.side_effect = ImportError(
                    "No module named 'sqlcipher3'"
                )

                with pytest.raises(RuntimeError) as exc_info:
                    DatabaseManager()

                error_msg = str(exc_info.value)
                assert "LDR_ALLOW_UNENCRYPTED" in error_msg, (
                    f"Error should mention workaround. Got: {error_msg}"
                )
        finally:
            if old_value is not None:
                os.environ["LDR_ALLOW_UNENCRYPTED"] = old_value

    def test_workaround_allows_startup_without_encryption(self):
        """LDR_ALLOW_UNENCRYPTED=true should allow startup without SQLCipher."""
        from src.local_deep_research.database.encrypted_db import (
            DatabaseManager,
        )

        old_value = os.environ.get("LDR_ALLOW_UNENCRYPTED")
        os.environ["LDR_ALLOW_UNENCRYPTED"] = "true"

        try:
            with patch(
                "src.local_deep_research.database.encrypted_db.get_sqlcipher_module"
            ) as mock_get:
                mock_get.side_effect = ImportError(
                    "No module named 'sqlcipher3'"
                )

                # Should NOT raise
                manager = DatabaseManager()
                assert manager.has_encryption is False, (
                    "With workaround and no SQLCipher, has_encryption should be False"
                )
        finally:
            if old_value is not None:
                os.environ["LDR_ALLOW_UNENCRYPTED"] = old_value
            else:
                os.environ.pop("LDR_ALLOW_UNENCRYPTED", None)

    def test_db_manager_has_encryption_is_boolean(self):
        """db_manager.has_encryption should be a boolean."""
        from src.local_deep_research.database.encrypted_db import db_manager

        assert isinstance(db_manager.has_encryption, bool), (
            f"has_encryption should be bool, got {type(db_manager.has_encryption)}"
        )
