import graphene
from graphene_django import DjangoObjectType

from vbv_lernwelt.course_session.models import (
    CourseSessionAssignment,
    CourseSessionAttendanceCourse,
    CourseSessionEdoniqTest,
)
from vbv_lernwelt.course_session.services.attendance import AttendanceUserStatus
from vbv_lernwelt.duedate.graphql.types import DueDateObjectType
from vbv_lernwelt.iam.permissions import is_course_session_expert
from vbv_lernwelt.learnpath.graphql.types import (
    LearningContentAssignmentObjectType,
    LearningContentAttendanceCourseObjectType,
    LearningContentEdoniqTestObjectType,
)


class AttendanceUserObjectType(graphene.ObjectType):
    user_id = graphene.UUID(required=True)
    status = graphene.Field(
        graphene.Enum.from_enum(AttendanceUserStatus), required=True
    )
    first_name = graphene.String()
    last_name = graphene.String()
    email = graphene.String()


class CourseSessionAttendanceCourseObjectType(DjangoObjectType):
    course_session_id = graphene.ID(source="course_session_id")
    learning_content_id = graphene.ID(source="learning_content_id")
    attendance_user_list = graphene.List(
        AttendanceUserObjectType, source="attendance_user_list"
    )
    due_date = graphene.Field(DueDateObjectType)
    learning_content = graphene.Field(
        LearningContentAttendanceCourseObjectType, required=True
    )

    class Meta:
        model = CourseSessionAttendanceCourse
        fields = (
            "id",
            "learning_content",
            "location",
            "trainer",
            "due_date",
        )

    def resolve_attendance_user_list(self, info):
        if is_course_session_expert(info.context.user, self.course_session_id):
            return self.attendance_user_list
        return []


class CourseSessionAssignmentObjectType(DjangoObjectType):
    course_session_id = graphene.ID(source="course_session_id", required=True)
    learning_content_id = graphene.ID(source="learning_content_id", required=True)
    submission_deadline = graphene.Field(DueDateObjectType)
    evaluation_deadline = graphene.Field(DueDateObjectType)
    learning_content = graphene.Field(
        LearningContentAssignmentObjectType, required=True
    )

    class Meta:
        model = CourseSessionAssignment
        fields = (
            "id",
            "learning_content",
            "submission_deadline",
            "evaluation_deadline",
        )


class CourseSessionEdoniqTestObjectType(DjangoObjectType):
    course_session_id = graphene.ID(source="course_session_id", required=True)
    learning_content_id = graphene.ID(source="learning_content_id", required=True)

    deadline = graphene.Field(DueDateObjectType)
    learning_content = graphene.Field(
        LearningContentEdoniqTestObjectType, required=True
    )

    class Meta:
        model = CourseSessionEdoniqTest
        fields = (
            "id",
            "learning_content",
            "deadline",
        )
