import pytest
from django.contrib.auth import get_user_model
from apps.publications.models import Publication, PublicationCategory, Author, Submission, ExternalPublication


@pytest.fixture
def user(db):
    User = get_user_model()
    return User.objects.create_user(email="pub@example.com", password="pass123")


@pytest.fixture
def category(db):
    return PublicationCategory.objects.create(name="Technology")


@pytest.mark.django_db
class TestPublicationCategoryModel:
    def test_create_category(self):
        cat = PublicationCategory.objects.create(name="Science")
        assert cat.slug == "science"


@pytest.mark.django_db
class TestPublicationModel:
    def test_create_publication(self, user, category):
        pub = Publication.objects.create(
            title="AI Trends 2026",
            content="Article content...",
            author=user,
            category=category
        )
        assert pub.status == "draft"


@pytest.mark.django_db
class TestAuthorModel:
    def test_create_author(self, user):
        author = Author.objects.create(
            user=user,
            bio="Tech writer"
        )
        assert author.bio == "Tech writer"


@pytest.mark.django_db
class TestSubmissionModel:
    def test_create_submission(self, user, category):
        pub = Publication.objects.create(
            title="Research Paper",
            content="Research content",
            author=user,
            category=category
        )
        submission = Submission.objects.create(
            publication=pub,
            submitted_by=user
        )
        assert submission.status == "submitted"


@pytest.mark.django_db
class TestExternalPublicationModel:
    def test_create_external_publication(self):
        ext_pub = ExternalPublication.objects.create(
            title="Industry Report 2025",
            source="Research Firm",
            url="https://example.com/report"
        )
        assert ext_pub.source == "Research Firm"