from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.pagination import PageNumberPagination
from rest_framework.exceptions import ValidationError, NotFound
from drf_yasg.utils import swagger_auto_schema
from django.shortcuts import get_object_or_404

from api.property_api.featured_listings.models.featured_listings import FeaturedListings
from api.property_api.featured_listings.serializers.listings_serializer import FeaturedListingsSerializer


class FeaturedListingsListCreateAPIView(APIView):
    @swagger_auto_schema(request_body=FeaturedListingsSerializer)
    def post(self, request):
        serializer = FeaturedListingsSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"success": "Featured Listings created successfully"}, status=status.HTTP_201_CREATED)
    
    def get(self, request):
        listings = FeaturedListings.objects.filter(is_active=True)
        paginator = PageNumberPagination()
        result_page = paginator.paginate_queryset(listings, request)
        serializer = FeaturedListingsSerializer(result_page, many=True)
        return paginator.get_paginated_response(serializer.data)
    

class FeaturedListingsDetailAPIView(APIView):
    @swagger_auto_schema(request_body=FeaturedListingsSerializer)
    def put(self, request, pk):
        listings = get_object_or_404(FeaturedListings, pk=pk)
        serializer = FeaturedListingsSerializer(listings, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"success": "Featured Listings updated successfully"}, status=status.HTTP_200_OK)

    def get(self, request, pk):
        listings = get_object_or_404(FeaturedListings, pk=pk)
        serializer = FeaturedListingsSerializer(listings)
        return Response(serializer.data)

    def delete(self, request, pk):
        listings = get_object_or_404(FeaturedListings, pk=pk)
        listings.soft_delete()
        return Response({"success": "Featured Listings deleted successfully"}, status=status.HTTP_200_OK)
    