
    soj1                     p   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	c m
c mZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ d dlmZmZmZm Z  d dl!m"Z"  G d de          Z# G d de          Z$dZ%	 	 d)dedej&        dej        dedededdfdZ'	 	 d*dedej&        dej        dedede(ddfdZ)	 	 d*dedej&        dej        dede(ddfdZ*dedej&        deddfd Z+dedej&        deddfd!Z,dede-fd"Z.de/e0e0f         fd#Z1d$e0de-fd%Z2dej        de-fd&Z3d'e-dedz  fd(Z4dS )+    )StrEnum)fn)Update)ContextTypesN)User)CommandName)MessageSource)ReservedKeyboardKeys)Screen)Keyboard)add_or_remove_bountyget_amount_from_stringvalidate_amount)"full_message_or_media_send_or_editget_message_sourceget_yes_no_keyboardmention_markdown_user)get_belly_formattedc                       e Zd ZdZdZdS )OwnerBountyReservedKeysz4
    Reserved keys for owner bounty management.
    aN)__name__
__module____qualname____doc__ACTION     3/app/src/chat/common/screens/screen_owner_bounty.pyr   r      s          FFFr   r   c                       e Zd ZdZdZdS )OwnerBountyActionz
    Owner bounty actions.
    rpallN)r   r   r   r   REVERT_PENDING_ALLr   r   r   r!   r!   $   s"          !r   r!   z-eupdatecontextcommanduserinbound_keyboardtarget_userreturnc                 f  K   |t          | ||           d{V  dS t          |           t          j        u }|j        t
          j        u r|rdS t          | ||           d{V  dS |j        t
          j        u rt          | ||||           d{V  dS t          | |||||           d{V  dS )aS  
    Manage owner-only bounty adjustment commands.
    :param update: The update
    :param context: The context
    :param command: The command
    :param user: The owner user
    :param inbound_keyboard: Inbound keyboard for confirmation callbacks
    :param target_user: Target user from replied message in groups
    :return: None
    N)manage_keyboardr   r	   GROUPnamer   REVERT_PENDING_BOUNTY_ALL$send_revert_pending_all_confirmationREVERT_PENDING_BOUNTYmanage_revert_pendingmanage_add_or_take)r$   r%   r&   r'   r(   r)   is_groups          r   manager5   3   s      & #fg/?@@@@@@@@@!&))]-@@H|{<<< 	F267DIIIIIIIII|{888#FGWk8TTTTTTTTT
VWgt[(
S
SSSSSSSSSSr   Fr4   c           	      r  K   |}t           |j        v}d |j        D             }|ra|t          |d| d           d{V  dS t          |          dk    r(t          |t	          |          | d           d{V  dS |d         }	nt          |          dk    rt          |d|            d{V  dS |d         }	t          |          d	k     rt          |d
|            d{V  dS t          |d                   }|t          |d
|            d{V  dS t          | |||	|d           d{V sdS t          |	|          }
t          j	        |j
                  }|j        t          j        u rt          ||
|| d||           d{V  |r1d                    t!          |
          t#          |                    }nd                    t!          |
          t#          |                    }n|j        |
k     rLd|_        |                                 d                    t#          |          t!          |
                    }nT|xj        |
z  c_        |                                 d                    t!          |
          t#          |                    }t          ||| |           d{V  dS )a=  
    Manage owner-only add/take bounty commands.
    :param update: The update
    :param context: The context
    :param command: The command
    :param user: The owner user
    :param target_user: Target user from replied message in groups
    :param is_group: Whether this is a group message
    :return: None
    c                 (    g | ]}|t           k    |S r   )EXEMPT_FLAG).0ps     r   
