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

from vbv_lernwelt.core.serializers import UserSerializer


@api_view(["GET", "PUT"])
def me_user_view(request):
    if not request.user.is_authenticated:
        return Response(status=403)

    if request.method == "GET":
        return Response(UserSerializer(request.user).data)

    if request.method == "PUT":
        serializer = UserSerializer(
            request.user,
            data=request.data,
            partial=True,
        )
        if serializer.is_valid():
            serializer.save()
            return Response(UserSerializer(request.user).data)

    return Response(status=400)
