import pytest, json
from django.contrib.auth import get_user_model
from model_bakery import baker
from projects.models import Projects, ProductFocus
from products.models import Products
from django.contrib.contenttypes.models import ContentType
from unittest.mock import patch
from django.core.files.uploadedfile import SimpleUploadedFile

Users = get_user_model()


@pytest.mark.django_db()
class TestProjectsModel:
    def test_return_str_model(self):
        modified_html = json.dumps({"delta": "", "html": "Hello World"})
        project = baker.make(
            Projects, description=modified_html, article_copy=modified_html
        )

        assert str(project) == f"{project.title} - {project.video_type}"


@pytest.mark.django_db()
class TestProductFocusModel:
    def test_return_str_model(self):
        modified_html = json.dumps({"delta": "", "html": "Hello World"})
        project = baker.make(
            Projects, description=modified_html, article_copy=modified_html
        )

        product_focus = baker.make(ProductFocus, project=project)

        assert (
            str(product_focus)
            == f"{product_focus.product_name} - {product_focus.collection} | {product_focus.project.title}"
        )


@pytest.mark.django_db()
class TestProjectsFetch:
    def setup_method(self):
        self.modified_html = json.dumps({"delta": "", "html": "Hello World"})

        self.copywriter = baker.make(Users, is_staff=True, role="Copywriter")
        self.web_admin = baker.make(Users, is_staff=True, role="Web Admin")

        self.user = baker.make(Users, is_staff=False)

        self.project_1 = baker.make(
            Projects,
            title="project 1",
            description=self.modified_html,
            article_copy=self.modified_html,
        )
        self.project_2 = baker.make(
            Projects,
            title="Project 2",
            description=self.modified_html,
            article_copy=self.modified_html,
        )

    def test_fetch_projects_by_any_user_return_200(self, authenticate_user):
        api_client = authenticate_user()

        response = api_client.get("/api/projects/")

        assert response.status_code == 200
        assert response.data["count"] == 2


@pytest.mark.django_db()
class TestProjectsCreate:
    def setup_method(self):
        self.modified_html = json.dumps({"delta": "", "html": "Hello World"})

        self.copywriter = baker.make(Users, is_staff=True, role="Copywriter")
        self.web_admin = baker.make(Users, is_staff=True, role="Web Admin")

        self.user = baker.make(Users, is_staff=False)

        self.product_1 = baker.make(
            Products,
            name="Product 1",
            edp_number="EDP-000",
            article_number="ART-000",
            price=100.0,
            description=self.modified_html,
        )

        self.project_1 = baker.make(
            Projects,
            title="project 1",
            description=self.modified_html,
            article_copy=self.modified_html,
        )

    def test_create_project_by_non_admin_returns_403(self, authenticate_user):
        api_client = authenticate_user()

        mock_image_1 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )
        mock_image_2 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )
        mock_image_3 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )

        data = {
            "title": "Test Project",
            "description": json.dumps({"delta": "", "html": "New Body"}),
            "article_copy": json.dumps({"delta": "", "html": "New Body"}),
            "images": [mock_image_1, mock_image_2],
            "video_title": "Test Video",
            "video_link": "https://example.youtube.com",
            "video_type": "YouTube",
            "product_focus": [
                {
                    "product_image": mock_image_3,
                    "collection": "Collection 1",
                    "product_name": "PF1",
                    "product_description": "Test Description",
                }
            ],
            "featured_products[]": [str(self.product_1.id)],
        }

        response = api_client.post("/api/projects/", data=data)

        assert response.status_code == 403

    def test_create_project_by_non_copywriter_admin_returns_403(
        self, authenticate_user
    ):
        api_client = authenticate_user(user=self.web_admin)

        mock_image_1 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )
        mock_image_2 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )
        mock_image_3 = SimpleUploadedFile(
            "image.jpg", b"mock-image-content", content_type="image/jpeg"
        )

        data = {
            "title": "Test Project",
            "description": json.dumps({"delta": "", "html": "New Body"}),
            "article_copy": json.dumps({"delta": "", "html": "New Body"}),
            "images": [mock_image_1, mock_image_2],
            "video_title": "Test Video",
            "video_link": "https://example.youtube.com",
            "video_type": "YouTube",
            "product_focus": [
                {
                    "product_image": mock_image_3,
                    "collection": "Collection 1",
                    "product_name": "PF1",
                    "product_description": "Test Description",
                }
            ],
            "featured_products[]": [str(self.product_1.id)],
        }

        response = api_client.post("/api/projects/", data=data)

        assert response.status_code == 403
