
    {i                        d Z ddlmZ ddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZ erdd	lmZ  G d
 de      Zy)zr
Base ORM class for SQLAlchemy models.

SQLAlchemy 2.0+ implementation with enhanced type safety and performance.
    )annotations)datetime)AnyTYPE_CHECKING)UUID)inspect)DeclarativeBasedeclared_attrMappedmapped_column)symbol)InspectionAttrc                  X    e Zd ZdZej
                  dd       Zd	dZd	dZd
dZ	dddZ
y)Basez
    Enhanced ORM base class with SQLAlchemy 2.0+ features.
    
    Provides automatic table naming, JSON serialization, and hybrid property support.
    Uses modern type annotations and SQLAlchemy 2.0 declarative syntax.
    c                    t        | d      r| j                  r| j                         S | j                  j                         S )z
        Generate __tablename__ for the SQLAlchemy model.
        
        Returns the explicit tablename if set, otherwise converts class name to lowercase.
        __tablename__)hasattrr   __name__lower)clss    W/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/src/apps/base/models/base.pyr   zBase.__tablename__   s8     3(S->->$$&&||!!##    c                2    |j                   t        d      k(  S )z/Check if a model property is a hybrid property.HYBRID_PROPERTYextension_typer   selform_descriptors     r   _is_hybrid_propertyzBase._is_hybrid_property(   s    ,,7H0IIIr   c                2    |j                   t        d      k(  S )z-Check if a model property is a hybrid method.HYBRID_METHODr   r   s     r   _is_hybrid_methodzBase._is_hybrid_method,   s    ,,0GGGr   c                   i }|j                         D ]r  \  }}t        |t              rt        |      ||<   %t        |t              r|j                         ||<   It        |t              r| j                  |      ||<   n|||<   t |S )aP  
        Convert a dictionary to a JSON-serializable format.
        
        Handles UUID and datetime objects by converting them to strings.
        Recursively processes nested dictionaries.
        
        Args:
            data: Dictionary to convert
            
        Returns:
            JSON-serializable dictionary
        )items
isinstancer   strr   	isoformatdict_prepare_json)r   dataresultkeyvalues        r   r*   zBase._prepare_json0   s     **, 	$JC%&!%jsE8,#oo/sE4("007s#s	$ r   c                   t        |       j                  }|j                  }t        |j	                               }|D ci c]'  }t        | |      rt        | |      |t        | |      ) }}|r_|j                         D cg c])  \  }}| j                  |      s| j                  |      r|+ }	}}|	D ]  }
	 t        | |
      }||||
<    | j                  |      S c c}w c c}}w # t        t        f$ r Y Fw xY w)a  
        Convert SQLAlchemy model to dictionary.
        
        If the model has joined attributes, they will be transformed to nested dictionaries.
        If include_properties is True, hybrid properties and methods will be included.
        
        Args:
            include_properties: Whether to include hybrid properties/methods
            
        Returns:
            Dictionary representation of the model
        )r   mapperall_orm_descriptorslistkeysr   getattrr%   r    r#   AttributeError	Exceptionr*   )r   include_propertiesr0   orm_descriptorsdefined_namescolumnresultsr-   
descriptorhybrid_namesnamer.   s               r   _asdictzBase._asdictI   s)    %% 44 _1134 (
tV$v)>)J GD&))
 
  (7'<'<'>#C++J74;Q;QR\;] L  % #D$/E((- !!'**1
 '	2 s   ,C.C$9C**C<;C<N)returnr'   )r   r   r@   bool)r+   dict[str, Any]r@   rB   )T)r7   rA   r@   rB   )r   
__module____qualname____doc__r
   	directiver   r    r#   r*   r?    r   r   r   r      s7     $ $JH2*+r   r   N)rE   
__future__r   r   typingr   r   uuidr   
sqlalchemyr   sqlalchemy.ormr	   r
   r   r   sqlalchemy.utilr   sqlalchemy.orm.baser   r   rG   r   r   <module>rO      s8    #  %   P P "2^+? ^+r   