import pytest
from django.contrib.auth import get_user_model
from datetime import date, timedelta, datetime
from apps.events.models import Event, EventTemplate, EventInvitation


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


@pytest.fixture
def event(user):
    return Event.objects.create(
        name="Tech Conference",
        event_type="conference",
        start_date=datetime.now(),
        end_date=datetime.now() + timedelta(days=2),
        organizer=user
    )


@pytest.mark.django_db
class TestEventModel:
    def test_create_event(self, user):
        evt = Event.objects.create(
            name="Workshop",
            event_type="workshop",
            start_date=datetime.now(),
            end_date=datetime.now() + timedelta(hours=4),
            organizer=user
        )
        assert evt.name == "Workshop"
        assert evt.status == "draft"

    def test_event_types(self, user):
        for event_type in ["webinar", "workshop", "conference"]:
            evt = Event.objects.create(
                name=f"{event_type.title()} Event",
                event_type=event_type,
                start_date=datetime.now(),
                end_date=datetime.now() + timedelta(hours=2),
                organizer=user
            )
            assert evt.event_type == event_type


@pytest.mark.django_db
class TestEventTemplateModel:
    def test_create_template(self, user):
        template = EventTemplate.objects.create(
            name="Standard Webinar",
            event_type="webinar",
            duration_hours=2.5,
            created_by=user
        )
        assert template.duration_hours == 2.5


@pytest.mark.django_db
class TestEventInvitationModel:
    def test_create_invitation(self, event, user):
        invitation = EventInvitation.objects.create(
            event=event,
            email="attendee@example.com",
            name="John Doe",
            invited_by=user
        )
        assert invitation.status == "pending"

    def test_invitation_unique_email_per_event(self, event, user):
        EventInvitation.objects.create(
            event=event,
            email="same@example.com",
            name="First",
            invited_by=user
        )
        with pytest.raises(Exception):
            EventInvitation.objects.create(
                event=event,
                email="same@example.com",
                name="Second",
                invited_by=user
            )