"""
Comprehensive tests for preference_manager/storage.py module.
Tests SQLPreferenceStorage class interface and basic behavior.
"""

import pytest
from unittest.mock import MagicMock


class TestSQLPreferenceStorageInit:
    """Tests for SQLPreferenceStorage initialization."""

    def test_requires_session(self):
        """Test requires a session parameter."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        with pytest.raises(ValueError):
            SQLPreferenceStorage(None)

    def test_stores_session(self):
        """Test stores the session in _session attribute."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert storage._session is mock_session

    def test_session_property_returns_session(self):
        """Test session property returns the stored session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert storage.session is mock_session


class TestSQLPreferenceStorageMethods:
    """Tests for SQLPreferenceStorage method signatures."""

    def test_has_create_method(self):
        """Test has create method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "create")
        assert callable(storage.create)

    def test_has_get_method(self):
        """Test has get method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "get")
        assert callable(storage.get)

    def test_has_update_method(self):
        """Test has update method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "update")
        assert callable(storage.update)

    def test_has_delete_method(self):
        """Test has delete method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "delete")
        assert callable(storage.delete)

    def test_has_list_method(self):
        """Test has list method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "list")
        assert callable(storage.list)

    def test_has_get_user_preferences_method(self):
        """Test has get_user_preferences method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "get_user_preferences")
        assert callable(storage.get_user_preferences)

    def test_has_upsert_preferences_method(self):
        """Test has upsert_preferences method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "upsert_preferences")
        assert callable(storage.upsert_preferences)

    def test_has_add_liked_item_method(self):
        """Test has add_liked_item method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "add_liked_item")
        assert callable(storage.add_liked_item)

    def test_has_add_disliked_item_method(self):
        """Test has add_disliked_item method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "add_disliked_item")
        assert callable(storage.add_disliked_item)

    def test_has_update_preference_embedding_method(self):
        """Test has update_preference_embedding method."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        assert hasattr(storage, "update_preference_embedding")
        assert callable(storage.update_preference_embedding)


class TestSQLPreferenceStorageMethodSignatures:
    """Tests for method parameter signatures."""

    def test_create_accepts_data_dict(self):
        """Test create accepts data dictionary."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.create)
        params = list(sig.parameters.keys())

        assert "data" in params

    def test_get_accepts_id(self):
        """Test get accepts id parameter."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.get)
        params = list(sig.parameters.keys())

        assert "id" in params

    def test_update_accepts_id_and_data(self):
        """Test update accepts id and data parameters."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.update)
        params = list(sig.parameters.keys())

        assert "id" in params
        assert "data" in params

    def test_delete_accepts_id(self):
        """Test delete accepts id parameter."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.delete)
        params = list(sig.parameters.keys())

        assert "id" in params

    def test_list_accepts_filters_limit_offset(self):
        """Test list accepts filters, limit, and offset parameters."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.list)
        params = list(sig.parameters.keys())

        assert "filters" in params
        assert "limit" in params
        assert "offset" in params

    def test_get_user_preferences_accepts_user_id(self):
        """Test get_user_preferences accepts user_id parameter."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.get_user_preferences)
        params = list(sig.parameters.keys())

        assert "user_id" in params

    def test_add_liked_item_accepts_user_id_item_id(self):
        """Test add_liked_item accepts user_id and item_id parameters."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.add_liked_item)
        params = list(sig.parameters.keys())

        assert "user_id" in params
        assert "item_id" in params

    def test_update_preference_embedding_accepts_user_id_embedding(self):
        """Test update_preference_embedding accepts user_id and embedding."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        import inspect

        mock_session = MagicMock()
        storage = SQLPreferenceStorage(mock_session)

        sig = inspect.signature(storage.update_preference_embedding)
        params = list(sig.parameters.keys())

        assert "user_id" in params
        assert "embedding" in params


class TestSQLPreferenceStorageInheritance:
    """Tests for class inheritance."""

    def test_extends_preference_storage(self):
        """Test SQLPreferenceStorage extends PreferenceStorage."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )
        from local_deep_research.news.core.storage import PreferenceStorage

        assert issubclass(SQLPreferenceStorage, PreferenceStorage)


class TestSQLPreferenceStorageImports:
    """Tests for module imports."""

    def test_imports_sql_preference_storage(self):
        """Test SQLPreferenceStorage can be imported."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        assert SQLPreferenceStorage is not None

    def test_is_class(self):
        """Test SQLPreferenceStorage is a class."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        assert isinstance(SQLPreferenceStorage, type)
