from rest_framework import serializers

from vbv_lernwelt.competence.models import PerformanceCriteria
from vbv_lernwelt.course.serializer_helpers import get_course_serializer_class
from vbv_lernwelt.course.serializers import CourseCategorySerializer


class PerformanceCriteriaSerializer(
    get_course_serializer_class(
        PerformanceCriteria,
        field_names=[
            "competence_id",
            "learning_unit",
            "circle",
            "course_category",
        ],
    )
):
    learning_unit = serializers.SerializerMethodField()
    circle = serializers.SerializerMethodField()
    course_category = serializers.SerializerMethodField()

    def get_learning_unit(self, obj):
        from vbv_lernwelt.learnpath.serializers import (
            LearningUnitPerformanceCriteriaSerializer,
        )

        return LearningUnitPerformanceCriteriaSerializer(obj.learning_unit).data

    def get_circle(self, obj):
        c = obj.learning_unit.get_parent()
        return {"id": c.id, "title": c.title, "translation_key": c.translation_key}

    def get_course_category(self, obj):
        if obj.learning_unit:
            return CourseCategorySerializer(obj.learning_unit.course_category).data
        return None


class PerformanceCriteriaLearningPathSerializer(
    get_course_serializer_class(
        PerformanceCriteria,
        field_names=[
            "competence_id",
        ],
    )
):
    pass
