import graphene
from basicknowledge.models import BasicKnowledge
from books.models import Module
from graphene_django.filter import DjangoFilterConnectionField
from books.models.topic import Topic
from users.models import User

from .types import ActivityNode, PrivateUserNode


class UsersQuery(object):
    me = graphene.Field(PrivateUserNode)
    all_users = DjangoFilterConnectionField(PrivateUserNode)
    my_activity = DjangoFilterConnectionField("books.schema.nodes.ModuleNode")
    my_instrument_activity = DjangoFilterConnectionField(
        "basicknowledge.queries.InstrumentNode"
    )
    my_activities = graphene.Field(ActivityNode)

    def resolve_me(self, info, **kwargs):
        return info.context.user

    def resolve_all_users(self, info, **kwargs):
        if not info.context.user.is_superuser:
            return User.objects.none()
        else:
            return User.objects.all()

    def resolve_my_activity(self, info, **kwargs):
        return Module.objects.all()

    def resolve_my_instrument_activity(self, info, **kwargs):
        return BasicKnowledge.objects.filter(instrumentbookmark__user=info.context.user)

    def resolve_my_activities(self, info, **kwargs):
        topics = Topic.objects.live()
        instruments = BasicKnowledge.objects.live()
        return {"topics": topics, "instruments": instruments}


class AllUsersQuery(object):
    me = graphene.Field(PrivateUserNode)
    all_users = DjangoFilterConnectionField(PrivateUserNode)

    def resolve_all_users(self, info, **kwargs):
        if not info.context.user.is_superuser:
            return User.objects.none()
        else:
            return User.objects.all()
