from unittest.mock import MagicMock, PropertyMock, patch

from GramAddict.core.qdrant_memory import NavigationMemoryDB, QdrantBase, ScreenMemoryDB


class DummyQdrantBase(QdrantBase):
    def __init__(self):
        self.client = MagicMock()
        self.collection_name = "test_collection"


@patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=PropertyMock)
def test_qdrant_base_has_delete_point(mock_is_connected):
    """Prove that QdrantBase implements delete_point for autonomous unlearning."""
    mock_is_connected.return_value = True
    db = DummyQdrantBase()
    # If delete_point does not exist, this will raise AttributeError (RED)
    db.generate_uuid = MagicMock(return_value="test-uuid-1234")

    result = db.delete_point("test-seed")

    # Assert
    db.client.delete.assert_called_once_with(collection_name="test_collection", points_selector=["test-uuid-1234"])
    assert result is True


@patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=PropertyMock)
def test_screen_memory_has_purge_screen(mock_is_connected):
    """Prove that ScreenMemoryDB exposes purge_screen to heal poisoned classifications."""
    mock_is_connected.return_value = True
    db = ScreenMemoryDB()
    db.client = MagicMock()
    db.delete_point = MagicMock()

    # If purge_screen does not exist, AttributeError (RED)
    db.purge_screen("<node class='feed' />")
    db.delete_point.assert_called_once_with("<node class='feed' />")


@patch("GramAddict.core.qdrant_memory.QdrantBase.is_connected", new_callable=PropertyMock)
def test_navigation_memory_has_unlearn_transition(mock_is_connected):
    """Prove that NavigationMemoryDB exposes unlearn_transition to destroy trap paths."""
    mock_is_connected.return_value = True
    db = NavigationMemoryDB()
    db.client = MagicMock()
    db.delete_point = MagicMock()

    # If unlearn_transition does not exist, AttributeError (RED)
    db.unlearn_transition("HOME_FEED", "tap explore tab")
    db.delete_point.assert_called_once_with("HOME_FEED_tap explore tab")
