from rest_framework import serializers

from vbv_lernwelt.core.utils import StringIDField
from vbv_lernwelt.course.models import (
    CircleDocument,
    Course,
    CourseCategory,
    CourseCompletion,
    CourseSession,
)
from vbv_lernwelt.duedate.models import DueDate
from vbv_lernwelt.duedate.serializers import DueDateSerializer


class CourseSerializer(serializers.ModelSerializer):
    id = StringIDField()

    class Meta:
        model = Course
        fields = ["id", "title", "category_name", "slug", "enable_circle_documents"]


class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategory
        fields = [
            "id",
            "title",
            "general",
        ]


class CourseCompletionSerializer(serializers.ModelSerializer):
    page_id = StringIDField()
    course_session_id = StringIDField()

    class Meta:
        model = CourseCompletion
        fields = [
            "id",
            "created_at",
            "updated_at",
            "user",
            "page_id",
            "page_type",
            "course_session_id",
            "completion_status",
            "additional_json_data",
        ]


class CourseSessionSerializer(serializers.ModelSerializer):
    id = StringIDField()

    course = serializers.SerializerMethodField()
    # course_url = serializers.SerializerMethodField()
    due_dates = serializers.SerializerMethodField()

    def get_course(self, obj):
        return CourseSerializer(obj.course).data

    # def get_course_url(self, obj):
    #     return obj.course.get_course_url()

    def get_due_dates(self, obj):
        due_dates = DueDate.objects.filter(course_session=obj)
        return DueDateSerializer(due_dates, many=True).data

    class Meta:
        model = CourseSession
        fields = [
            "id",
            "created_at",
            "updated_at",
            "course",
            "title",
            "start_date",
            "end_date",
            "due_dates",
        ]


class CircleDocumentSerializer(serializers.ModelSerializer):
    learning_sequence = serializers.SerializerMethodField()

    class Meta:
        model = CircleDocument
        fields = [
            "id",
            "name",
            "file_name",
            "url",
            "course_session",
            "learning_sequence",
        ]

    def get_learning_sequence(self, obj):
        ls = obj.learning_sequence
        circle = ls.get_circle()
        return {
            "title": ls.title,
            "id": str(ls.id),
            "slug": ls.slug,
            "circle": {
                "title": circle.title,
                "id": str(circle.id),
                "slug": circle.slug,
            },
        }


class DocumentUploadStartInputSerializer(serializers.Serializer):
    file_name = serializers.CharField()
    file_type = serializers.CharField()
    name = serializers.CharField()
    course_session = serializers.IntegerField()
    learning_sequence = serializers.IntegerField()


class DocumentUploadFinishInputSerializer(serializers.Serializer):
    file_id = serializers.UUIDField()
