"""Coverage tests for encrypted_db.py using regular SQLite (no SQLCipher required).

Covers:
- DatabaseManager initialisation helpers
- _is_valid_encryption_key
- is_user_connected
- get_connected_usernames
- get_memory_usage
- cleanup_thread_engines (filter logic)
- cleanup_dead_thread_engines
- maybe_sweep_dead_engines (rate limiting)
- cleanup_all_thread_engines
- close_user_database / close_all_databases
- check_database_integrity success/failure paths
- get_pool_kwargs
"""

import threading
import time
from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest

MODULE = "local_deep_research.database.encrypted_db"


# ---------------------------------------------------------------------------
# Helpers – create a DatabaseManager with encryption disabled
# ---------------------------------------------------------------------------


@contextmanager
def _unencrypted_manager():
    """Yield a DatabaseManager that has encryption disabled (SQLCipher not available)."""
    with (
        patch(
            f"{MODULE}.get_data_directory",
            return_value=MagicMock(
                __truediv__=lambda self, other: MagicMock(
                    mkdir=MagicMock(),
                    __truediv__=lambda self2, other2: MagicMock(),
                )
            ),
        ),
        patch(f"{MODULE}.get_env_setting", return_value=True),
        patch(
            f"{MODULE}.get_sqlcipher_module",
            side_effect=ImportError("no sqlcipher"),
        ),
    ):
        from local_deep_research.database.encrypted_db import DatabaseManager

        mgr = DatabaseManager.__new__(DatabaseManager)
        mgr.connections = {}
        mgr._connections_lock = threading.RLock()
        mgr._thread_engines = {}
        mgr._thread_engine_lock = threading.Lock()
        mgr._sweep_lock = threading.Lock()
        mgr._last_sweep_time = 0.0
        mgr.has_encryption = False
        import os

        mgr._use_static_pool = bool(os.environ.get("TESTING"))
        from sqlalchemy.pool import StaticPool, QueuePool

        mgr._pool_class = StaticPool if mgr._use_static_pool else QueuePool
        yield mgr


# ---------------------------------------------------------------------------
# _is_valid_encryption_key
# ---------------------------------------------------------------------------


class TestIsValidEncryptionKey:
    def test_none_is_invalid(self):
        with _unencrypted_manager() as mgr:
            assert mgr._is_valid_encryption_key(None) is False

    def test_empty_string_is_invalid(self):
        with _unencrypted_manager() as mgr:
            assert mgr._is_valid_encryption_key("") is False

    def test_whitespace_only_is_invalid(self):
        with _unencrypted_manager() as mgr:
            assert mgr._is_valid_encryption_key("   ") is False

    def test_valid_password(self):
        with _unencrypted_manager() as mgr:
            assert mgr._is_valid_encryption_key("secret123") is True

    def test_single_char_password(self):
        with _unencrypted_manager() as mgr:
            assert mgr._is_valid_encryption_key("x") is True


# ---------------------------------------------------------------------------
# is_user_connected / get_connected_usernames
# ---------------------------------------------------------------------------


class TestConnectionState:
    def test_is_user_connected_false_when_absent(self):
        with _unencrypted_manager() as mgr:
            assert mgr.is_user_connected("alice") is False

    def test_is_user_connected_true_when_present(self):
        with _unencrypted_manager() as mgr:
            mgr.connections["alice"] = MagicMock()
            assert mgr.is_user_connected("alice") is True

    def test_get_connected_usernames_empty(self):
        with _unencrypted_manager() as mgr:
            assert mgr.get_connected_usernames() == set()

    def test_get_connected_usernames_snapshot(self):
        with _unencrypted_manager() as mgr:
            mgr.connections["alice"] = MagicMock()
            mgr.connections["bob"] = MagicMock()
            names = mgr.get_connected_usernames()
            assert names == {"alice", "bob"}


# ---------------------------------------------------------------------------
# get_memory_usage
# ---------------------------------------------------------------------------


class TestGetMemoryUsage:
    def test_empty_state(self):
        with _unencrypted_manager() as mgr:
            stats = mgr.get_memory_usage()
            assert stats["active_connections"] == 0
            assert stats["thread_engines"] == 0
            assert stats["estimated_memory_mb"] == 0.0

    def test_with_connections_and_thread_engines(self):
        with _unencrypted_manager() as mgr:
            mgr.connections["user1"] = MagicMock()
            mgr.connections["user2"] = MagicMock()
            mgr._thread_engines[("user1", 100)] = MagicMock()
            stats = mgr.get_memory_usage()
            assert stats["active_connections"] == 2
            assert stats["thread_engines"] == 1
            assert stats["estimated_memory_mb"] == pytest.approx(3 * 3.5)


# ---------------------------------------------------------------------------
# _get_pool_kwargs
# ---------------------------------------------------------------------------


class TestGetPoolKwargs:
    def test_static_pool_returns_empty(self):
        with _unencrypted_manager() as mgr:
            mgr._use_static_pool = True
            result = mgr._get_pool_kwargs()
            assert result == {}

    def test_queue_pool_returns_kwargs(self):
        with _unencrypted_manager() as mgr:
            mgr._use_static_pool = False
            result = mgr._get_pool_kwargs()
            assert "pool_size" in result
            assert "max_overflow" in result


# ---------------------------------------------------------------------------
# close_user_database
# ---------------------------------------------------------------------------


