import graphene
from graphene_django import DjangoObjectType

from vbv_lernwelt.course.models import Course
from vbv_lernwelt.course.permissions import has_course_access
from vbv_lernwelt.learnpath.models import LearningPath


class CoursePageInterface(graphene.Interface):
    id = graphene.ID()
    title = graphene.String()
    slug = graphene.String()
    content_type = graphene.String()
    live = graphene.Boolean()
    translation_key = graphene.String()
    frontend_url = graphene.String()

    def resolve_frontend_url(self, info):
        return self.get_frontend_url()


class LearningPathType(DjangoObjectType):
    class Meta:
        model = LearningPath
        interfaces = (CoursePageInterface,)


class CourseType(DjangoObjectType):
    learning_path = graphene.Field(LearningPathType)

    class Meta:
        model = Course
        fields = ("id", "title", "category_name", "slug", "learning_path")

    def resolve_learning_path(self, info):
        return self.get_learning_path()


class CourseQuery(graphene.ObjectType):
    course = graphene.Field(CourseType, id=graphene.Int())

    def resolve_course(root, info, id):
        course = Course.objects.get(pk=id)
        if has_course_access(info.context.user, course):
            return course
        raise PermissionError("You do not have access to this course")
