import structlog
from rest_framework import serializers
from wagtail.models import Page

from vbv_lernwelt.course.models import CourseSession
from vbv_lernwelt.feedback.models import FeedbackResponse

logger = structlog.get_logger(__name__)


class FeedbackResponseSerializer(serializers.ModelSerializer):
    page = serializers.CharField(write_only=True)
    course_session = serializers.CharField(write_only=True)

    class Meta:
        model = FeedbackResponse
        exclude = ["circle"]
        # extra_kwargs = {"course", {"read_only": True}}

    def create(self, validated_data):
        logger.info("creating feedback")
        page_key = validated_data.pop("page")
        course_session_id = validated_data.pop("course_session")

        learning_content = Page.objects.get(
            translation_key=page_key, locale__language_code="de-CH"
        )
        circle = learning_content.get_parent().specific
        course_session = CourseSession.objects.get(id=course_session_id)
        return FeedbackResponse.objects.create(
            **validated_data, circle=circle, course_session=course_session
        )
