import pytest
from django.contrib.auth import get_user_model
from datetime import datetime, timedelta
from apps.activities.models import Activity
from apps.contacts.models import Contact
from apps.companies.models import Company


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


@pytest.fixture
def contact(db, user):
    return Contact.objects.create(
        name="John Doe",
        email="john@example.com",
        created_by=user
    )


@pytest.fixture
def company(db, user):
    return Company.objects.create(
        name="Acme Corp",
        created_by=user
    )


@pytest.mark.django_db
class TestActivityModel:
    def test_create_activity(self, user, contact):
        activity = Activity.objects.create(
            title="Sales Call",
            activity_type="call",
            description="Discussed new product",
            contact=contact,
            user=user
        )
        assert activity.title == "Sales Call"
        assert activity.activity_type == "call"
        assert activity.is_completed is False

    def test_activity_types(self, user, contact):
        Activity.objects.create(title="Call", activity_type="call", contact=contact, user=user)
        Activity.objects.create(title="Email", activity_type="email", contact=contact, user=user)
        Activity.objects.create(title="Meeting", activity_type="meeting", contact=contact, user=user)
        assert Activity.objects.filter(activity_type="call").count() == 1
        assert Activity.objects.filter(activity_type="email").count() == 1
        assert Activity.objects.filter(activity_type="meeting").count() == 1

    def test_activity_completion(self, user, contact):
        activity = Activity.objects.create(
            title="Follow up",
            activity_type="task",
            contact=contact,
            user=user
        )
        activity.is_completed = True
        activity.completed_at = datetime.now()
        activity.save()
        activity.refresh_from_db()
        assert activity.is_completed is True
        assert activity.completed_at is not None

    def test_activity_with_due_date(self, user, contact):
        due = datetime.now() + timedelta(days=7)
        activity = Activity.objects.create(
            title="Deadline Task",
            activity_type="deadline",
            contact=contact,
            user=user,
            due_date=due
        )
        assert activity.due_date == due

    def test_activity_linked_to_company(self, user, company):
        activity = Activity.objects.create(
            title="Company Meeting",
            activity_type="meeting",
            company=company,
            user=user
        )
        assert activity.company == company
        assert activity.contact is None

    def test_activity_metadata(self, user, contact):
        metadata = {"priority": "high", "category": "sales"}
        activity = Activity.objects.create(
            title="Important Call",
            activity_type="call",
            contact=contact,
            user=user,
            metadata=metadata
        )
        assert activity.metadata == {"priority": "high", "category": "sales"}
