import graphene
from graphene import List
from graphene_django import DjangoObjectType

from vbv_lernwelt.assignment.graphql.types import AssignmentObjectType
from vbv_lernwelt.competence.models import (
    ActionCompetence,
    CompetenceCertificate,
    CompetenceCertificateList,
    PerformanceCriteria,
)
from vbv_lernwelt.course.graphql.interfaces import CoursePageInterface
from vbv_lernwelt.learnpath.graphql.types import LearningUnitObjectType


class CompetenceCertificateObjectType(DjangoObjectType):
    assignments = List(graphene.NonNull(AssignmentObjectType), required=True)

    class Meta:
        model = CompetenceCertificate
        interfaces = (CoursePageInterface,)
        fields = ["assignments"]

    def resolve_assignments(self, info):
        return self.assignment_set.all()


class CompetenceCertificateListObjectType(DjangoObjectType):
    competence_certificates = List(
        graphene.NonNull(CompetenceCertificateObjectType), required=True
    )

    class Meta:
        model = CompetenceCertificateList
        interfaces = (CoursePageInterface,)
        fields = []

    def resolve_competence_certificates(self, info):
        return CompetenceCertificate.objects.child_of(self)


class PerformanceCriteriaObjectType(DjangoObjectType):
    learning_unit = graphene.Field(LearningUnitObjectType)

    class Meta:
        model = PerformanceCriteria
        interfaces = (CoursePageInterface,)
        fields = ["competence_id"]

    def resolve_learning_unit(self: PerformanceCriteria, info):
        return self.learning_unit


class ActionCompetenceObjectType(DjangoObjectType):
    performance_criteria = graphene.List(
        graphene.NonNull(PerformanceCriteriaObjectType), required=True
    )

    class Meta:
        model = ActionCompetence
        interfaces = (CoursePageInterface,)
        fields = ["competence_id"]

    def resolve_performance_criteria(self, info):
        return self.get_children().specific()
