"""
Comprehensive tests for news/preference_manager/storage.py

Tests cover:
- SQLPreferenceStorage initialization
- CRUD operations (create, get, update, delete)
- User preference retrieval
- Upsert functionality
- Like/dislike item management
- Preference embedding updates
- Error handling and edge cases
"""

import pytest
from unittest.mock import Mock, MagicMock, patch


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

    def test_init_with_valid_session(self):
        """Test initialization with a valid session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = Mock()
        storage = SQLPreferenceStorage(mock_session)

        assert storage._session is mock_session

    def test_init_with_none_session_raises(self):
        """Test initialization with None session raises ValueError."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

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

        assert "Session is required" in str(exc_info.value)

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

        mock_session = Mock()
        storage = SQLPreferenceStorage(mock_session)

        assert storage.session is mock_session


class TestSQLPreferenceStorageCreate:
    """Tests for the create method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_create_with_minimal_data(self, storage):
        """Test creating preferences with minimal required data."""
        mock_prefs = Mock()
        mock_prefs.id = 1

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            MockUserPref.return_value = mock_prefs

            result = storage.create({"user_id": "user123"})

            assert result == "1"
            MockUserPref.assert_called_once()
            storage.session.__enter__().add.assert_called_once_with(mock_prefs)
            storage.session.__enter__().commit.assert_called_once()

    def test_create_with_all_fields(self, storage):
        """Test creating preferences with all fields."""
        mock_prefs = Mock()
        mock_prefs.id = 42

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            MockUserPref.return_value = mock_prefs

            data = {
                "user_id": "user456",
                "liked_categories": ["Tech", "Science"],
                "disliked_categories": ["Politics"],
                "liked_topics": ["AI", "ML"],
                "disliked_topics": ["Crypto"],
                "impact_threshold": 7,
                "focus_preferences": {"depth": "detailed"},
                "custom_prompt": "Focus on research papers",
                "custom_search_terms": "academic papers",
                "preference_embedding": [0.1, 0.2, 0.3],
                "liked_news_ids": ["news1", "news2"],
                "disliked_news_ids": ["news3"],
            }

            result = storage.create(data)

            assert result == "42"
            call_kwargs = MockUserPref.call_args[1]
            assert call_kwargs["user_id"] == "user456"
            assert call_kwargs["liked_categories"] == ["Tech", "Science"]
            assert call_kwargs["impact_threshold"] == 7

    def test_create_uses_defaults_for_missing_fields(self, storage):
        """Test that defaults are used for missing optional fields."""
        mock_prefs = Mock()
        mock_prefs.id = 1

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            MockUserPref.return_value = mock_prefs

            storage.create({"user_id": "user789"})

            call_kwargs = MockUserPref.call_args[1]
            assert call_kwargs["liked_categories"] == []
            assert call_kwargs["disliked_categories"] == []
            assert call_kwargs["impact_threshold"] == 5
            assert call_kwargs["focus_preferences"] == {}


class TestSQLPreferenceStorageGet:
    """Tests for the get method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_get_existing_preference(self, storage):
        """Test getting existing preferences by ID."""
        mock_prefs = Mock()
        mock_prefs.to_dict.return_value = {
            "id": 1,
            "user_id": "user123",
            "liked_categories": ["Tech"],
        }

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.get("1")

        assert result["id"] == 1
        assert result["user_id"] == "user123"
        mock_query.filter_by.assert_called_once_with(id=1)

    def test_get_nonexistent_preference_returns_none(self, storage):
        """Test getting nonexistent preferences returns None."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        result = storage.get("999")

        assert result is None

    def test_get_converts_id_to_int(self, storage):
        """Test that string ID is converted to int."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        storage.get("42")

        mock_query.filter_by.assert_called_once_with(id=42)


class TestSQLPreferenceStorageUpdate:
    """Tests for the update method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_update_existing_preference(self, storage):
        """Test updating existing preferences."""
        mock_prefs = Mock()
        mock_prefs.liked_categories = ["Old"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.update(
            "1", {"liked_categories": ["New", "Categories"]}
        )

        assert result is True
        assert mock_prefs.liked_categories == ["New", "Categories"]
        storage.session.__enter__().commit.assert_called_once()

    def test_update_nonexistent_preference_returns_false(self, storage):
        """Test updating nonexistent preferences returns False."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        result = storage.update("999", {"liked_categories": ["Test"]})

        assert result is False

    def test_update_multiple_fields(self, storage):
        """Test updating multiple fields at once."""
        mock_prefs = Mock()
        mock_prefs.liked_categories = []
        mock_prefs.impact_threshold = 5
        mock_prefs.custom_prompt = None

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.update(
            "1",
            {
                "liked_categories": ["Tech"],
                "impact_threshold": 8,
                "custom_prompt": "Focus on AI",
            },
        )

        assert result is True
        assert mock_prefs.liked_categories == ["Tech"]
        assert mock_prefs.impact_threshold == 8
        assert mock_prefs.custom_prompt == "Focus on AI"

    def test_update_ignores_nonexistent_fields(self, storage):
        """Test that nonexistent fields are ignored."""
        mock_prefs = Mock(spec=["liked_categories", "id"])
        mock_prefs.liked_categories = []

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.update(
            "1",
            {"liked_categories": ["Tech"], "nonexistent_field": "value"},
        )

        assert result is True
        assert mock_prefs.liked_categories == ["Tech"]
        # nonexistent_field should not be set
        assert not hasattr(mock_prefs, "nonexistent_field")


