
    $i                    t    S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  \
" \5      r " S S	5      rg
)zAudit logging for data changes and corrections.

Provides the AuditLogger class for recording and querying data modifications
across the system, enabling traceability of data corrections.
    )annotations)Any)desc)Session)
get_logger)AuditLogc                      \ rS rSrSr\    S                 S	S jj5       r\        S
S j5       r\ S     SS jj5       rSr	g)AuditLogger   u  Logger for recording and querying data changes.

Usage::

    audit = AuditLogger()
    audit.log_change(
        session, table_name="starters", record_id="42",
        action="CORRECT", old_values={"placing": 3}, new_values={"placing": 2},
        changed_by="admin", change_reason="Manual correction from video review",
    )

All methods are safe to call — failures are logged but never raised,
ensuring audit logging never blocks the primary operation.
Nc           
     p   UR                  5       R                  5       nUS;  a  [        R                  SU5        g [	        U[        U5      UUUUUS9n	U R                  U	5        U R                  5         [        R                  SUUUS.S9  U	$ ! [         a    [        R                  SUUUS.S9   gf = f)	uw  Record a data change in the audit log.

Args:
    session: Database session.
    table_name: Name of the table that was changed.
    record_id: Primary key value of the changed record (stringified).
    action: Type of change — ``INSERT``, ``UPDATE``, ``DELETE``, or ``CORRECT``.
    old_values: Snapshot of values before the change (optional).
    new_values: Snapshot of values after the change (optional).
    changed_by: Identifier of the user/system that made the change (optional).
    change_reason: Human-readable reason for the change (optional).

Returns:
    The created AuditLog entry, or ``None`` if logging failed.
)INSERTUPDATEDELETECORRECTuH   Invalid audit action '%s' — must be INSERT, UPDATE, DELETE, or CORRECTN)
table_name	record_idaction
old_values
new_values
changed_bychange_reasonzAudit log entry created)r   r   r   extraz Failed to create audit log entry)upperstriploggerwarningr   straddflushdebug	Exception	exception)
sessionr   r   r   r   r   r   r   action_upperentrys
             @/root/tipsharks/tipsharks-elo-api/packages/core/storage/audit.py
log_changeAuditLogger.log_change$   s    4 ||~++-HHNNZ 	%i.#%%%+E KKMMOLL)",!**   L 		2",!**   		s   AB "B54B5c                \    U R                  [        5      R                  [        R                  U:H  [        R                  [        U5      :H  5      R                  [        [        R                  5      5      R                  5       $ ! [         a    [        R                  SXS.S9  / s $ f = f)zRetrieve all audit log entries for a specific record.

Args:
    session: Database session.
    table_name: Name of the table.
    record_id: Primary key value of the record (stringified).

Returns:
    List of AuditLog entries, newest first.
z$Failed to query audit log for record)r   r   r   )queryr   filterr   r   r   order_byr   
created_atallr"   r   r#   )r$   r   r   s      r'   get_changes_for_record"AuditLogger.get_changes_for_recordf   s     	h''':5&&#i.8 $x2234  	6%/H   I	s   BB "B+*B+c                    U R                  [        5      R                  [        [        R                  5      5      R                  U5      R                  5       $ ! [         a    [        R                  S5        / s $ f = f)zRetrieve the most recent audit log entries across all tables.

Args:
    session: Database session.
    limit: Maximum number of entries to return (default 100).

Returns:
    List of recent AuditLog entries, newest first.
z(Failed to query recent audit log entries)
r+   r   r-   r   r.   limitr/   r"   r   r#   )r$   r3   s     r'   get_recent_changesAuditLogger.get_recent_changes   sb    		h'$x2234u	  	GHI	s   AA !A>=A> )NNNN)r$   r   r   r   r   r   r   r   r   dict[str, Any] | Noner   r7   r   
str | Noner   r8   returnzAuditLog | None)r$   r   r   r   r   r   r9   list[AuditLog])d   )r$   r   r3   intr9   r:   )
__name__
__module____qualname____firstlineno____doc__staticmethodr(   r0   r4   __static_attributes__r6       r'   r
   r
      s      -1,0!%$(??? ? 	?
 *? *? ? "? 
? ?B   
	 @   
 rD   r
   N)rA   
__future__r   typingr   
sqlalchemyr   sqlalchemy.ormr   packages.core.common.loggingr   packages.core.storage.modelsr   r=   r   r
   r6   rD   r'   <module>rK      s4    #   " 3 1	H	J JrD   