from rest_framework.fields import SerializerMethodField

from vbv_lernwelt.core.serializer_helpers import (
    get_it_serializer_class,
    ItWagtailBaseSerializer,
)


class CourseBaseSerializer(ItWagtailBaseSerializer):
    course = SerializerMethodField()
    course_category = SerializerMethodField()
    circles = SerializerMethodField()

    meta_fields = []

    def get_course(self, obj):
        course = obj.get_course()
        if course:
            from vbv_lernwelt.course.serializers import CourseSerializer

            return CourseSerializer(course).data
        return ""

    def get_course_category(self, obj):
        from vbv_lernwelt.course.serializers import CourseCategorySerializer

        return CourseCategorySerializer(obj.course_category).data

    def get_circles(self, obj):
        circles = obj.get_circles()

        if circles:
            return [
                {"id": c.id, "title": c.title, "translation_key": c.translation_key}
                for c in circles
            ]

        return []


def get_course_serializer_class(model, field_names=None, base_field_names=None):
    return get_it_serializer_class(
        model, field_names, base_field_names, CourseBaseSerializer
    )
