from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
from drf_yasg.utils import swagger_auto_schema
from api.property_api.property.models.property import Property
from api.property_api.property.serializers.property_partial_serializer import PropertyPartialUpdateSerializer
from api.property_api.property.serializers.property_serializer import PropertySerializer


class PropertyPartialUpdateAPIView(APIView):
    @swagger_auto_schema(request_body=PropertyPartialUpdateSerializer)
    def patch(self, request, pk):
        property = get_object_or_404(Property, pk=pk)

        serializer = PropertyPartialUpdateSerializer(
            property,
            data=request.data,
            partial=True
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(
            {"success": "Property updated successfully",
             "data": PropertySerializer(
                    serializer.instance,
                    context={"request": request}
                ).data},
            status=status.HTTP_200_OK
        )