
    jc'                       d 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 ddlmZmZ ddlmZmZ dd	lmZmZmZmZmZ dd
lmZmZ ddlmZm Z m!Z!  G d de      Z" G d de"      Z# G d de"      Z$ G d de"      Z%d"dZ&d#dZ'd$dZ(d%dZ)d%dZ*d&dZ+d'dZ,d(dZ-d)dZ.d)dZ/	 	 	 	 	 	 	 	 d*dZ0d+dZ1	 	 	 	 	 	 d,d Z2y!)-zGDeterministic clarification and safety policy for nutrition onboarding.    )annotations)Mapping)deepcopy)date)DecimalInvalidOperation)Literalcast)	BaseModel
ConfigDict)POLICY_DIGESTPOLICY_VERSIONQUESTION_COPYSENSITIVE_FIELDSVISIBLE_ISSUE_LIMIT)QUESTION_FIELDScanonical_digest)build_onboarding_baselinecanonical_equation_sex_basisderive_adult_agec                       e Zd Z edd      Zy)_PolicyModelforbidT)extrafrozenN)__name__
__module____qualname__r   model_config     /home/cube/projects/richard/traning coach/task-12-independent-verification-r9/build/hermes-full/dualcoach/profile/checkin_cli/nutrition_onboarding_clarification_policy.pyr   r      s    HT:Lr!   r   c                  6    e Zd ZU ded<   ded<   ded<   ded<   y)ClarificationIssuestrissue_idfieldreason_codequestion_koNr   r   r   __annotations__r    r!   r"   r$   r$      s    MJr!   r$   c                  "    e Zd ZU ded<   ded<   y)
SafetyHoldr%   r'   z3Literal['sensitive_input_requires_clinical_review']r(   Nr*   r    r!   r"   r-   r-   &   s    JDDr!   r-   c                  h    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   ded<   ded<   ded<   y)ClarificationPolicyResultr%   policy_versionpolicy_digestr   reference_datedict[str, object]answersanswers_digestztuple[ClarificationIssue, ...]issuesztuple[str, ...]visible_issue_idsztuple[SafetyHold, ...]holdsresult_digestNr*   r    r!   r"   r/   r/   +   s8    **&&!!r!   r/   c                    t        | t              s t        | t        t        t        t
        f      sy t        |       }||j                         sy t        |j                         d      }d|v r |j                  d      j                  d      S |S )Nf.0)
isinstanceboolr%   intfloatr   _decimal	is_finiteformat	normalizerstrip)valueparsedrendereds      r"   _canonical_decimalrJ   7   sw    %jc5'8R&Se_F~V--/f&&(#.H/2h8??3&&s+LHLr!   c                j    | dk(  rt        |      xs |S | dv r|t        |      xs |S t        |      S )zBCanonicalize one boundary value without accepting an invalid type.equation_sex_basis>   	height_cm	weight_kgtarget_weight_kg)r   rJ   r   r'   rG   s     r"   canonicalize_answerrQ   A   sC    $$+E2;e;>>5CT!%(1E1E?r!   c           	     X    t         D ci c]  }|| v r|t        || |          c}S c c}w N)r   rQ   )r4   r'   s     r"   _canonical_answersrT   J   sB     %$EG 	"5'%.99$  s   'c                     t         t              rt         fddD              S t         t              r t        d  j	                         D              S t         t
        t        f      rt        d  D              S y)Nc              3  &   K   | ]  }|v  
 y wrS   r    ).0markerrG   s     r"   	<genexpr>z _is_ambiguous.<locals>.<genexpr>T   s     J1Iv6U?1Is   )u    또는 u    혹은 c              3  2   K   | ]  }t        |        y wrS   _is_ambiguousrW   items     r"   rY   z _is_ambiguous.<locals>.<genexpr>V   s     B>4=&>   c              3  2   K   | ]  }t        |        y wrS   r[   r]   s     r"   rY   z _is_ambiguous.<locals>.<genexpr>X   s     954=&5r_   F)r>   r%   anyr   valueslisttuplerG   s   `r"   r\   r\   R   sb    %J1IJJJ%!B5<<>BBB%$'95999r!   c                   t        | t              rt        |       ddhk7  ryt        t        t        t
        f   |       }|j                  d      }|j                  d      }t        |t              rt        d |D              ryt        t        t           |      }|dk(  xr | xs& |dk(  xr t        |      xr t        d |D              S )NstatusitemsFc              3  >   K   | ]  }t        |t                 y wrS   )r>   r%   r]   s     r"   rY   z$_valid_structured.<locals>.<genexpr>b   s     )VPUjs.C*CPUs   noneprovidedc              3  <   K   | ]  }|j                           y wrS   )stripr]   s     r"   rY   z$_valid_structured.<locals>.<genexpr>h   s     5