class TestSQLPreferenceStorageDelete:
    """Tests for the delete method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_delete_existing_preference(self, storage):
        """Test deleting existing preferences."""
        mock_prefs = Mock()

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.delete("1")

        assert result is True
        storage.session.__enter__().delete.assert_called_once_with(mock_prefs)
        storage.session.__enter__().commit.assert_called_once()

    def test_delete_nonexistent_preference_returns_false(self, storage):
        """Test deleting nonexistent preferences returns False."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        result = storage.delete("999")

        assert result is False
        storage.session.__enter__().delete.assert_not_called()


class TestSQLPreferenceStorageList:
    """Tests for the list method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_list_all_preferences(self, storage):
        """Test listing all preferences without filters."""
        mock_pref1 = Mock()
        mock_pref1.to_dict.return_value = {"id": 1, "user_id": "user1"}
        mock_pref2 = Mock()
        mock_pref2.to_dict.return_value = {"id": 2, "user_id": "user2"}

        mock_query = Mock()
        mock_query.limit.return_value.offset.return_value.all.return_value = [
            mock_pref1,
            mock_pref2,
        ]
        storage.session.__enter__().query.return_value = mock_query

        result = storage.list()

        assert len(result) == 2
        assert result[0]["id"] == 1
        assert result[1]["id"] == 2

    def test_list_with_user_filter(self, storage):
        """Test listing preferences filtered by user_id."""
        mock_pref = Mock()
        mock_pref.to_dict.return_value = {"id": 1, "user_id": "user1"}

        mock_query = Mock()
        mock_query.filter_by.return_value = mock_query
        mock_query.limit.return_value.offset.return_value.all.return_value = [
            mock_pref
        ]
        storage.session.__enter__().query.return_value = mock_query

        result = storage.list(filters={"user_id": "user1"})

        assert len(result) == 1
        mock_query.filter_by.assert_called_once_with(user_id="user1")

    def test_list_with_limit(self, storage):
        """Test listing preferences with limit."""
        mock_query = Mock()
        mock_query.limit.return_value.offset.return_value.all.return_value = []
        storage.session.__enter__().query.return_value = mock_query

        storage.list(limit=50)

        mock_query.limit.assert_called_once_with(50)

    def test_list_with_offset(self, storage):
        """Test listing preferences with offset."""
        mock_query = Mock()
        mock_limited = Mock()
        mock_query.limit.return_value = mock_limited
        mock_limited.offset.return_value.all.return_value = []
        storage.session.__enter__().query.return_value = mock_query

        storage.list(offset=10)

        mock_limited.offset.assert_called_once_with(10)

    def test_list_empty_result(self, storage):
        """Test listing when no preferences exist."""
        mock_query = Mock()
        mock_query.limit.return_value.offset.return_value.all.return_value = []
        storage.session.__enter__().query.return_value = mock_query

        result = storage.list()

        assert result == []


class TestGetUserPreferences:
    """Tests for the get_user_preferences method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_get_existing_user_preferences(self, storage):
        """Test getting preferences for an existing user."""
        mock_prefs = Mock()
        mock_prefs.to_dict.return_value = {
            "id": 1,
            "user_id": "user123",
            "liked_categories": ["Tech"],
        }

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.get_user_preferences("user123")

        assert result is not None
        assert result["user_id"] == "user123"
        mock_query.filter_by.assert_called_once_with(user_id="user123")

    def test_get_nonexistent_user_preferences_returns_none(self, storage):
        """Test getting preferences for nonexistent user returns None."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        result = storage.get_user_preferences("nonexistent_user")

        assert result is None


class TestUpsertPreferences:
    """Tests for the upsert_preferences method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_upsert_creates_new_when_not_exists(self, storage):
        """Test upsert creates new preferences when user doesn't exist."""
        mock_prefs = Mock()
        mock_prefs.id = 1

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        with patch.object(storage, "get_user_preferences", return_value=None):
            with patch.object(
                storage, "create", return_value="1"
            ) as mock_create:
                result = storage.upsert_preferences(
                    "new_user", {"liked_categories": ["Tech"]}
                )

                assert result == "1"
                mock_create.assert_called_once()
                call_args = mock_create.call_args[0][0]
                assert call_args["user_id"] == "new_user"
                assert call_args["liked_categories"] == ["Tech"]

    def test_upsert_updates_existing(self, storage):
        """Test upsert updates existing preferences."""
        existing_prefs = {"id": 1, "user_id": "existing_user"}
        mock_prefs = Mock()
        mock_prefs.id = 1
        mock_prefs.liked_categories = ["Old"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        with patch.object(
            storage, "get_user_preferences", return_value=existing_prefs
        ):
            result = storage.upsert_preferences(
                "existing_user", {"liked_categories": ["New"]}
            )

            assert result == "1"
            assert mock_prefs.liked_categories == ["New"]
            storage.session.__enter__().commit.assert_called()


class TestAddLikedItem:
    """Tests for the add_liked_item method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_add_liked_item_to_existing_prefs(self, storage):
        """Test adding liked item to existing preferences."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = ["item1", "item2"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_liked_item("user1", "item3", "news")

        assert result is True
        assert "item3" in mock_prefs.liked_news_ids
        storage.session.__enter__().commit.assert_called_once()

    def test_add_liked_item_creates_prefs_if_not_exists(self, storage):
        """Test adding liked item creates preferences if none exist."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            mock_new_prefs = Mock()
            MockUserPref.return_value = mock_new_prefs

            result = storage.add_liked_item("new_user", "item1", "news")

            assert result is True
            storage.session.__enter__().add.assert_called_once()
            MockUserPref.assert_called_once_with(
                user_id="new_user",
                liked_news_ids=["item1"],
                disliked_news_ids=[],
            )

    def test_add_liked_item_does_not_duplicate(self, storage):
        """Test adding already liked item doesn't create duplicate."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = ["item1", "item2"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_liked_item("user1", "item1", "news")

        assert result is True
        # Should still only have 2 items
        assert mock_prefs.liked_news_ids.count("item1") == 1

    def test_add_liked_item_limits_to_100(self, storage):
        """Test that liked items list is limited to 100 items."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = [f"item{i}" for i in range(100)]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_liked_item("user1", "new_item", "news")

        assert result is True
        # Should have exactly 100 items (newest 100)
        assert len(mock_prefs.liked_news_ids) == 100
        assert "new_item" in mock_prefs.liked_news_ids

    def test_add_liked_item_handles_none_list(self, storage):
        """Test adding liked item when list is None."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = None

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_liked_item("user1", "item1", "news")

        assert result is True
        assert mock_prefs.liked_news_ids == ["item1"]

    def test_add_liked_item_non_news_type_ignored(self, storage):
        """Test adding liked item with non-news type doesn't add to news list."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = []

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_liked_item("user1", "item1", "other_type")

        assert result is True
        # Should not be added to liked_news_ids
        assert mock_prefs.liked_news_ids == []


class TestAddDislikedItem:
    """Tests for the add_disliked_item method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_add_disliked_item_to_existing_prefs(self, storage):
        """Test adding disliked item to existing preferences."""
        mock_prefs = Mock()
        mock_prefs.disliked_news_ids = ["item1"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_disliked_item("user1", "item2", "news")

        assert result is True
        assert "item2" in mock_prefs.disliked_news_ids
        storage.session.__enter__().commit.assert_called_once()

    def test_add_disliked_item_creates_prefs_if_not_exists(self, storage):
        """Test adding disliked item creates preferences if none exist."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            mock_new_prefs = Mock()
            MockUserPref.return_value = mock_new_prefs

            result = storage.add_disliked_item("new_user", "item1", "news")

            assert result is True
            MockUserPref.assert_called_once_with(
                user_id="new_user",
                liked_news_ids=[],
                disliked_news_ids=["item1"],
            )

    def test_add_disliked_item_does_not_duplicate(self, storage):
        """Test adding already disliked item doesn't create duplicate."""
        mock_prefs = Mock()
        mock_prefs.disliked_news_ids = ["item1"]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_disliked_item("user1", "item1", "news")

        assert result is True
        assert mock_prefs.disliked_news_ids.count("item1") == 1

    def test_add_disliked_item_limits_to_100(self, storage):
        """Test that disliked items list is limited to 100 items."""
        mock_prefs = Mock()
        mock_prefs.disliked_news_ids = [f"item{i}" for i in range(100)]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_disliked_item("user1", "new_item", "news")

        assert result is True
        assert len(mock_prefs.disliked_news_ids) == 100
        assert "new_item" in mock_prefs.disliked_news_ids

    def test_add_disliked_item_handles_none_list(self, storage):
        """Test adding disliked item when list is None."""
        mock_prefs = Mock()
        mock_prefs.disliked_news_ids = None

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.add_disliked_item("user1", "item1", "news")

        assert result is True
        assert mock_prefs.disliked_news_ids == ["item1"]


class TestUpdatePreferenceEmbedding:
    """Tests for the update_preference_embedding method."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_update_embedding_for_existing_prefs(self, storage):
        """Test updating embedding for existing preferences."""
        mock_prefs = Mock()
        mock_prefs.preference_embedding = None

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        embedding = [0.1, 0.2, 0.3, 0.4]
        result = storage.update_preference_embedding("user1", embedding)

        assert result is True
        assert mock_prefs.preference_embedding == embedding
        storage.session.__enter__().commit.assert_called_once()

    def test_update_embedding_creates_prefs_if_not_exists(self, storage):
        """Test updating embedding creates preferences if none exist."""
        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = None
        storage.session.__enter__().query.return_value = mock_query

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            mock_new_prefs = Mock()
            MockUserPref.return_value = mock_new_prefs

            embedding = [0.1, 0.2, 0.3]
            result = storage.update_preference_embedding("new_user", embedding)

            assert result is True
            storage.session.__enter__().add.assert_called_once()
            MockUserPref.assert_called_once_with(
                user_id="new_user", preference_embedding=embedding
            )

    def test_update_embedding_with_large_vector(self, storage):
        """Test updating embedding with a large vector."""
        mock_prefs = Mock()
        mock_prefs.preference_embedding = None

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        # Typical embedding size (e.g., 1536 for OpenAI embeddings)
        embedding = [0.001 * i for i in range(1536)]
        result = storage.update_preference_embedding("user1", embedding)

        assert result is True
        assert len(mock_prefs.preference_embedding) == 1536

    def test_update_embedding_replaces_existing(self, storage):
        """Test that updating embedding replaces existing value."""
        mock_prefs = Mock()
        mock_prefs.preference_embedding = [0.1, 0.2, 0.3]

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        new_embedding = [0.9, 0.8, 0.7]
        result = storage.update_preference_embedding("user1", new_embedding)

        assert result is True
        assert mock_prefs.preference_embedding == [0.9, 0.8, 0.7]


class TestEdgeCases:
    """Tests for edge cases and error handling."""

    @pytest.fixture
    def storage(self):
        """Create a storage instance with mocked session."""
        from local_deep_research.news.preference_manager.storage import (
            SQLPreferenceStorage,
        )

        mock_session = MagicMock()
        return SQLPreferenceStorage(mock_session)

    def test_create_with_empty_user_id(self, storage):
        """Test creating preferences with empty user_id."""
        mock_prefs = Mock()
        mock_prefs.id = 1

        with patch(
            "local_deep_research.news.preference_manager.storage.UserPreference"
        ) as MockUserPref:
            MockUserPref.return_value = mock_prefs

            # Should not raise - empty string is technically valid
            result = storage.create({"user_id": ""})

            assert result == "1"

    def test_list_with_empty_filters(self, storage):
        """Test listing with empty filters dict."""
        mock_query = Mock()
        mock_query.limit.return_value.offset.return_value.all.return_value = []
        storage.session.__enter__().query.return_value = mock_query

        result = storage.list(filters={})

        assert result == []
        # Should not call filter_by with empty filters
        mock_query.filter_by.assert_not_called()

    def test_update_with_empty_data(self, storage):
        """Test updating with empty data dict."""
        mock_prefs = Mock()

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.update("1", {})

        assert result is True
        storage.session.__enter__().commit.assert_called_once()

    def test_get_with_invalid_id_format(self, storage):
        """Test getting with non-numeric ID raises ValueError."""
        mock_query = Mock()
        storage.session.__enter__().query.return_value = mock_query

        with pytest.raises(ValueError):
            storage.get("not_a_number")

    def test_multiple_concurrent_liked_items(self, storage):
        """Test adding multiple liked items preserves order."""
        mock_prefs = Mock()
        mock_prefs.liked_news_ids = []

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        # Add items in sequence
        storage.add_liked_item("user1", "item1", "news")
        storage.add_liked_item("user1", "item2", "news")
        storage.add_liked_item("user1", "item3", "news")

        # Should maintain insertion order
        assert mock_prefs.liked_news_ids == ["item1", "item2", "item3"]

    def test_preferences_with_special_characters_in_user_id(self, storage):
        """Test handling user_id with special characters."""
        mock_prefs = Mock()
        mock_prefs.to_dict.return_value = {
            "id": 1,
            "user_id": "user@example.com",
        }

        mock_query = Mock()
        mock_query.filter_by.return_value.first.return_value = mock_prefs
        storage.session.__enter__().query.return_value = mock_query

        result = storage.get_user_preferences("user@example.com")

        assert result["user_id"] == "user@example.com"
