from rest_framework import serializers

from vbv_lernwelt.core.utils import StringIDField
from vbv_lernwelt.duedate.models import DueDate


class DueDateSerializer(serializers.ModelSerializer):
    id = StringIDField()
    course_session_id = serializers.SerializerMethodField()
    circle = serializers.SerializerMethodField()

    class Meta:
        model = DueDate
        fields = [
            "id",
            "start",
            "end",
            "manual_override_fields",
            "title",
            "assignment_type_translation_key",
            "date_type_translation_key",
            "subtitle",
            "url",
            "url_expert",
            "course_session_id",
            "circle",
        ]

    def get_course_session_id(self, obj):
        return str(obj.course_session.id)

    def get_circle(self, obj):
        if obj.circle_data:
            return {
                "id": obj.circle_data.get("id"),
                "title": obj.circle_data.get("title"),
                "slug": obj.circle_data.get("slug"),
            }
        return None
