from rest_framework.generics import ListAPIView
from api.property_api.property.models.property import Property
from api.property_api.property.serializers.property_serializer import PropertySerializer
from django.db.models import Q
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from common.pagination.custom_pagination import CustomPageNumberPagination

class PropertySearchView(ListAPIView):
    serializer_class = PropertySerializer
    pagination_class = CustomPageNumberPagination

    @swagger_auto_schema(manual_parameters=[
        openapi.Parameter('search', openapi.IN_QUERY, description="Search in name or description", type=openapi.TYPE_STRING),
        openapi.Parameter('name', openapi.IN_QUERY, description="Property name", type=openapi.TYPE_STRING),
        openapi.Parameter('country', openapi.IN_QUERY, description="Country name", type=openapi.TYPE_STRING),
        openapi.Parameter('city', openapi.IN_QUERY, description="City name", type=openapi.TYPE_STRING),
        openapi.Parameter('state', openapi.IN_QUERY, description="State name", type=openapi.TYPE_STRING),
        openapi.Parameter('min_price', openapi.IN_QUERY, description="Minimum price", type=openapi.TYPE_NUMBER),
        openapi.Parameter('max_price', openapi.IN_QUERY, description="Maximum price", type=openapi.TYPE_NUMBER),
        openapi.Parameter('property_type', openapi.IN_QUERY, description="Property Type (e.g. Buy, Sale, Rent)", type=openapi.TYPE_NUMBER),
    ])
    
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)
    
    def get_queryset(self):
        queryset = Property.objects.filter(is_active=True).select_related(
            'country', 'city', 'state', 'created_by'
        )
        
        search = self.request.query_params.get('search')
        name = self.request.query_params.get('name')
        country = self.request.query_params.get('country')
        city = self.request.query_params.get('city')
        state = self.request.query_params.get('state')
        max_price = self.request.query_params.get('max_price')
        min_price = self.request.query_params.get('min_price')
        property_type = self.request.query_params.get('property_type')

        if search:
            queryset = queryset.filter(
                Q(name__icontains=search) |
                Q(address__icontains=search) |
                Q(city__name__icontains=search) |
                Q(country__name__icontains=search) |
                Q(state__name__icontains=search)  
            )
        if name:
            queryset = queryset.filter(name__icontains=name)
        if country:
            queryset = queryset.filter(country__name__icontains=country)
        if city:
            queryset = queryset.filter(city__name__icontains=city)
        if state:
            queryset = queryset.filter(state__name__icontains=state)
        if max_price:
            queryset = queryset.filter(per_sqr_ft_price__lte=max_price)
        if min_price:
            queryset = queryset.filter(per_sqr_ft_price__gte=min_price)
        if property_type:
            queryset = queryset.filter(listed_for__iexact=property_type)

        return queryset