s   )r>   dictsetr
   r%   objectgetrc   ra   r?   all)rG   
structuredrg   rh   typed_itemss        r"   _valid_structuredru   \   s    eT"c%jXw4G&Gd3;'/J^^H%FNN7#EeT"c)VPU)V&VtCy%(Kf0[ * 	6	6555r!   c                    | dv r<t        |      xr/ t        t        t        t        f   |      j                  d      dk(  S |du xs |d u xr | dv S )N>   
conditionsmedicationsrg   rk   T>   eating_disorder_riskpregnancy_breastfeeding)ru   r
   rn   r%   rp   rq   rP   s     r"   
_sensitiver{   l   sj    -- ' &Dfu-

#h-:-& 	& TM*Ud]  : 1 r!   c                    	 | j                  | j                  dz
        j                         S # t        $ r1 | j                  | j                  dz
  d      j                         cY S w xY w)N   )year   )r~   day)replacer~   	isoformat
ValueError)r2   s    r"   _safe_adult_dobr   w   sk    Y%%>+>+>+C%DNNPP Y%%>+>+>+C%LVVXXYs   ,/ 7A)(A)c                `    	 t        t        |             S # t        t        t        f$ r Y y w xY wrS   )r   r%   r   	TypeErrorr   re   s    r"   rB   rB   ~   s/    s5z""i4 s    --c                0   h d}|D ](  }|| v st        | |         r|j                  |d       * dD ]4  }|| v s| |   t        | |         t        us#|j                  |d       6 d| v r(t        | d         t        ur|j                  dd       y y y )N>   	allergiesrw   rx   intolerancesdisliked_foodsdietary_preferencesreligious_ethical_exclusionsinvalid_value)rz   ry   
meal_count)ru   
setdefaulttyper?   r@   )r4   reasonsrs   r'   s       r"   _type_reasonsr      s    J G$5gen$Euo6  EG :tGEN?S[_?_uo6 E w4(=#>c#I<9 $Jr!   c                   | j                  d      }| j                  d      }| j                  d      }|dk(  r)||j                  dd       ||j                  dd       y |dvry ||j                  dd       ||j                  dd       t        | j                  d            }t        |      }||y |d	k(  r||k\  r|j                  dd
       |dk(  r||k  r|j                  dd
       y y y )N	goal_typerO   target_datemaintaintarget_not_allowed>   gainlosstarget_requiredrN   r   target_direction_invalidr   )rq   r   rB   )r4   r   goaltarget_weightr   currenttargets          r"   _cross_field_reasonsr      s   ;;{#DKK 23M++m,Kz$13GH"}.BC##-/@A=*;<w{{;/0Gm$F&.v~&G+-/IJv~&G+-/IJ ,~r!   c                T   t        |       }|j                  d      }	 t        |t              st	        d      t        j                  |      }t        ||       |j                  ddddd	
       	 t        ||       y # t        t        f$ r# |j                  dd       t        |      |d<   Y Vw xY w# t        $ ri} t        |dd              }|D ]G  }|j                  dd      }	|	st        |	d         }
