"""
Custom pagination class for DRF to support dynamic page_size query parameter
and unified response format
"""
from rest_framework.pagination import PageNumberPagination
from .response import get_request_id


class CustomPageNumberPagination(PageNumberPagination):
    """
    Custom pagination class that allows clients to override the page size
    via the page_size query parameter.
    
    Default page size: 10
    Max page size: 100
    
    Returns unified format with success field
    """
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100
    
    def paginate_queryset(self, queryset, request, view=None):
        """
        Override to store request for later use in get_paginated_response
        """
        self.request = request
        return super().paginate_queryset(queryset, request, view)
    
    def get_paginated_response(self, data):
        """
        Return a paginated response with unified format including success field
        """
        from rest_framework.response import Response
        
        response_data = {
            'success': True,
            'count': self.page.paginator.count,
            'next': self.get_next_link(),
            'previous': self.get_previous_link(),
            'results': data
        }
        
        # Add request_id if request is available
        if hasattr(self, 'request') and self.request:
            response_data['request_id'] = get_request_id(self.request)
        
        return Response(response_data)

