import graphene
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from books.models import Topic, Module
from books.schema.nodes import ModuleNode


class TopicNode(DjangoObjectType):
    pk = graphene.Int()
    modules = DjangoFilterConnectionField(ModuleNode)

    class Meta:
        model = Topic
        only_fields = [
            'slug', 'title', 'teaser', 'description', 'vimeo_id', 'order', 'instructions'
        ]
        filter_fields = {
            'slug': ['exact', 'icontains', 'in'],
            'title': ['exact', 'icontains', 'in'],
        }
        interfaces = (relay.Node,)

    def resolve_pk(self, *args, **kwargs):
        return self.id

    def resolve_modules(self, *args, **kwargs):
        return Module.get_by_parent(self)
