from rest_framework.response import Response
from utils.paginations import OurLimitOffsetPagination


class AppointmentLimitOffsetPagination(OurLimitOffsetPagination):
    def get_paginated_response(self, data):
        view = self.request.parser_context['view']
        queryset = view.filter_queryset(view.get_queryset())

        total_offtime_bookings = queryset.filter(offtime=1).count()
        total_ontime_bookings = queryset.filter(offtime=0).count()
        
        total_all = total_offtime_bookings + total_ontime_bookings
                
        offtime_percentage = round(
            (total_offtime_bookings / total_all) * 100, 2
        ) if total_all else 0
        ontime_percentage = round(
            (total_ontime_bookings / total_all) * 100, 2
        ) if total_all else 0

        return Response({
            'count': total_all,
            'next': self.get_next_link(),
            'previous': self.get_previous_link(),
            'total_offtime': total_offtime_bookings,
            'total_ontime': total_ontime_bookings,
            'offtime_percentage': offtime_percentage,
            'ontime_percentage': ontime_percentage,
            'results': data,
        })
