
    Eoh                     d    d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	  ede      Z
 G d d      Zy)	    N)DictListTypeTypeVar)	BaseModel)QueryResponseSchemaType)boundc                       e Zd ZdZdZdZdZdZeedfdede	e
   d	ed
ededefdZdefdZdefdZdedz  fdZdefdZdefdZdedz  fdZdee
   fdZdefdZy)QueryPaginatoraS  
    DB collection paginator.

    :param query: SQLAlchemy Query object
    :param schema: Pydantic BaseModel schema for response data serialization
    :param url: Base URL for pagination
    :param offset: Number of records to skip
    :param limit: Number of records to show
    :param use_orm: Flag to indicate whether to use ORM
    r   
   Ni  Tqueryschemaurloffsetlimituse_ormc                 t    || _         || _        || _        || _        || _        d | _        g | _        || _        y )N)r   r   r   r   r   countlistr   )selfr   r   r   r   r   r   s          V/var/www/html/wine-match-dev/backend/winematch-backend/src/utils/helpers/pagination.py__init__zQueryPaginator.__init__   s<     


.0	    returnc                 p    | j                   | j                  j                         | _         | j                   S )zRetrieve count of the records.)r   r   r   s    r   	get_countzQueryPaginator.get_count-   s*    ::))+DJzzr   c                 `    t        j                  | j                  | j                  z        dz   S )z!Retrieve the current page number.   )mathfloorr   r   r   s    r   get_current_pagezQueryPaginator.get_current_page3   s#    zz$++

23a77r   c                     | j                   | j                  z   | j                         k\  ry| j                   d| j	                         dz    d| j                   S )z+Construct URL for the next page of results.N?page=r    
&per_page=)r   r   r   r   r#   r   s    r   get_next_urlzQueryPaginator.get_next_url7   sO    ;;#t~~'77((6$"7"7"9A"=!>jUUr   c                     t        j                  | j                         | j                  z        }|dk(  r|dz  }| j                   d| d| j                   S )z+Construct URL for the last page of results.r   r    r%   r&   )r!   ceilr   r   r   )r   	last_pages     r   get_last_urlzQueryPaginator.get_last_url=   sO    IIdnn.;<	>NI((6)JtzzlCCr   c                 8    | j                    d| j                   S )z,Construct URL for the first page of results.z?page=1&per_page=)r   r   r   s    r   get_first_urlzQueryPaginator.get_first_urlD   s    ((,TZZL99r   c                     | j                   dk  ry| j                   | j                  z
  dk  r| j                         S | j                   d| j	                         dz
   d| j                   S )z/Construct URL for the previous page of results.r   Nr%   r    r&   )r   r   r-   r   r#   r   s    r   get_previous_urlzQueryPaginator.get_previous_urlH   sc    ;;!;;#q(%%''((6$"7"7"9A"=!>jUUr   c           	         | j                   j                  | j                        j                  | j                        j                         }| j                  r:|D cg c]  }| j
                  j                  |       c}| _        | j                  S |D cg c]"  } | j
                  di |j                         $ c}| _        | j                  S c c}w c c}w )zRetrieve the list of records. )	r   r   r   allr   r   model_validater   _asdict)r   itemsitems      r   get_listzQueryPaginator.get_listP   s    

!!$++.44TZZ@DDF<<FKLd33D9LDI yy DII46t||~6IDIyy MIs   "C'Cc           	          | j                         }| j                         | j                         | j                  | j	                         | j                         | j                         | j                         |dS )z5Perform pagination and return the paginated response.)totalpageper_pagenextpreviousfirstlastresult)r7   r   r#   r   r'   r/   r-   r+   )r   resultss     r   paginatezQueryPaginator.paginateY   sg    --/^^%))+

%%'--/'')%%'	
 		
r   )__name__
__module____qualname____doc__default_offsetdefault_limit
max_offset	max_limitr   r   r	   strintboolr   r   r#   r'   r+   r-   r/   r   r7   r   rB   r1   r   r   r   r   
   s    	 NMJI %" '( 	
   $3 8# 8VcDj VDc D:s :V#* V$12 
$ 
r   r   )r!   typingr   r   r   r   pydanticr   sqlalchemy.ormr   r	   r   r1   r   r   <module>rQ      s.     , ,   1C [
 [
r   