from rest_framework import serializers

from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.models import CourseSessionUser


class UserSerializer(serializers.ModelSerializer):
    course_session_experts = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = [
            "id",
            "first_name",
            "last_name",
            "email",
            "username",
            "avatar_url",
            "is_superuser",
            "course_session_experts",
        ]

    def get_course_session_experts(self, obj):
        qs = CourseSessionUser.objects.filter(
            role=CourseSessionUser.Role.EXPERT, user=obj
        )

        return [csu.course_session.id for csu in qs]
