"""
Tests for news/subscription_manager/storage.py

Tests cover:
- SQLSubscriptionStorage initialization
- create() - new subscription creation
- get() - subscription retrieval
- update() - subscription modification
- delete() - subscription removal
- list() - subscription listing with filters
- get_active_subscriptions() - active subscription retrieval
- get_due_subscriptions() - due subscription retrieval
- update_refresh_time() - refresh time updates
- increment_stats() - stats updates
- pause_subscription() / resume_subscription() / expire_subscription()
"""

from unittest.mock import MagicMock, patch
from datetime import datetime, timezone, timedelta


class TestSQLSubscriptionStorageInitialization:
    """Tests for SQLSubscriptionStorage initialization."""

    def test_initialization_with_valid_session(self):
        """Test initialization with a valid session."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        storage = SQLSubscriptionStorage(mock_session)

        assert storage._session is mock_session

    def test_session_property(self):
        """Test session property returns correct session."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        storage = SQLSubscriptionStorage(mock_session)

        assert storage.session is mock_session


class TestCreate:
    """Tests for create() method."""

    @patch(
        "local_deep_research.news.subscription_manager.storage.NewsSubscription"
    )
    def test_create_subscription_with_all_fields(self, mock_model_class):
        """Test creating subscription with all fields."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        # Mock context manager
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_model_class.return_value = MagicMock()

        storage = SQLSubscriptionStorage(mock_session)
        storage.generate_id = MagicMock(return_value="test-id-123")

        data = {
            "user_id": "user123",
            "subscription_type": "topic",
            "query_or_topic": "AI news",
            "refresh_interval_minutes": 60,
            "name": "My AI Subscription",
        }

        result = storage.create(data)

        assert result == "test-id-123"
        mock_session.add.assert_called_once()
        mock_session.commit.assert_called_once()

    @patch(
        "local_deep_research.news.subscription_manager.storage.NewsSubscription"
    )
    def test_create_subscription_with_provided_id(self, mock_model_class):
        """Test creating subscription with provided ID."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_model_class.return_value = MagicMock()

        storage = SQLSubscriptionStorage(mock_session)

        data = {
            "id": "provided-id-456",
            "user_id": "user123",
            "subscription_type": "topic",
            "query_or_topic": "Tech news",
            "refresh_interval_minutes": 30,
        }

        result = storage.create(data)

        assert result == "provided-id-456"


class TestGet:
    """Tests for get() method."""

    def test_get_existing_subscription(self):
        """Test getting an existing subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        # Create mock subscription
        mock_subscription = MagicMock()
        mock_subscription.id = "sub-123"
        mock_subscription.user_id = "user123"
        mock_subscription.name = "Test Subscription"
        mock_subscription.subscription_type = "topic"
        mock_subscription.query_or_topic = "AI news"
        mock_subscription.refresh_interval_minutes = 60
        mock_subscription.created_at = datetime.now(timezone.utc)
        mock_subscription.updated_at = datetime.now(timezone.utc)
        mock_subscription.last_refresh = None
        mock_subscription.next_refresh = datetime.now(timezone.utc)
        mock_subscription.expires_at = None
        mock_subscription.source_type = None
        mock_subscription.source_id = None
        mock_subscription.created_from = None
        mock_subscription.folder = None
        mock_subscription.folder_id = None
        mock_subscription.notes = None
        mock_subscription.status = "active"
        mock_subscription.refresh_count = 0
        mock_subscription.results_count = 0
        mock_subscription.last_error = None
        mock_subscription.error_count = 0

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.get("sub-123")

        assert result is not None
        assert result["id"] == "sub-123"
        assert result["user_id"] == "user123"
        assert result["name"] == "Test Subscription"

    def test_get_nonexistent_subscription(self):
        """Test getting a nonexistent subscription returns None."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.get("nonexistent-id")

        assert result is None


