import pytest
from django.contrib.auth import get_user_model
from datetime import date, timedelta
from apps.projects.models import Project, ProjectDocument, ProjectTeamMember


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


@pytest.fixture
def project(user):
    return Project.objects.create(
        name="Test Project",
        description="A test project",
    )


@pytest.mark.django_db
class TestProjectModel:
    def test_create_project(self, user):
        proj = Project.objects.create(
            name="New Project",
            description="Description",
            )
        assert proj.name == "New Project"
        assert proj.status == "planning"

    def test_project_str(self, project):
        assert str(project) == "Test Project"


@pytest.mark.django_db
class TestProjectDocumentModel:
    def test_create_document(self, project, user):
        doc = ProjectDocument.objects.create(
            project=project,
            name="README.md",
            uploaded_by=user
        )
        assert doc.name == "README.md"


@pytest.mark.django_db
class TestProjectTeamMemberModel:
    def test_add_team_member(self, project, user):
        member = ProjectTeamMember.objects.create(
            project=project,
            user=user,
            role="member"
        )
        assert member.role == "member"

    def test_unique_team_member(self, project, user):
        ProjectTeamMember.objects.create(project=project, user=user, role="member")
        with pytest.raises(Exception):
            ProjectTeamMember.objects.create(project=project, user=user, role="owner")