"""
TDD RED: Verify that wipe_all_ai_caches exists, is importable, and
correctly wipes all global (non-user-specific) Qdrant collections.

This test catches the production error:
  ERROR | Failed to wipe global AI caches: cannot import name 'wipe_all_ai_caches'
"""

from unittest.mock import patch

import pytest


@pytest.fixture(autouse=True)
def mock_qdrant_client():
    """Ensure qdrant_client is mocked at the module level for import safety."""
    # This is already handled by session-scoped conftest, but we guard explicitly
    pass


class TestWipeAllAiCachesExists:
    """The function must be importable from qdrant_memory without errors."""

    def test_wipe_all_ai_caches_is_importable(self):
        """RED: This must not raise ImportError."""
        from GramAddict.core.qdrant_memory import wipe_all_ai_caches

        assert callable(wipe_all_ai_caches)

    def test_wipe_all_ai_caches_calls_wipe_on_all_global_collections(self):
        """RED: The function must instantiate and wipe all global memory DBs."""
        from GramAddict.core.qdrant_memory import wipe_all_ai_caches

        # Patch QdrantBase.__init__ to prevent real Qdrant connections
        with patch("GramAddict.core.qdrant_memory.QdrantBase.__init__", return_value=None):
            with patch("GramAddict.core.qdrant_memory.QdrantBase.wipe_collection") as mock_wipe:
                # Make is_connected return True so wipe_collection doesn't short-circuit
                with patch(
                    "GramAddict.core.qdrant_memory.QdrantBase.is_connected",
                    new_callable=lambda: property(lambda self: True),
                ):
                    wipe_all_ai_caches()

                    # Must wipe at least the known global collections
                    assert mock_wipe.call_count >= 6, (
                        f"Expected at least 6 global collection wipes, got {mock_wipe.call_count}. "
                        f"Global collections: HeuristicMemoryDB, UIMemoryDB, CommentMemoryDB, "
                        f"ContentMemoryDB, ScreenMemoryDB, NavigationMemoryDB"
                    )


class TestBlankStartCodePathIntegrity:
    """
    The blank_start code path in bot_flow must NOT silently swallow ImportErrors.
    If a function doesn't exist, it's a code defect, not a runtime condition.
    """

    def test_blank_start_wipe_does_not_catch_import_error(self):
        """
        The production code catches `Exception` broadly on Line 197 of bot_flow.py.
        ImportError IS an Exception subclass, so it gets swallowed silently.
        This test verifies that wipe_all_ai_caches actually exists (the root cause).
        """
        # If this import works, the blank_start try/except will never hit ImportError
        from GramAddict.core.qdrant_memory import wipe_all_ai_caches

        assert wipe_all_ai_caches is not None
