
    Yj                        d Z ddlmZmZmZ ddlmZmZmZm	Z	  G d d      Z
edk(  rddlm	Z	  e	d	d
      Zg dZe
j                  ee      Z ede         ede
j!                  ee              g dZe
j                  ee      Z ede         ede
j!                  ee              g dZe
j                  ee      Z ede         ede
j!                  ee              yy)z
Split engine for TripTalley expenses.
Handles equal, custom, and percentage splits with largest-remainder rounding.
All money amounts are in integer minor units.
    )DictListTuple   )split_equalsplit_percentagesplit_customMoneyc            	           e Zd ZdZededee   deeef   fd       Z	ededee
eef      deeef   fd       Zededee
eef      deeef   fd	       Zeded
eeef   defd       Zy)SplitEnginezW
    Performs expense splits using various strategies with deterministic rounding.
    totalparticipant_idsreturnc           	          |si S t        | j                  |      }|j                         D ci c]  \  }}|t        || j                         c}}S c c}}w )a+  
        Split an expense equally among participants.
        
        Args:
            total: The total expense amount
            participant_ids: List of user IDs who are sharing the expense
            
        Returns:
            Dict mapping user_id to their share as a Money amount
        )r   amount_minoritemsr
   currency)r   r   shares_minoruser_idamounts        2/srv/projects/triptalley/backend/app/core/split.pyr   zSplitEngine.split_equal   s[     I"5#5#5G'3'9'9';=#GV vu~~66 = 	= =   !Aparticipant_weightsc           	          |si S t        | j                  |      }|j                         D ci c]  \  }}|t        || j                         c}}S c c}}w )a  
        Split an expense by percentage weights.
        
        Args:
            total: The total expense amount
            participant_weights: List of (user_id, weight_in_basis_points)
                                Weights must sum to 10000
        
        Returns:
            Dict mapping user_id to their share as a Money amount
            
        Raises:
            ValueError: If weights don't sum to 10000
        )r   r   r   r
   r   )r   r   r   r   r   s        r   r   zSplitEngine.split_percentage#   s\    " #I'(:(:<OP'3'9'9';=#GV vu~~66 = 	= =r   participant_amountsc           	          |si S t        | j                  |      }|j                         D ci c]  \  }}|t        || j                         c}}S c c}}w )a  
        Custom split where each participant specifies their exact share.
        
        Args:
            total: The total expense amount
            participant_amounts: List of (user_id, exact_minor_amount)
                                Amounts must sum to total.amount_minor
        
        Returns:
            Dict mapping user_id to their share as a Money amount
            
        Raises:
            ValueError: If amounts don't sum to total
        )r	   r   r   r
   r   )r   r   r   r   r   s        r   r	   zSplitEngine.split_custom;   s\    " #I#E$6$68KL'3'9'9';=#GV vu~~66 = 	= =r   sharesc                     t        |j                         t        d| j                              }|j                  | j                  k(  S )zJ
        Validate that a set of shares sums exactly to the total.
        r   )sumvaluesr
   r   r   )r   r   total_sharess      r   validate_splitzSplitEngine.validate_splitS   s8     6==?E!U^^,DE((E,>,>>>    N)__name__
__module____qualname____doc__staticmethodr
   r   intr   r   r   r   r	   boolr"    r#   r   r   r      s     =5 =49 =c5jAQ = =$ = =-1%S/-B=GKCQVJGW= =. =E =)-eCHo)>=CGU
CS= =. ?e ?"3:.?37? ?r#   r   __main__)r
   i  USD)r         zEqual split of $10.00: zSum validation: ))r   i  )r.   i  )r/   i  zPercentage split: ))r   i  )r.   ,  )r/   r0   zCustom split: N)r'   typingr   r   r   moneyr   r   r	   r
   r   r$   expenseparticipantsr   printr"   weights
shares_pctcustomshares_customr+   r#   r   <module>r:      s   % $ E EO? O?f z D% GL$$Wl;F	#F8
,-	[77HI
JK 0G--gw?J	zl
+,	[77LM
NO ,F,,Wf=M	N=/
*+	[77OP
QR) r#   