from math import ceil

class ReusablePaginationMixin:
    """
    A mixin that encapsulates reusable pagination logic.
    Use in Django ListViews to generate custom pagination context.
    """
    per_page = 10  # Default; can be overridden in your view.

    def paginate_data(self, data_list, page_number):
        """
        Paginate a list of items.

        Arguments:
            data_list (list): The list of items to paginate.
            page_number (int): Current page number.

        Returns:
            tuple: (paginated_items, pagination_context)
        """
        total_items = len(data_list)
        num_pages = ceil(total_items / self.per_page) if self.per_page > 0 else 1

        # Ensure page_number is within valid bounds.
        if page_number < 1:
            page_number = 1
        elif page_number > num_pages:
            page_number = num_pages

        start = (page_number - 1) * self.per_page
        end = page_number * self.per_page
        items = data_list[start:end]

        pagination_context = {
            "number": page_number,
            "has_previous": page_number > 1,
            "has_next": page_number < num_pages,
            "previous_page": page_number - 1 if page_number > 1 else 1,
            "next_page": page_number + 1 if page_number < num_pages else num_pages,
            "paginator": {"num_pages": num_pages},
        }
        return items, pagination_context 