class TestCloseUserDatabase:
    def test_closes_and_removes_connection(self):
        with _unencrypted_manager() as mgr:
            mock_engine = MagicMock()
            mgr.connections["alice"] = mock_engine
            with patch.object(mgr, "cleanup_thread_engines"):
                mgr.close_user_database("alice")
            mock_engine.dispose.assert_called_once()
            assert "alice" not in mgr.connections

    def test_no_error_when_user_not_connected(self):
        with _unencrypted_manager() as mgr:
            # Should not raise
            with patch.object(mgr, "cleanup_thread_engines"):
                mgr.close_user_database("nonexistent")

    def test_dispose_error_handled_gracefully(self):
        with _unencrypted_manager() as mgr:
            mock_engine = MagicMock()
            mock_engine.dispose.side_effect = RuntimeError("dispose failed")
            mgr.connections["alice"] = mock_engine
            with patch.object(mgr, "cleanup_thread_engines"):
                mgr.close_user_database("alice")
            assert "alice" not in mgr.connections


# ---------------------------------------------------------------------------
# close_all_databases
# ---------------------------------------------------------------------------


class TestCloseAllDatabases:
    def test_disposes_all_engines(self):
        with _unencrypted_manager() as mgr:
            e1, e2 = MagicMock(), MagicMock()
            mgr.connections["a"] = e1
            mgr.connections["b"] = e2
            with patch.object(mgr, "cleanup_all_thread_engines"):
                mgr.close_all_databases()
            e1.dispose.assert_called_once()
            e2.dispose.assert_called_once()
            assert mgr.connections == {}


# ---------------------------------------------------------------------------
# cleanup_thread_engines
# ---------------------------------------------------------------------------


class TestCleanupThreadEngines:
    def test_cleanup_by_username(self):
        with _unencrypted_manager() as mgr:
            e1, e2 = MagicMock(), MagicMock()
            mgr._thread_engines[("alice", 100)] = e1
            mgr._thread_engines[("bob", 200)] = e2
            mgr.cleanup_thread_engines(username="alice")
            e1.dispose.assert_called_once()
            assert ("alice", 100) not in mgr._thread_engines
            assert ("bob", 200) in mgr._thread_engines

    def test_cleanup_by_thread_id(self):
        with _unencrypted_manager() as mgr:
            e1, e2 = MagicMock(), MagicMock()
            mgr._thread_engines[("alice", 100)] = e1
            mgr._thread_engines[("alice", 200)] = e2
            mgr.cleanup_thread_engines(thread_id=100)
            e1.dispose.assert_called_once()
            assert ("alice", 100) not in mgr._thread_engines
            assert ("alice", 200) in mgr._thread_engines

    def test_cleanup_by_username_and_thread(self):
        with _unencrypted_manager() as mgr:
            e1, e2 = MagicMock(), MagicMock()
            mgr._thread_engines[("alice", 100)] = e1
            mgr._thread_engines[("alice", 200)] = e2
            mgr.cleanup_thread_engines(username="alice", thread_id=100)
            e1.dispose.assert_called_once()
            e2.dispose.assert_not_called()

    def test_cleanup_current_thread_when_no_args(self):
        with _unencrypted_manager() as mgr:
            tid = threading.get_ident()
            e = MagicMock()
            mgr._thread_engines[("alice", tid)] = e
            mgr.cleanup_thread_engines()
            e.dispose.assert_called_once()


# ---------------------------------------------------------------------------
# cleanup_dead_thread_engines
# ---------------------------------------------------------------------------


class TestCleanupDeadThreadEngines:
    def test_removes_dead_thread_engines(self):
        with _unencrypted_manager() as mgr:
            dead_tid = 999999  # very unlikely to be a real thread id
            e = MagicMock()
            mgr._thread_engines[("alice", dead_tid)] = e
            mgr.cleanup_dead_thread_engines()
            e.dispose.assert_called_once()
            assert ("alice", dead_tid) not in mgr._thread_engines

    def test_keeps_alive_thread_engines(self):
        with _unencrypted_manager() as mgr:
            alive_tid = threading.get_ident()
            e = MagicMock()
            mgr._thread_engines[("alice", alive_tid)] = e
            mgr.cleanup_dead_thread_engines()
            e.dispose.assert_not_called()

    def test_no_error_when_empty(self):
        with _unencrypted_manager() as mgr:
            mgr.cleanup_dead_thread_engines()  # Should not raise


# ---------------------------------------------------------------------------
# maybe_sweep_dead_engines
# ---------------------------------------------------------------------------


class TestMaybeSweepDeadEngines:
    def test_sweeps_when_interval_elapsed(self):
        with _unencrypted_manager() as mgr:
            mgr._last_sweep_time = 0.0
            with patch.object(mgr, "cleanup_dead_thread_engines") as mock_sweep:
                mgr.maybe_sweep_dead_engines(interval=0.0)
                mock_sweep.assert_called_once()

    def test_skips_when_interval_not_elapsed(self):
        with _unencrypted_manager() as mgr:
            mgr._last_sweep_time = time.monotonic() + 9999
            with patch.object(mgr, "cleanup_dead_thread_engines") as mock_sweep:
                mgr.maybe_sweep_dead_engines(interval=60.0)
                mock_sweep.assert_not_called()


# ---------------------------------------------------------------------------
# cleanup_all_thread_engines
# ---------------------------------------------------------------------------


class TestCleanupAllThreadEngines:
    def test_disposes_all_thread_engines(self):
        with _unencrypted_manager() as mgr:
            e1, e2 = MagicMock(), MagicMock()
            mgr._thread_engines[("alice", 100)] = e1
            mgr._thread_engines[("bob", 200)] = e2
            mgr.cleanup_all_thread_engines()
            e1.dispose.assert_called_once()
            e2.dispose.assert_called_once()
            assert mgr._thread_engines == {}
