from django.http import HttpRequest
from graphene import List, String, relay
from graphene_django import DjangoObjectType

from engine.blog.models import Post, PostTag


class PostType(DjangoObjectType):
    tags = List(lambda: PostTagType)
    content = String()

    class Meta:
        model = Post
        fields = ["tags", "content", "title", "slug"]
        interfaces = (relay.Node,)

    def resolve_content(self: Post, _info: HttpRequest) -> str:
        return self.content.html.replace("\n", "<br/>")


class PostTagType(DjangoObjectType):
    class Meta:
        model = PostTag
        fields = "__all__"
        interfaces = (relay.Node,)