<listcomp>z&manage_add_or_take.<locals>.<listcomp>p   s"    DDD13C3C!3C3C3Cr   Nz<This command can only be used in reply to someone's message.Tr$   add_delete_buttonr   z!Please specify the bounty amount.r$      z=Please specify a target user by username or Telegram user ID.   F)r=   should_validate_user_has_amount)r%   r$   should_save
should_taxcheck_for_loanu   Added ฿{} Berries to {}.uN   Added ฿{} Berries \(exempt from income tax and bounty distributions\) to {}.uL   {} doesn't have ฿{} Berries, so their bounty has been set to ฿0 instead.u   Took ฿{} Berries from {}.)r8   
parametersr   lenget_missing_group_amount_textget_target_userr   r   r   	get_by_ididr.   r   
ADD_BOUNTYr   formatr   r   bountysave)r$   r%   r&   r'   r)   r4   r=   rC   rE   amount_textamounttexts               r   r3   r3   Z   s-     & !G$66JDDW/DDDJ .4N"&	          Fz??a4-g66"&	          F mz??a43         
 F mz??Q4O         
 F%jm444O         
 F +(-          	#K66F.00K|{---"!%
 
 
 	
 	
 	
 	
 	
 	
 	
  	/66#F++-B;-O-O DD fll#F++-B;-O-O DD &&!"K^ee)+668KF8S8S  D &(077#F++-B;-O-O D -+	           r   c                 j  K   |r|t          |d| d           d{V  dS nht          |j                  dk    rt          |d|            d{V  dS t          |j        d                   }|t          |d|            d{V  dS t	          |          }t          ||| |           d{V  dS )a  
    Manage single-user pending bounty revert.
    :param update: The update
    :param context: The context
    :param command: The command
    :param target_user: Target user from replied message in groups
    :param is_group: Whether this is a group message
    :return: None
    NzCPlease reply to the player whose pending bounty you want to revert.Tr<   r   z.Please specify a username or Telegram user ID.r>   )r   rF   rE   rH   revert_pending_bounty)r$   r%   r&   r)   r4   rQ   s         r   r2   r2      sx     "  4U"&	          F  w!""a''4@         
 F%g&8&;<<4@         
 F --D
,"	           r   c                    K   t          |t          j        t          j        t
          j        i          g}t          |d| |           d{V  dS )z
    Send pending bounty revert all confirmation.
    :param update: The update
    :param context: The context
    :param user: The owner
    :return: None
    )screen
extra_keyszDAre you sure you want to revert the pending bounty for every player?)r$   keyboardN)r   r   PVT_OWNER_BOUNTYr   r   r!   r#   r   )r$   r%   r'   rW   s       r   r0   r0     s       	*'.0A0T	
 	
 	
H -N	           r   c                   K   |                     t          j                  t          j        k    rdS |                    t          j                  st          |d|            d{V  dS t          |d|            d{V  t                      \  }}t          |d
                    ||          |            d{V  dS )z
    Manage owner bounty keyboard actions.
    :param update: The update
    :param context: The context
    :param inbound_keyboard: Inbound keyboard
    :return: None
    Nz Pending bounty revert cancelled.r>   z4Reverting pending bounty for all eligible players...u   Pending bounty revert completed successfully.

