"""Tests for campaigns app models."""

import pytest
from django.utils import timezone

from .factories import CampaignFactory


@pytest.mark.django_db
class TestCampaign:
    """Tests for the Campaign model."""

    def test_create_campaign(self):
        """Test creating a campaign with the factory."""
        campaign = CampaignFactory()
        assert campaign.pk is not None
        assert campaign.name.startswith("Campaign")
        assert campaign.workspace is not None

    def test_campaign_str(self):
        """Test campaign string representation."""
        campaign = CampaignFactory(name="Summer Newsletter")
        assert str(campaign) == "Summer Newsletter"

    def test_campaign_default_status(self):
        """Test campaign default status is draft."""
        campaign = CampaignFactory()
        assert campaign.status == "draft"

    def test_campaign_status_choices(self):
        """Test campaign with different status choices."""
        statuses = ["draft", "scheduled", "sent"]
        for status in statuses:
            campaign = CampaignFactory(status=status)
            assert campaign.status == status

    def test_campaign_default_channel(self):
        """Test campaign default channel is announcement."""
        campaign = CampaignFactory()
        assert campaign.channel == "announcement"

    def test_campaign_channel_choices(self):
        """Test campaign with different channel choices."""
        channels = ["announcement", "email"]
        for channel in channels:
            campaign = CampaignFactory(channel=channel)
            assert campaign.channel == channel

    def test_campaign_without_send_at(self):
        """Test campaign can be created without send_at."""
        campaign = CampaignFactory(send_at=None)
        assert campaign.send_at is None

    def test_campaign_with_send_at(self):
        """Test campaign with scheduled send_at."""
        future_time = timezone.now() + timezone.timedelta(days=7)
        campaign = CampaignFactory(send_at=future_time)
        assert campaign.send_at is not None
        assert campaign.send_at >= timezone.now()

    def test_campaign_has_timestamps(self):
        """Test that campaign has created_at and updated_at."""
        campaign = CampaignFactory()
        assert campaign.created_at is not None
        assert campaign.updated_at is not None

    def test_campaign_belongs_to_workspace(self):
        """Test campaign belongs to a workspace."""
        campaign = CampaignFactory()
        assert campaign.workspace is not None
        assert campaign in campaign.workspace.campaigns.all()
