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


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


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


@pytest.mark.django_db
class TestCompanyModel:
    def test_create_company(self):
        company = Company.objects.create(
            name="Tech Startup Inc",
            industry="technology"
        )
        assert company.slug == "tech-startup-inc"
        assert company.industry == "technology"


@pytest.mark.django_db
class TestContactModel:
    def test_create_contact(self, user, company):
        contact = Contact.objects.create(
            name="John Doe",
            email="john@acme.com",
            phone="08123456789",
            company=company,
            created_by=user
        )
        assert contact.name == "John Doe"
        assert contact.type == "individual"

    def test_contact_types(self, user):
        Contact.objects.create(name="Individual Contact", type="individual", created_by=user)
        Contact.objects.create(name="Org Contact", type="organization", created_by=user)
        # Scope to created_by: the hr→contacts signal bridge auto-creates a
        # Contact for every user, which would pollute an unscoped count.
        assert Contact.objects.filter(type="individual", created_by=user).count() == 1
        assert Contact.objects.filter(type="organization", created_by=user).count() == 1