class TestUpdate:
    """Tests for update() method."""

    def test_update_existing_subscription(self):
        """Test updating an existing subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.update("sub-123", {"name": "Updated Name"})

        assert result is True
        assert mock_subscription.name == "Updated Name"
        mock_session.commit.assert_called_once()

    def test_update_nonexistent_subscription(self):
        """Test updating a nonexistent subscription returns False."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.update("nonexistent-id", {"name": "New Name"})

        assert result is False

    def test_update_refresh_interval_recalculates_next_refresh(self):
        """Test that updating refresh_interval_minutes recalculates next_refresh."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_subscription.next_refresh = datetime.now(timezone.utc)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        storage.update("sub-123", {"refresh_interval_minutes": 120})

        # next_refresh should be updated
        assert mock_subscription.next_refresh is not None


class TestDelete:
    """Tests for delete() method."""

    def test_delete_existing_subscription(self):
        """Test deleting an existing subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.delete("sub-123")

        assert result is True
        mock_session.delete.assert_called_once_with(mock_subscription)
        mock_session.commit.assert_called_once()

    def test_delete_nonexistent_subscription(self):
        """Test deleting a nonexistent subscription returns False."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.delete("nonexistent-id")

        assert result is False


class TestList:
    """Tests for list() method."""

    def test_list_all_subscriptions(self):
        """Test listing all subscriptions."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_sub1 = MagicMock()
        mock_sub1.id = "sub-1"
        mock_sub1.user_id = "user1"
        mock_sub1.name = "Sub 1"
        mock_sub1.subscription_type = "topic"
        mock_sub1.query_or_topic = "Topic 1"
        mock_sub1.refresh_interval_minutes = 60
        mock_sub1.created_at = datetime.now(timezone.utc)
        mock_sub1.updated_at = datetime.now(timezone.utc)
        mock_sub1.last_refresh = None
        mock_sub1.next_refresh = datetime.now(timezone.utc)
        mock_sub1.status = "active"
        mock_sub1.folder = None
        mock_sub1.notes = None

        mock_query = MagicMock()
        mock_query.limit.return_value.offset.return_value.all.return_value = [
            mock_sub1
        ]
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.list()

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

    def test_list_with_user_filter(self):
        """Test listing subscriptions with user filter."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.limit.return_value.offset.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        storage.list(filters={"user_id": "user123"})

        mock_query.filter_by.assert_called_once_with(user_id="user123")

    def test_list_with_pagination(self):
        """Test listing subscriptions with pagination."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.limit.return_value.offset.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        storage.list(limit=50, offset=10)

        mock_query.limit.assert_called_once_with(50)
        mock_query.limit.return_value.offset.assert_called_once_with(10)


class TestGetActiveSubscriptions:
    """Tests for get_active_subscriptions() method."""

    def test_get_active_subscriptions_all_users(self):
        """Test getting active subscriptions for all users."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.database.models.news import SubscriptionStatus

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_sub = MagicMock()
        mock_sub.to_dict.return_value = {"id": "sub-1", "status": "active"}

        mock_query = MagicMock()
        mock_query.filter_by.return_value.all.return_value = [mock_sub]
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.get_active_subscriptions()

        assert len(result) == 1
        mock_query.filter_by.assert_called_once_with(
            status=SubscriptionStatus.ACTIVE
        )

    def test_get_active_subscriptions_for_user(self):
        """Test getting active subscriptions for specific user."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.filter_by.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        storage.get_active_subscriptions(user_id="user123")

        # Should filter by user_id after filtering by status
        assert mock_query.filter_by.call_count >= 1


class TestGetDueSubscriptions:
    """Tests for get_due_subscriptions() method."""

    def test_get_due_subscriptions(self):
        """Test getting subscriptions due for refresh."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_sub = MagicMock()
        mock_sub.to_dict.return_value = {
            "id": "sub-1",
            "next_refresh": datetime.now(timezone.utc),
        }

        mock_query = MagicMock()
        mock_query.filter.return_value.limit.return_value.all.return_value = [
            mock_sub
        ]
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.get_due_subscriptions()

        assert len(result) == 1

    def test_get_due_subscriptions_with_limit(self):
        """Test getting due subscriptions with limit."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter.return_value.limit.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        storage.get_due_subscriptions(limit=50)

        mock_query.filter.return_value.limit.assert_called_once_with(50)


class TestUpdateRefreshTime:
    """Tests for update_refresh_time() method."""

    def test_update_refresh_time_success(self):
        """Test successful refresh time update."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        now = datetime.now(timezone.utc)
        next_refresh = now + timedelta(hours=1)

        result = storage.update_refresh_time("sub-123", now, next_refresh)

        assert result is True
        assert mock_subscription.last_refresh == now
        assert mock_subscription.next_refresh == next_refresh
        mock_session.commit.assert_called_once()

    def test_update_refresh_time_not_found(self):
        """Test refresh time update for nonexistent subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        now = datetime.now(timezone.utc)

        result = storage.update_refresh_time("nonexistent", now, now)

        assert result is False