*Statistics*
• Players processed: *{}*
• Players skipped \(no pending bounty\): *{}*)getr   r   r!   r#   get_boolr
   CONFIRMr   revert_pending_bounty_allrL   )r$   r%   r(   	processedskippeds        r   r,   r,   *  s?      	4;<</	0 	0 	$$%9%ABB 0.
 
 
 	
 	
 	
 	
 	
 	
 	

 	
,>         
 344Iw
,	< =CF9g<V<V           r   c                 "   t          j        | j                  } | j        pd}|dk    r"d                    t          |                     S t           j        j                                        5  t          j	        t           j
        |z   d                              t           j        | j        k                                               ddd           n# 1 swxY w Y   d                    t          |           t          |                    S )z
    Revert pending bounty for one user without applying income tax.
    :param user: The user
    :return: Result message
    r   z-{} doesn't have any pending bounty to revert.rM   pending_bountyNzLSuccessfully reverted {}'s pending bounty of *{}* back to their main bounty.)r   rI   rJ   rb   rL   r   _metadatabaseatomicr$   rM   whereexecuteget_signed_belly_formatted)r'   rb   s     r   rS   rS   S  s+    >$'""D(-AN>EEF[\`FaFabbb			#	#	%	% . .;/	
 	
 	
 %47"
#
#GGIII	. . . . . . . . . . . . . . . Z``d##%?%O%O  s   .ACCCc                  |   d} t          j                                        t           j                            d          t           j        dk    z            }|                                D ]}|j        pd}t           j        j                                        5  t          j	        t           j
        |z   d                              t           j        |j        k                                               ddd           n# 1 swxY w Y   | dz  } t          j                                                    | z
  }| |fS )zu
    Revert pending bounty for every user without applying income tax.
    :return: Processed and skipped counts
    r   Fra   Nr@   )r   selectrf   rb   is_nulliteratorrc   rd   re   r$   rM   rJ   rg   count)r^   users_with_pendingr'   rb   r_   s        r   r]   r]   j  sS    I,,			$	$U	+	+0Cq0HI  #++--  ,1Z '')) 	2 	2K{^3    eDGtw&''					2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2
 	Q		kmm!!##i/Ggs   ADD	D	rP   c                 v    | dk     rdnd}d                     |t          t          |                               S )zd
    Format a signed bounty amount.
    :param amount: The amount
    :return: Formatted amount
    r   - u   {}฿{})rL   r   abs)rP   signs     r   rh   rh     s:     1**33"DD"5c&kk"B"BCCCr   c                 0    | j         t          j        u rdS dS )zv
    Get missing amount text for group commands.
    :param command: The command
    :return: Missing amount text
    z(Please specify the bounty amount to add.z+Please specify the bounty amount to remove.)r.   r   rK   )r&   s    r   rG   rG     s      |{---9988r   
identifierc                 x   |                                  } |                     d          r
| dd         } |                                 r&t          j        t          j        | k              }||S | dk    rdS t          j        t          j        t          j                  | 	                                k              S )z
    Resolve a user by Telegram user id or username.
    :param identifier: Telegram user id or username
    :return: User, if found
    @r@   Nrq   )
strip
startswith	isnumericr   get_or_none
tg_user_idr   LOWERtg_usernamelower)ru   r'   s     r   rH   rH     s     !!##JS!! $^
 : =>>KRtBHT%566*:J:J:L:LLMMMr   )NN)NF)5enumr   peeweer   src.tg_compatr   src.tg_compat.extr   src.model.enums.CommandmodelenumsCommandsrc.model.Userr   src.model.enums.CommandNamer   src.model.enums.MessageSourcer	   $src.model.enums.ReservedKeyboardKeysr
   src.model.enums.Screenr   src.model.pojo.Keyboardr   src.service.bounty_servicer   r   r   src.service.message_servicer   r   r   r   src.utils.string_utilsr   r   r!   r8   DEFAULT_TYPEr5   boolr3   r2   r0   r,   strrS   tupleintr]   rh   rG   rH   r   r   r   <module>r      s;                           * * * * * * ) ) ) ) ) ) ) ) ) ) ) )       3 3 3 3 3 3 7 7 7 7 7 7 E E E E E E ) ) ) ) ) ) , , , , , ,         
            7 6 6 6 6 6    g   ! ! ! ! ! ! ! !  "&$T $T$T&$T _$T 	$T
 $T $T 
$T $T $T $TX | ||&| _| 	|
 | | 
| | | |F 2 22&2 _2 	2
 2 
2 2 2 2j)6>B	   8&&)6&JR&	& & & &R     .5c?    0Ds Ds D D D D
97? 
9s 
9 
9 
9 
9N Nt N N N N N Nr   