
    jk                       d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	  G d de
      Z G d d	e      Z G d
 de      Z G d de	      Z edd       G d d             Z edd       G d d             Zdd	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 d dZ	 	 	 	 	 	 d!dZ	 	 	 	 	 	 d"dZ	 	 	 	 	 	 d#dZ	 	 	 	 	 	 	 	 d$dZg dZy)%zBTyped Telegram addresses for DualCoach customer and staff routing.    )annotations)IterableMapping)	dataclass)StrEnumc                      e Zd ZdZy)AddressConfigurationErrorz@Raised when persisted Telegram address configuration is invalid.N__name__
__module____qualname____doc__     m/home/cube/projects/richard/.worktrees/nutricoach-v140-impl/gateway/platforms/telegram_nutrition_addresses.pyr	   r	   
   s    Jr   r	   c                      e Zd ZdZy)#LegacySharedTopicConfigurationErrorz;Raised when legacy shared-topic routing requires migration.Nr
   r   r   r   r   r      s    Er   r   c                      e Zd ZdZy)AddressReadinessErrorzCRaised when valid addresses are not safe for production activation.Nr
   r   r   r   r   r      s    Mr   r   c                      e Zd ZdZdZdZy)StaffReviewRolez6Trusted authority roles for staff review destinations.owneroperatorN)r   r   r   r   OWNEROPERATORr   r   r   r   r      s    @EHr   r   T)frozenslotsc                  4    e Zd ZU dZded<   ded<   dZded<   y)CustomerPrivateDeliveryAddressz8Canonical private Telegram destination for one customer.intuser_idchat_idr   topic_idN)r   r   r   r   __annotations__r#   r   r   r   r   r      s    BLLHcr   r   c                  :    e Zd ZU dZded<   ded<   ded<   ded<   y)	StaffReviewAddressz6Typed Telegram destination for one trusted staff role.r   roler    r!   r"   r#   N)r   r   r   r   r$   r   r   r   r&   r&   &   s    @
LLMr   r&   N)minimumc               L   t        | t              rt        | d      t        | t              r| }n;t        | t              r| j                         r	 t        |       }nt        | d      |||k  rt        | d|       |S # t        $ r}t        | d      |d }~ww xY w)Nz must be an integer, not boolz must be an integerz must be >= )
isinstanceboolr	   r    strstrip
ValueError)valuefieldr(   parsedexcs        r   _parse_integerr3   0   s     %'5'1N(OPP%	E3	EKKM	TZF (5'1D(EFFv/'5'gY(GHHM  	T+ug5H,IJPSS	Ts   B 	B#BB#c               p    t        t        |       |z
        }|rt        ddj                  |             y )Nz'unexpected fields in Telegram address: z, )sortedsetr	   join)sourceallowed
unexpecteds      r   _reject_unexpected_fieldsr;   F   s@    
 Fg-.J'5dii
6K5LM
 	
 r   c                F   t        | t        h d             t        | j                  d      dd      }t        | j                  d      d      }|dk  rt	        d	      | j                  d
      }|dnt        |d
d      }|dk7  rt	        d      t        ||d      S )zFParse a production customer destination at the configuration boundary.   r"   r!   r#   r9   r!      r0   r(   r"   r0   r   zQcustomer delivery requires a private Telegram chat; configure the customer Bot DMr#   zNcustomer delivery requires canonical topic_id 0; configure the customer Bot DM)r!   r"   r#   )r;   	frozensetr3   getr	   r   )r8   r!   r"   	raw_topicr#   s        r   'parse_customer_private_delivery_addressrE   R   s    
 <= VZZ	2)QOGVZZ	2)DG!|'_
 	
 

:&I  	
IZC 
 1}'\
 	
 * r   c                T   t        | t              st        d      t        |t	        h d             t        |j                  d      dd      }t        |j                  d      d      }|d	k(  rt        d
      t        |j                  dd	      dd	      }t        | |||      S )zBParse a staff destination while preserving trusted role authority.z2staff address requires a trusted staff review roler=   r>   r!   r?   r@   r"   rA   r   z8staff review address requires a nonzero Telegram chat_idr#   )r'   r!   r"   r#   )r*   r   r	   r;   rB   r3   rC   r&   )r'   r8   r!   r"   r#   s        r   parse_staff_review_addressrG   s   s     dO,'@
 	
 <= VZZ	2)QOGVZZ	2)DG!|'F
 	
 

:q!H
 	 r   c                    t        | j                  d      d      }| j                  d      }|dnt        |dd      }|dk\  s|dk(  ry|D ]%  }t        |j                  d      d      }||k(  s% y y)zEReturn whether customer delivery shares a staff group through topics.r"   rA   r#   r   r@   FT)r3   rC   )customerstaff_addressescustomer_chat_idrD   customer_topic_idstaffstaff_chat_ids          r   $is_legacy_shared_topic_configurationrO      s     &hll9&=YOZ(I  	
IZC 
 1 1Q 6 &uyy';9M,, ! r   c                P    t        | |      rt        dj                  d            y)z6Fail readiness with the exact safe migration required. )z4legacy shared-topic routing is not production-ready:z4migrate customer delivery to the customer Bot DM and/remove the customer from the staff review groupN)rO   r   r7   )rI   rJ   s     r   (reject_legacy_shared_topic_configurationrS      s2     ,HoF1HH
 	
 Gr   c          	         |D ch c]  }t        |d       }}|D ]J  }|j                  |v st        dj                  d| j                   dd|j                   ddd	f             y
c c}w )zFReject activation while the customer remains in any staff review chat.customer_member_chat_idrA   rQ   z	customer z is present inzstaff review chat ;rR   zbefore production activationN)r3   r"   r   r7   r!   )rI   rJ   customer_member_chat_idsr"   member_chat_idsrM   s         r    reject_customer_staff_membershiprY      s     0/G 	w&?@/   !==O+'#H$4$4#5^D,U]]O1=I6		 	 !	s   A+)r	   r   r   r   r&   r   rO   rE   rG   rY   rS   )r/   objectr0   r,   r(   z
int | Nonereturnr    )r8   Mapping[str, object]r9   zfrozenset[str]r[   None)r8   r\   r[   r   )r'   rZ   r8   r\   r[   r&   )rI   r\   rJ   Iterable[Mapping[str, object]]r[   r+   )rI   r\   rJ   r^   r[   r]   )rI   r   rJ   zIterable[StaffReviewAddress]rW   zIterable[int]r[   r]   )r   
__future__r   collections.abcr   r   dataclassesr   enumr   r.   r	   r   r   r   r   r&   r3   r;   rE   rG   rO   rS   rY   __all__r   r   r   <module>rd      s   H " - ! K
 KF*C FN5 Ng  $d#  $ $d#  $ 	  	
 	,	
 	
 	
 
		
 #B
  B"3 
.
"
3
 

$,1 ,	
 
4r   