import pytest
from django.contrib.auth import get_user_model
from rest_framework.test import APIClient
from apps.contacts.models import Contact, Note
from apps.companies.models import Company, Organization
from apps.activities.models import Activity


@pytest.fixture
def organization(db):
    return Organization.objects.create(name="Test Org")


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


@pytest.fixture
def admin_user(db):
    User = get_user_model()
    return User.objects.create_superuser(email="admin-notes@example.com", password="pass123")


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


@pytest.mark.django_db
class TestCompanyModel:
    def test_create_company(self, organization):
        company = Company.objects.create(
            organization=organization,
            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, organization):
        contact = Contact.objects.create(
            organization=organization,
            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, organization):
        Contact.objects.create(organization=organization, name="Individual Contact", type="individual", created_by=user)
        Contact.objects.create(organization=organization, 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


@pytest.mark.django_db
class TestNoteModel:
    def test_create_note(self, user, organization):
        note = Note.objects.create(
            organization=organization,
            title="Post-meeting summary",
            body="Discussed roadmap",
            contact="John Smith",
            company="Acme",
            tags=["depol", "ekon"],
            created_by=user,
        )
        assert str(note) == "Post-meeting summary"
        assert note.tags == ["depol", "ekon"]


@pytest.mark.django_db
class TestNoteViewSet:
    def test_create_and_list(self, admin_user):
        client = APIClient()
        client.force_authenticate(user=admin_user)
        resp = client.post("/api/notes/", {
            "title": "Meeting", "body": "x", "contact": "Jane",
            "company": "Acme", "tags": ["a"],
        }, format="json")
        assert resp.status_code == 201, resp.content
        assert resp.data["created_by_name"]
        listed = client.get("/api/notes/")
        assert listed.status_code == 200
        assert len(listed.data) == 1

    def test_scoped_to_creator(self, admin_user):
        User = get_user_model()
        other = User.objects.create_superuser(email="other-notes@example.com", password="pass123")
        admin_org = admin_user.organizations.first()
        other_org = other.organizations.first()
        Note.objects.create(organization=admin_org, title="Mine", created_by=admin_user)
        Note.objects.create(organization=other_org, title="Theirs", created_by=other)
        client = APIClient()
        client.force_authenticate(user=admin_user)
        resp = client.get("/api/notes/")
        titles = [n["title"] for n in resp.data]
        assert titles == ["Mine"]


