"""Tests for analytics app models."""

import pytest
from django.utils import timezone

from .factories import EventFactory


@pytest.mark.django_db
class TestEvent:
    """Tests for the Event model."""

    def test_create_event(self):
        """Test creating an event with the factory."""
        event = EventFactory()
        assert event.pk is not None
        assert event.name.startswith("event.")
        assert event.workspace is not None

    def test_event_str(self):
        """Test event string representation."""
        event = EventFactory(name="user.signup")
        assert str(event) == "user.signup"

    def test_event_with_member(self):
        """Test event associated with a member."""
        event = EventFactory()
        assert event.member is not None
        assert event.member.workspace == event.workspace

    def test_event_without_member(self):
        """Test event can be created without a member."""
        event = EventFactory(member=None)
        assert event.member is None
        assert event.pk is not None

    def test_event_with_properties(self):
        """Test event with custom properties."""
        properties = {"plan": "pro", "amount": 99.99}
        event = EventFactory(properties=properties)
        assert event.properties == properties

    def test_event_default_properties(self):
        """Test event has empty dict as default properties."""
        event = EventFactory()
        assert event.properties == {}

    def test_event_has_timestamps(self):
        """Test that event has created_at and updated_at."""
        event = EventFactory()
        assert event.created_at is not None
        assert event.updated_at is not None

    def test_event_occurred_at(self):
        """Test event occurred_at is set."""
        event = EventFactory()
        assert event.occurred_at is not None
        assert event.occurred_at <= timezone.now()

    def test_event_belongs_to_workspace(self):
        """Test event belongs to a workspace."""
        event = EventFactory()
        assert event.workspace is not None
        assert event in event.workspace.events.all()