|
dk(  rd}
|
t        v s6|j                  |
d       I Y d }~y d }~ww xY w)Ndate_of_birthzdate_of_birth is required)as_ofinvalid_date_of_birthz1.0policy_validation	completedpendingunpublished)schema_versioncustomer_keysession_statusreview_decisionpublication_statuserrorsc                      y)Nr    r    r    r!   r"   <lambda>z#_baseline_reasons.<locals>.<lambda>   s    r!   locr    r   	adult_ager   )rn   rq   r>   r%   r   r   fromisoformatr   r   r   r   updater   getattrr   )r4   r   r2   payloadraw_dob
parsed_dobexcr   errorlocationr'   s              r"   _baseline_reasonsr      s1   
 7mGkk/*GC'3'899''0
>: NN("!(  ;!'@ z" C?,CD#2>#B C  
;3h
35Eyy+H$E#''""5/: 
;s0   =B  2B5  /B21B25	D'>A
D"	D""D'c                n    t        t        t        t        | |t        |      d      | |t        |          S )N)r0   r1   r'   r(   value_digest)r&   r'   r(   r)   )r$   r   r   r   _QUESTION_COPY)r'   r(   rG   s      r"   _issuer      s@    !"0!.* 0 7
 "5) r!   c                 
 t        |       
t        
      }i t        D ]&  }|
vrd|<   t        
|         s|dv rdnd|<   ( t	        
       t        
       t        
|       t        
fdt        D              }|D ]  }j                  |j                  d         t        
fdt        D              }t        d |dt         D              }t        t        |j                         ||D cg c]  }|j                  d	
       c}||D cg c]  }|j                  d	
       c}d}	t!        t        t        |
||||t        |	      	      S c c}w c c}w )zHCompile every ordered issue and hold against an explicit reference date.missing_value>   rM   rN   rO   multiple_measurementsmultiple_valuesc              3  ^   K   | ]$  }|v rt        ||         rt        |d        & yw)(sensitive_input_requires_clinical_review)r'   r(   N)r{   r-   )rW   r'   	canonicals     r"   rY   z/compile_clarification_policy.<locals>.<genexpr>   s:      &EI*UIe4D"E 	,VWW&s   *-Nc              3  d   K   | ]'  }|v r!t        ||   j                  |             ) y wrS   )r   rq   )rW   r'   r   r   s     r"   rY   z/compile_clarification_policy.<locals>.<genexpr>  s7      $EG 	ugenimmE&:;$s   -0c              3  4   K   | ]  }|j                     y wrS   )r&   )rW   issues     r"   rY   z/compile_clarification_policy.<locals>.<genexpr>  s     X:Wenn:Ws   json)mode)r0   r1   r2   r5   r6   r7   r8   )	r0   r1   r2   r4   r5   r6   r7   r8   r9   )rT   r   r   r\   r   r   r   rd   _SENSITIVE_FIELDSpopr'   _VISIBLE_ISSUE_LIMITr   r   r   
model_dumpr/   )r4   r2   r5   r'   r8   holdr6   r7   r   result_payloadr   r   s             @@r"   compile_clarification_policyr      s    #7+I%i0N G 	!,GEN9U+, JJ (& EN	 ! )W%G,i.9 & E
 DJJ%  $ F
 X&AVBV:WXX(&(224(>DEfU5###0fE.;@A54$//v/.5AN %%#%%+&~6
 
	 FAs   :EE"N)rG   rp   returnz
str | None)r'   r%   rG   rp   r   rp   )r4   Mapping[str, object]r   r3   )rG   rp   r   r?   )r'   r%   rG   rp   r   r?   )r2   r   r   r%   )rG   rp   r   zDecimal | None)r4   r   r   dict[str, str]r   None)r4   r   r   r   r2   r   r   r   )r'   r%   r(   r%   rG   rp   r   r$   )r4   r   r2   r   r   r/   )3__doc__
__future__r   collections.abcr   copyr   datetimer   decimalr   r   typingr	   r
   pydanticr   r   4checkin_cli.nutrition_onboarding_clarification_rulesr   r   r   r   r   r   r   r   )checkin_cli.nutrition_onboarding_contractr   r   'checkin_cli.nutrition_onboarding_modelsr   r   r   r   r$   r-   r/   rJ   rQ   rT   r\   ru   r{   r   rB   r   r   r   r   r   r    r!   r"   <module>r      s    M " #   -   *  X ;9 ; E E
	 	M Y:(K4";!";"; "; 
	";J"6!6 6 	6r!   