import pytest, json
from model_bakery import baker
from products.models import FeaturedProducts, Products
from products.serializers import FeaturedProductsSerializer
from blogs.models import Blogs
from django.contrib.contenttypes.models import ContentType


@pytest.mark.django_db()
class TestFeaturedProductsModel:
    def test_return_str_model(self):
        modified_body = json.dumps({"delta": "", "html": "Hello World"})

        blog = baker.make(Blogs, title="Blog 1", body=modified_body)
        blog_content_type = ContentType.objects.get_for_model(Blogs)

        product = baker.make(
            Products, name="Sample Product 1", description=modified_body
        )

        fp = FeaturedProducts.objects.create(
            model=blog_content_type,
            model_object_id=blog.id,
            product=product,
        )

        assert str(fp) == f"{fp.product.name} in {fp.model} {fp.model_object_id}"


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

        blog = baker.make(Blogs, title="Blog 1", body=modified_body)
        blog_content_type = ContentType.objects.get_for_model(Blogs)

        product = baker.make(
            Products, name="Sample Product 1", description=modified_body
        )

        FeaturedProducts.objects.create(
            model=blog_content_type,
            model_object_id=blog.id,
            product=product,
        )

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

        response = api_client.get("/api/products/featured-products/")

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