
    YFi                     d    d dl mZmZmZmZ d dlmZ d dlmZ d dl	Z	 ede      Z
 G d d      Zy)	    )DictListTypeTypeVar)	BaseModel)QueryN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fdZdefdZdefdZdefdZdee	e
      fdZdefdZy)QueryPaginatoraT  
    DB collection paginator.

    :param query: SQLAlchemy Query object
    :param schema: Pydantic Base Model 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          L/var/www/html/BankruptcyAI-uat/bankruptcy-ai/src/utils/helpers/pagination.py__init__zQueryPaginator.__init__   s<     


46	    returnc                 X    | j                   j                         | _        | j                  S )zRetrieve count of the records.)r   r   r   s    r   	get_countzQueryPaginator.get_count,   s     ZZ%%'
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_page1   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_url5   sL    ;;#tzz1((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_urlB   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_urlF   sc    ;;!;;#q(%%''((6$"7"7"9A"=!>jUUr   c           	         | j                   j                  | j                        j                  | j                        j                         | _        | j
                  rD| j                  D cg c]  }| j                  j                  |       c}| _        | j                  S | j                  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   r   model_validate_asdict)r   items     r   get_listzQueryPaginator.get_listN   s    JJ%%dkk288DHHJ	<<FJiiPd33D9PDI yy DH99M46t||~6MDIyy QMs   ,"C+0'C0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)r6   r   r#   r   r'   r/   r-   r+   )r   resultss     r   paginatezQueryPaginator.paginateW   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   r6   r   rA   r1   r   r   r   r   	   s    	 NMJI %" '( 	
   $3 
8# 8Vc VDc D:s :V# V$t$678 
$ 
r   r   )typingr   r   r   r   pydantic.mainr   sqlalchemy.orm.queryr   r!   r	   r   r1   r   r   <module>rP      s.    , , # & 1C Z
 Z
r   