from rest_framework.decorators import api_view
from rest_framework.exceptions import PermissionDenied
from rest_framework.response import Response

from vbv_lernwelt.course.models import CircleDocument
from vbv_lernwelt.course.serializers import CircleDocumentSerializer
from vbv_lernwelt.iam.permissions import has_course_session_access


@api_view(["GET"])
def get_course_session_documents(request, course_session_id):
    if not has_course_session_access(request.user, course_session_id):
        raise PermissionDenied()

    circle_documents = CircleDocument.objects.filter(
        course_session_id=course_session_id
    )

    return Response(
        status=200, data=CircleDocumentSerializer(circle_documents, many=True).data
    )
