from rest_framework import serializers

from vbv_lernwelt.core.serializers import UserSerializer
from vbv_lernwelt.learning_mentor.models import LearningMentor, MentorInvitation


class MentorAssignmentCompletionSerializer(serializers.Serializer):
    status = serializers.SerializerMethodField()
    user_id = serializers.CharField()
    last_name = serializers.CharField()
    url = serializers.CharField()

    @staticmethod
    def get_status(obj):
        return obj.status.value


class MentorAssignmentStatusSerializer(serializers.Serializer):
    id = serializers.CharField()
    title = serializers.CharField()
    circle_id = serializers.CharField()
    pending_evaluations = serializers.IntegerField()
    completions = MentorAssignmentCompletionSerializer(many=True)
    type = serializers.ReadOnlyField()


class InvitationSerializer(serializers.ModelSerializer):
    class Meta:
        model = MentorInvitation
        fields = ["id", "email"]
        read_only_fields = ["id"]

    def create(self, validated_data):
        participant = self.context["course_session_user"]
        invitation, _ = MentorInvitation.objects.get_or_create(
            email=validated_data["email"], participant=participant
        )
        return invitation


class MentorSerializer(serializers.ModelSerializer):
    mentor = UserSerializer(read_only=True)

    class Meta:
        model = LearningMentor
        fields = ["id", "mentor"]
        read_only_fields = ["id"]