class TestIncrementStats:
    """Tests for increment_stats() method."""

    def test_increment_stats_success(self):
        """Test successful stats increment."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_subscription.refresh_count = 5
        mock_subscription.results_count = 50

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.increment_stats("sub-123", 10)

        assert result is True
        assert mock_subscription.refresh_count == 6
        assert mock_subscription.results_count == 10
        mock_session.commit.assert_called_once()

    def test_increment_stats_not_found(self):
        """Test stats increment for nonexistent subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.increment_stats("nonexistent", 10)

        assert result is False


class TestPauseSubscription:
    """Tests for pause_subscription() method."""

    def test_pause_subscription_success(self):
        """Test successful subscription pause."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.database.models.news import SubscriptionStatus

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.pause_subscription("sub-123")

        assert result is True
        assert mock_subscription.status == SubscriptionStatus.PAUSED
        mock_session.commit.assert_called_once()

    def test_pause_subscription_not_found(self):
        """Test pausing nonexistent subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.pause_subscription("nonexistent")

        assert result is False


class TestResumeSubscription:
    """Tests for resume_subscription() method."""

    def test_resume_subscription_success(self):
        """Test successful subscription resume."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.database.models.news import SubscriptionStatus

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_subscription.status = SubscriptionStatus.PAUSED
        mock_subscription.refresh_interval_minutes = 60

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.resume_subscription("sub-123")

        assert result is True
        assert mock_subscription.status == SubscriptionStatus.ACTIVE
        mock_session.commit.assert_called_once()

    def test_resume_subscription_not_paused(self):
        """Test resuming a subscription that's not paused returns False."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.database.models.news import SubscriptionStatus

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_subscription.status = SubscriptionStatus.ACTIVE  # Not paused

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.resume_subscription("sub-123")

        assert result is False


class TestExpireSubscription:
    """Tests for expire_subscription() method."""

    def test_expire_subscription_success(self):
        """Test successful subscription expiration."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.database.models.news import SubscriptionStatus

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_subscription = MagicMock()
        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = mock_subscription
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.expire_subscription("sub-123")

        assert result is True
        assert mock_subscription.status == SubscriptionStatus.EXPIRED
        assert mock_subscription.expires_at is not None
        mock_session.commit.assert_called_once()

    def test_expire_subscription_not_found(self):
        """Test expiring nonexistent subscription."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.filter_by.return_value.first.return_value = None
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.expire_subscription("nonexistent")

        assert result is False


class TestInheritance:
    """Tests for SubscriptionStorage inheritance."""

    def test_inherits_from_subscription_storage(self):
        """Test that SQLSubscriptionStorage inherits from SubscriptionStorage."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )
        from local_deep_research.news.core.storage import SubscriptionStorage

        mock_session = MagicMock()
        storage = SQLSubscriptionStorage(mock_session)

        assert isinstance(storage, SubscriptionStorage)

    def test_has_generate_id_method(self):
        """Test that storage has generate_id method."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        storage = SQLSubscriptionStorage(mock_session)

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


class TestEdgeCases:
    """Edge case tests."""

    def test_empty_filters_in_list(self):
        """Test list with empty filters dict."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_query = MagicMock()
        mock_query.limit.return_value.offset.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        storage = SQLSubscriptionStorage(mock_session)
        result = storage.list(filters={})

        assert result == []

    @patch(
        "local_deep_research.news.subscription_manager.storage.NewsSubscription"
    )
    def test_default_values_in_create(self, mock_model_class):
        """Test that default values are applied in create."""
        from local_deep_research.news.subscription_manager.storage import (
            SQLSubscriptionStorage,
        )

        mock_session = MagicMock()
        mock_session.__enter__ = MagicMock(return_value=mock_session)
        mock_session.__exit__ = MagicMock(return_value=False)

        mock_model_class.return_value = MagicMock()

        storage = SQLSubscriptionStorage(mock_session)
        storage.generate_id = MagicMock(return_value="gen-id")

        # Minimal required fields
        data = {
            "user_id": "user123",
            "subscription_type": "topic",
            "query_or_topic": "Test",
            "refresh_interval_minutes": 60,
        }

        result = storage.create(data)

        # Should succeed with minimal data
        assert result == "gen-id"
