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

from vbv_lernwelt.assignment.models import AssignmentCompletion
from vbv_lernwelt.iam.permissions import is_course_session_expert

logger = structlog.get_logger(__name__)


@api_view(["GET"])
def request_assignment_completion_status(request, assignment_id, course_session_id):
    # TODO quickfix before GraphQL...
    if is_course_session_expert(request.user, course_session_id):
        qs = AssignmentCompletion.objects.filter(
            course_session_id=course_session_id,
            assignment_id=assignment_id,
        ).values(
            "id",
            "assignment_user_id",
            "completion_status",
            "evaluation_points",
            "evaluation_max_points",
            "evaluation_passed",
            "learning_content_page_id",
        )

        # Convert the learning_content_page_id to a string
        data = list(qs)  # Evaluate the queryset
        for item in data:
            item["learning_content_page_id"] = str(item["learning_content_page_id"])

        return Response(status=200, data=data)

    raise PermissionDenied()
