from rest_framework import serializers

from vbv_lernwelt.course.models import (
    CircleDocument,
    Course,
    CourseCategory,
    CourseCompletion,
    CourseSession,
)


class CourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = ["id", "title", "category_name", "slug"]


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


class CourseCompletionSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCompletion
        fields = [
            "id",
            "created_at",
            "updated_at",
            "user",
            "page_key",
            "page_type",
            "page_slug",
            "course_session",
            "completion_status",
            "additional_json_data",
        ]


class CourseSessionSerializer(serializers.ModelSerializer):
    course = serializers.SerializerMethodField()
    course_url = serializers.SerializerMethodField()
    learning_path_url = serializers.SerializerMethodField()
    cockpit_url = serializers.SerializerMethodField()
    competence_url = serializers.SerializerMethodField()
    media_library_url = serializers.SerializerMethodField()
    documents = 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_learning_path_url(self, obj):
        return obj.course.get_learning_path_url()

    def get_cockpit_url(self, obj):
        return obj.course.get_cockpit_url()

    def get_media_library_url(self, obj):
        return obj.course.get_media_library_url()

    def get_competence_url(self, obj):
        return obj.course.get_competence_url()

    def get_documents(self, obj):
        documents = CircleDocument.objects.filter(
            course_session=obj, file__upload_finished_at__isnull=False
        )
        return CircleDocumentSerializer(documents, many=True).data

    class Meta:
        model = CourseSession
        fields = [
            "id",
            "created_at",
            "updated_at",
            "course",
            "title",
            "start_date",
            "end_date",
            "additional_json_data",
            "attendance_courses",
            "assignment_details_list",
            "learning_path_url",
            "cockpit_url",
            "competence_url",
            "media_library_url",
            "course_url",
            "documents",
        ]


class CircleDocumentSerializer(serializers.ModelSerializer):
    class Meta:
        model = CircleDocument
        fields = [
            "id",
            "name",
            "file_name",
            "url",
            "course_session",
            "learning_sequence",
        ]


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.IntegerField()
