from rest_framework.fields import SerializerMethodField

from vbv_lernwelt.competence.serializers import (
    PerformanceCriteriaLearningPathSerializer,
)
from vbv_lernwelt.course.serializer_helpers import get_course_serializer_class
from vbv_lernwelt.learnpath.models import LearningUnit


class LearningUnitSerializer(
    get_course_serializer_class(
        LearningUnit,
        field_names=["evaluate_url", "course_category", "children", "title_hidden"],
    )
):
    evaluate_url = SerializerMethodField()

    def get_children(self, obj):
        return [
            PerformanceCriteriaLearningPathSerializer(child).data
            for child in obj.performancecriteria_set.all()
        ]

    def get_evaluate_url(self, obj):
        return obj.get_evaluate_url()


class LearningUnitPerformanceCriteriaSerializer(
    get_course_serializer_class(
        LearningUnit,
        field_names=[
            "evaluate_url",
            "course_category",
        ],
    )
):
    evaluate_url = SerializerMethodField()

    def get_evaluate_url(self, obj):
        return obj.get_evaluate_url()
