
    jm                       d Z ddlmZ ddlmZ ddlmZ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 ddlmZmZmZmZmZ   edd       G d d             Z!	 	 	 	 	 	 	 	 	 	 d<dZ edd       G d d             Z"eZ#eZ$e"Z%e"Z&e"Z'e"Z(dddd	 	 	 	 	 	 	 	 	 	 	 	 	 d=dZ)d>dZ*d?dZ+d@dZ,dAdBdZ-dCdZ.	 	 	 	 	 	 	 	 	 	 	 	 dDdZ/dEdZ0	 	 	 	 	 	 	 	 	 	 	 	 	 	 dFdZ1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dGdZ2dHd Z3	 dIdddd	 	 	 	 	 	 	 	 	 	 	 	 	 dJd!Z4	 dIdddd	 	 	 	 	 	 	 	 	 	 	 	 	 dJd"Z5	 	 dKdd#	 	 	 	 	 	 	 	 	 dLd$Z6	 	 dKdd#	 	 	 	 	 	 	 	 	 dLd%Z7	 	 dKdd#	 	 	 	 	 	 	 	 	 dLd&Z8	 	 dKdd#	 	 	 	 	 	 	 	 	 dLd'Z9	 	 	 	 	 	 	 	 dMd(Z:dNd)Z;dOd*Z<dPd+Z=dPd,Z>dQd-Z?dRd.Z@dSd/ZAdTd0ZBdUd1ZCdVd2ZDdVd3ZE	 	 	 	 	 	 	 	 dWd4ZF	 	 	 	 	 	 dXd5ZGdYd6ZHdZd7ZId[d8ZJd\d9ZKd]d:ZLd^d;ZMe4ZNe6ZOe6ZPe6ZQe6ZRy)_z/Deterministic customer-scoped period reporting.    )annotations)	dataclass)date	timedelta)Path)Counter)fmean)Any)Mapping)CustomerProfilePlanWeekTwelveWeekPlan)ContractStatusEvent	EventType)CustomerWeeklyReviewSourceWeeklyActionOutcomeWeeklyReviewRequestWeeklySummary#build_customer_weekly_review_sourceT)frozenslotsc                     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<   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<   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#)$CustomerPeriodReportr   	starts_onends_onintsample_countfloat | Noneaverage_weight_kg
int | Noneaverage_calories_kcalaverage_protein_gaverage_carbohydrate_gaverage_fat_gaverage_water_litersaverage_sleep_hoursaverage_sleep_quality_1to5average_readiness_1to5weight_change_kgstrweight_trend calorie_target_adherence_percent protein_target_adherence_percentcompletion_percentmissing_day_countmissing_weight_countmissing_calories_count
str | Nonelatest_meal_summarylatest_digestion_summarylatest_appetite_stress_summarytuple[str, ...]repeated_digestion_signals	well_doneimprovement_prioritiesnext_actionsN)__name__
__module____qualname____annotations__     o/home/cube/projects/richard/.worktrees/nutricoach-v140-impl/dualcoach/profile/checkin_cli/customer_reporting.pyr   r      s    OM##%%!!&&&&%% ,,((""&00&00##(($.. //++!!rA   r   c               0    t        t        | |||            S )z@Compatibility boundary for the established weekly reporting API.)$_build_customer_weekly_review_sourcer   )summarycustomer_keylatest_actionnext_review_dates       rB   r   r   7   s      0G\=BRS rA   c                     e Zd ZU dZ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<   d
ed<   d
ed<   ded<   d
ed<   ded<   ed d       Zed d       Zed!d       Zed"d       Z	ed d       Z
ed#d       Zed d       Zy)$PilotKPIJudgementzCOne deterministic conjunction projected from one customer's events.r   r   r   tuple[date, ...]eligible_weekdaysaccepted_checkin_datesfloatcheckin_rate_percentboolcheckin_passr   satisfaction_scoresatisfaction_passtuple[int, ...]weekly_operator_minutesoperator_time_passrenewal_validr3   renewal_event_idpassedr7   failure_reasonsc                    | j                   S NrY   selfs    rB   all_passzPilotKPIJudgement.all_passV       {{rA   c                    | j                   S r\   r]   r^   s    rB   kpi_passzPilotKPIJudgement.kpi_passZ   ra   rA   c                    | j                   S r\   )rO   r^   s    rB   checkin_ratezPilotKPIJudgement.checkin_rate^   s    (((rA   c                    | j                   S r\   )rR   r^   s    rB   satisfactionzPilotKPIJudgement.satisfactionb   s    &&&rA   c                    | j                   S r\   )rS   r^   s    rB   satisfaction_passedz%PilotKPIJudgement.satisfaction_passedf   s    %%%rA   c                    | j                   S r\   )rU   r^   s    rB   operator_minutes_by_weekz*PilotKPIJudgement.operator_minutes_by_weekj   s    +++rA   c                    | j                   S r\   )rW   r^   s    rB   renewal_passzPilotKPIJudgement.renewal_passn   s    !!!rA   N)returnrP   )rn   rN   )rn   r   )rn   rT   )r<   r=   r>   __doc__r?   propertyr`   rc   re   rg   ri   rk   rm   r@   rA   rB   rJ   rJ   C   s    MOM'',,$$,,  L$$    ) ) ' ' & & , , " "rA   rJ   Ntargetsplanprofilec                  t        |       }|D ch c]  }|j                  |j                   c}t        t        fd|D        d             }t	        |d      }	t	        |d      }
t	        |d      }t	        |d      }t	        |d	      }t	        |d
      }t	        |d      }t	        |d      }t	        |d      }z
  j
                  dz   }t        |d||d      }t        |d
||d      }t        |d      }t        d t        |      j                         D              }t        |d      }t        |d      }t        t        |      |||||      }t        t        |      |||||||      }t        |	      dk\  r't        t        |	d         t        |	d         z
  d      nd}t!        d5i dddt        |      dt        |	      dt#        |
      dt#        |      dt#        |      dt#        |      dt        |      d |d!t        |d      d"|d#|d$t%        |      d%|d&|d't        t        |      |z  d(z        d)t'        d|t        |      z
        d*t        |      t        |	      z
  d+t        |      t        |
      z
  d,t)        |d-      d.t)        |d      d/t)        |d0      d1|d2|d3|d4t+        |      S c c}w )6zEProject one customer's immutable events over an inclusive date range.Nc              3  `  K   | ]  }|j                   t        j                  u r|j                  t        j
                  t        j                  t        j                  fv rL|j                  vr>|j                  2t        j                  |j                  d d       cxk  rk  rn n|  y wN
   )statusr   ACCEPTED
event_typer   MORNING_CHECKINNUTRITION_CHECKIN
CORRECTIONevent_idcheck_inr   fromisoformatoccurred_at_kst).0eventr   r   
supersededs     rB   	<genexpr>z/build_customer_period_report.<locals>.<genexpr>   s      E<<>222!:!:I<W<WYbYmYm nnNN*,NN&++E,A,A#2,FGR7R 	s   B+B.c                    | j                   S r\   )r   )r   s    rB   <lambda>z.build_customer_period_report.<locals>.<lambda>   s
    ..rA   )keybody_weight_kgcalories_kcalsleep_hourssleep_quality_1to5readiness_1to5	protein_gcarbohydrate_gfat_gwater_liters   T)	symmetricFdigestion_summaryc              3  T   K   | ]   \  }}|d k\  r|j                         dvr| " yw)   >      정상nonenormalN)casefold)r   valuecounts      rB   r   z/build_customer_period_report.<locals>.<genexpr>   s2      "C,%A:%..*2NN 	"Cs   &(r   r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   r*   r,   r-   r.   r/   d   r0   r1   r2   r4   meal_summaryr5   r6   appetite_stress_summaryr8   r9   r:   r;   r@   )_read_events
supersedestuplesorted_valuesdays_period_adherence_textsr   items_average
_well_donelen_improvementsroundrN   r   _average_int_weight_trendmax_latest_text_next_actions)events_pathr   r   rr   rs   rt   eventsr   eligibleweightscaloriessleepssleep_qualities	readinessproteinscarbohydratesfatswaters
total_dayscalorie_adherenceprotein_adherencedigestion_valuesrepeated_digestionaverage_sleepaverage_readyr9   improvementsweight_changer   s    ``                         @rB   build_customer_period_reportr   |   sw    +&F06Wu%:J:J:V%""WJV  /0 1H h 01Gx1HX}-Fh(<=O"23Ix-HH&67M8W%DX~.FI%++a/J)/7DD *+w h(;< ")*:";"A"A"C  VQ'MY*M3x=*6GIZ\ikrsI Hz#46G}&8'L ILGXYHYE%,uWQZ/@@!D_cM  ] #7+	
 +84 'x0  ,M: #4( &f- * $,OQ#?  - ' #=1 *;  *;!" !X!;c!AB#$ ac(m!;<%& !]S\9'(  #8}s8}<)* )>B+, ".h8K!L-. (4H>W'X/0 $612 34  ,56 #<07 O Xs
   KKc                >    fd| D        }t        d |D              S )Nc              3  f   K   | ](  }|j                   t        |j                   d        * y wr\   r   getattrr   r   fields     rB   r   z_values.<locals>.<genexpr>   (     eu%..JdgenneT2   11c              3  N   K   | ]  }t        |t        t        f      s|  y wr\   )
isinstancerN   r   r   r   s     rB   r   z_values.<locals>.<genexpr>   s     NF5j.MFs   %%r   r   r   valuess    ` rB   r   r      s    eeFNFNNNrA   c                >    fd| D        }t        d |D              S )Nc              3  f   K   | ](  }|j                   t        |j                   d        * y wr\   r   r   s     rB   r   z_texts.<locals>.<genexpr>   r   r   c              3  d   K   | ](  }t        |t              s|j                         s%| * y wr\   r   r+   stripr   s     rB   r   z_texts.<locals>.<genexpr>   s"     WF5j.DF   000r   r   s    ` rB   r   r      s    eeFWFWWWrA   c                ,    t        | |      }|r|d   S d S )Nr   )r   r   s      rB   r   r      s    FE"F6":)T)rA   c                2    | sy t        t        |       |      S r\   r   r	   )r   digitss     rB   r   r      s    v''rA   c                2    | rt        t        |             S d S r\   r   r   s    rB   r   r      s    #)5v3t3rA   c               @   g }| D ]  }|j                   t        |j                   |d       nd }t        j                  |j                  d d       }|#t        d||j                  z
  j                        nd}	||j                  t        d|	dz           n|}
|
t        |
|d       nd }t        |t        t        f      rt        |t              s|j                  |rt        ||z
        |dz  k  n||k\          |sy t        t!        |      t#        |      z  dz        S )Nrx   r         g?r   )r   r   r   r   r   r   r   r   weeksminr   rN   r   appendabsr   sumr   )r   r   rr   rs   r   comparisonsr   r   dayelapsedtarget_weektargets               rB   r   r      s    !K8=8Rt4X\  !6!6s!;<9=9I#a#.445q;?;KdjjRA!67QX6A6MeT2SW%%.j6M)3uv~.&3,>QVZ`Q`a  [!C$44s:;;rA   c                8    | yt        |       dk  ry| dkD  rdS dS )Ninsufficient_datag333333?stabler   
increasing
decreasing)r   )changes    rB   r   r      s*    ~"
6{S!A:<7<7rA   c                   g }| |z  dk\  r|j                  d       ||dk\  r|j                  d       ||dk\  r|j                  d       |r.|j                  "| ||j                  k\  r|j                  d       t        |      S )N皙?u   체크인 지속성 80% 이상P   u"   칼로리 목표 범위 80% 이상u"   단백질 목표 달성 80% 이상u   수면 목표 달성r   sleep_goal_hoursr   )samplesr   r   proteinsleeprt   r   s          rB   r   r     s     E~56B9:w"}9:7++7E<MRW[b[s[sRs+,<rA   c                d   g }| |z  dk  r|j                  d       ||dk  r|j                  d       ||dk  r|j                  d       |r.|j                  "| ||j                  k  r|j                  d       ||dk  r|j                  d       |r|j                  d	       t        |      S )
Nr      체크인 누락 줄이기r   !   칼로리 목표 범위 일관성   단백질 목표 달성률   수면 목표 시간 확보   (   낮은 컨디션과 회복 원인 확인,   반복 소화 불편과 식사 구성 대조r   )	r   r   r   r   r   r   	digestionrt   r   s	            rB   r   r     s    
 E~12289w|127++7E<MRWZaZrZrRr23Q?@CD<rA   c                >    dddddddt        fd| D              S )	NuF   다음 주에는 체크인 시간을 한 시각으로 고정합니다.uS   외식 전후 식사를 계획해 목표 칼로리 ±10% 범위를 확인합니다.u<   각 식사에 단백질 공급원을 먼저 배치합니다.uc   취침 시작 시각을 30분 앞당기고 다음 체크인에서 수면 시간을 확인합니다.uZ   훈련량·수면·식사 누락을 함께 기록해 회복 저하 원인을 좁힙니다.uP   불편이 있었던 식사와 양을 기록해 반복 패턴을 확인합니다.)r  r  r  r  r  r  c              3  (   K   | ]	  }|     y wr\   r@   )r   itemactionss     rB   r   z _next_actions.<locals>.<genexpr>/  s     8<4<s   r   )r   r  s    @rB   r   r   &  s;    &n .C&d (M 5Q 9KG 8<888rA   c               T   ||t        d      z   }||k  rt        d      t        t        |             }t	        ||      t        fdt        |||      D              }t        | |||||      }t        t        |      t                    }	t        g |j                  |	dk\  rdnd      }
t        |j                        }|sd	|vrg |d	}t        |      }t        ||j                        }t!        ||||	||
|||j"                  |j$                  |j&                  
      S )zBBuild the bounded owner-facing summary for one inclusive KST week.   r   z)weekly summary end must not precede startc              3  <   K   | ]  }|t              v r|  y wr\   setr   r   rL   s     rB   r   z'build_weekly_summary.<locals>.<genexpr>B  s'      J#'(( 	J   rq   g      T@)u   평일 체크인 80% 이상r@   r  )r   r   rL   checkin_datesrO   trendskeep_behaviorschange_behaviorsnext_decisionr    r*   r,   )r   
ValueError_active_eventsr   _weekday_datesr   _accepted_checkin_datesr   _rate_percentr   _unique_stringsr9   r:   _summary_trends_summary_next_decisionr;   r   r    r*   r,   )r   r   r   rr   rs   rt   r   r  reportratekeepr   r  r  rL   s                 @rB   build_weekly_summaryr%  2  sS    iQ//DEEL56F&y': .vy'J M *F ]+S1B-CDDX&

X$$,>TVXD V::;F9G86878V$F*663F3FGM+#!# 2200(( rA   c               $    t        | |||||      S )z>Named customer-scoped alias for the weekly summary projection.rq   )r%  )r   r   r   rr   rs   rt   s         rB   build_customer_weekly_summaryr'  g  s#       rA   rs   c               H   t        |      \  t        t        |             }t              t	        fdt        |      D              }t        t        |      t                    }t              xr t        |      dz  t              dz  k\  }t	        fd|D              }|rt        t        |      d      nd}	|	duxr |	dk\  }
t        |      }t	        d |D              }t        d	 |D              }t        |      }|du}g }|s|j                  d
       |
s|j                  d       |s!|j                  d t!        |      D               |s|j                  d       t#        ||||	|
|||t%        |dd      | t	        |            S )a  Judge the four pilot KPIs as one exact conjunction.

    The check-in denominator is the weekday set in the fixed 28-day window.
    Operator minutes are projected into four individual seven-day buckets;
    superseded entries are excluded before totals are calculated.
    c              3  <   K   | ]  }|t              v r|  y wr\   r  r  s     rB   r   z#judge_pilot_kpis.<locals>.<genexpr>  s'      J#'(( 	Jr  r   r   c              3     K   | ]c  }t        |      d k(  rSt        |      dk(  rEt        |      x8cxk  rk  r-n n*t        t	        |dd            xdcxk  rdk  rn n e yw)satisfaction_recordacceptedNscore_1to10scorer   rx   )_event_type_value_event_status_value_event_date_number_payload_value)r   r   r   satisfaction_dayr/  r   s     rB   r   z#judge_pilot_kpis.<locals>.<genexpr>  sy      	 EU#'<<&*4!,U!33@)4W4nUM7KLLUY" 	s   A)A,r   Ng       @c              3  &   K   | ]	  \  }}|  y wr\   r@   r   minutes_s      rB   r   z#judge_pilot_kpis.<locals>.<genexpr>  s     D^zw7^   c              3  ,   K   | ]  \  }}|d k    yw)<   Nr@   r7  s      rB   r   z#judge_pilot_kpis.<locals>.<genexpr>  s       (JGQ 	2(s   re   rg   c              3  B   K   | ]  \  }\  }}|d kD  r
d|dz      yw)r<  operator_time_week_r   Nr@   )r   indexr8  r9  s       rB   r   z#judge_pilot_kpis.<locals>.<genexpr>  s3      
'@#|| "%!)-'@s   renewalr   )r   r   rL   rM   rO   rQ   rR   rS   rU   rV   rW   rX   rY   rZ   )_pilot_windowr  r   r  r   r  r  r   rP   r   r	   _operator_time_bucketsall_effective_exact_renewalr   extend	enumeraterJ   r   )r   r   r   rs   r   accepted_datesre   rQ   satisfaction_scoresrR   rS   weekly_bucketsweekly_minutesrV   renewal_eventrW   failuresrL   r5  r/  s    ``              @@@rB   judge_pilot_kpisrM  {  s    'y'4@IwL56F&y': .vy'J N !^!4c:K6LML)*gs>/BS/HCPaLbegLg/gL 	 	  	 BUu%891=Z^*$6T;MQT;T+FI>ND^DDN (  -VY?M!-MH'' 
'0'@
 	

 	"+-)!-+ .-# 
DA|h rA   c                    t        | |||      S )z:Explicit alias for the single-source pilot KPI projection.r(  rM  r   r   r   rs   s       rB   build_pilot_kpi_judgementrQ         KG$GGrA   c                    t        | |||      S )z=Customer-scoped alias retained for gateway/reporting callers.r(  rO  rP  s       rB   build_customer_kpi_reportrT    rR  rA   c                    t        | |||      S )Nr(  rO  rP  s       rB   evaluate_pilot_kpisrV    s     KG$GGrA   c                   |F| Dt        | t              s4t        | d|       }t        |d|      }t        |d      rt        |d      r|}|Ht	        t        |dd             }|t        d      |t        d      z   }|||k7  rt        d      ||fS t        | t              st        d	      | t        d      z   }|||k7  rt        d      | |fS )
Nspecrs   r   r   z!pilot plan must provide starts_on   r  z(pilot KPI window must be exactly 28 daysz1pilot KPI judgement requires a start date or plan)r   r   r   hasattr_as_dater  r   )r   r   rs   	candidatecandidate_plancanonical_startcanonical_ends          rB   rA  rA    s    
 |	-jD6QIvy9	 FI>>;/GNG4T!D"74d#CD"@AA')*<<7m#;GHH--i&LMM	r 22Mw-7CDDm##rA   c                ^     t         fdt        | z
  j                  dz         D              S )Nc              3  |   K   | ]3  }t        |       z   j                         dk  rt        |       z    5 yw)r     N)r   weekday)r   offsetr   s     rB   r   z!_weekday_dates.<locals>.<genexpr>  s>      ;F	v..779A= 	I6**;s   9<r   )r   ranger   )r   r   s   ` rB   r  r    s2     Wy066:;  rA   c                2    |dk  ryt        | |z  dz  d      S )Nr   g        r   r   )r   )	numeratordenominators     rB   r  r  
  s$    a[(3.22rA   c                H    t        | dd      }t        t        |d|            S )Nr{    r   r   r+   r   r   s     rB   r0  r0    s$    E<,Ewugu-..rA   c                H    t        | dd      }t        t        |d|            S )Nry   rj  r   rk  rl  s     rB   r1  r1    s$    E8R(Ewugu-..rA   c                    dD ]  }t        | |d       }||c S  t        |       }||j                  d      | dfD ]  }t        | |d       }||c S  y )N)payloadevent_payloadtyped_payload_record_payload)r   r0  removesuffix)r   namero  r{   s       rB   _event_payloadrv    sx    =%t,N > #5)J	*,h
 %t,N rA   c                    t        |       }|D ]Q  }|:t        ||d       }||c S t        |t              r|j	                  |      }||c S t        | |d       }|O|c S  y r\   )rv  r   r   r   get)r   namesro  ru  r   s        rB   r4  r4  +  sv    U#GGT40E '7+D)$ LtT*L  rA   c                    t        | t              r| S t        | t              r	 t        j                  | d d       S y # t        $ r Y y w xY wrw   )r   r   r+   r   r  r   s    rB   r[  r[  <  sP    %%	%%eCRj11   		s   = 	A	A	c           	     f    t        t        | dd             }||S t        t        | dddd            S )Nr   kst_datecheckin_datecollection_date	work_date)r[  r   r4  )r   occurreds     rB   r2  r2  G  s;    '8$?@HN5*nFWYdeffrA   c                f    t        | t              st        | t        t        f      sy t        |       S r\   )r   rP   r   rN   r{  s    rB   r3  r3  N  s&    %je&E<rA   c                    t               }g }| D ]H  }t        |dd      }t        |t              r|r||v r'|j	                  |       |j                  |       J t        |      S )zGCollapse duplicate append attempts without collapsing distinct entries.
dedupe_keyN)r  r   r   r+   addr   r   )r   	seen_keysselectedr   r   s        rB   _unique_event_streamr  T  sa    %IHe\40c3CiMM#  ?rA   c                    t        |       } | D ch c]  }t        |dd       x}t        |       c}| D ],  }t        |dd      }|j	                  t        |             . t        fd| D              S c c}w )Nr   supersedes_entry_idsupersedes_event_idc              3     K   | ]5  }t        t        |d d            vrt        t        |d            vr| 7 yw)r   rj  entry_idN)r+   r   r4  )r   r   superseded_refss     rB   r   z!_active_events.<locals>.<genexpr>m  sC      Ewuj"-.oEuj12/I 	s   ;>)r  r   r+   r4  r  r   )r   r   r   r  s      @rB   r  r  b  s    !&)F !EUL$77ED 	E
!O
 u&;=RSE
+     !s   !A:c                    h d}| D ch c]:  }t        |      dk(  r*t        |      |v rt        |      x}||cxk  r|k  rn n|< }}t        t	        |            S c c}w )N>   
correctionmorning_checkincustomer_checkinnutrition_checkincheck_in_validatedr-  )r1  r0  r2  r   r   )r   r   r   accepted_typesr   r   datess          rB   r  r  u  sx    
N Eu%3e$6&&S3'' 	 
  s   ?Ac                v   t        d      D cg c]  }ddg }}t               }|t        d      z   }| D ]  }t        |      dk7  st	        |      dk7  r t        |d      }|t        |      nt        t        |dd	            }||v rT|j                  |       t        t        |d
d            }	t        t        |d            }
|	|
||	cxk  r|k  sn |	|z
  j                  dz  }d|cxk  rdk  sn ||   dxx   t        |
      z  cc<   ||   dxx   dz  cc<    t        d |D              S c c}w )N   r   rY  r  r-  operator_time_recordr  r   rj  r  r}  r8  r   r   c              3  *   K   | ]  \  }}||f  y wr\   r@   )r   r8  entriess      rB   r   z)_operator_time_buckets.<locals>.<genexpr>  s     DG(8'7#Gs   )re  r  r   r1  r0  r4  r+   r   r  r[  r3  r   r   r   )r   r   r9  bucketsseen_entriesendr   r  r   work_dayr8  buckets               rB   rB  rB    sC     %Qx(x!1vxG( UL
iR(
(Cu%37H7OSi7i!%4'3c(mWUJXZ=[9\,N5+zJK.	:;wi86Rs6RY&,,1??FOA#g,.FOA!#   DGDDD' )s   D6c                :    t        d t        | |      D              S )Nc              3  &   K   | ]	  \  }}|  y wr\   r@   r7  s      rB   r   z,_operator_minutes_by_week.<locals>.<genexpr>  s     U+TZWa+Tr:  )r   rB  )r   r   s     rB   _operator_minutes_by_weekr    s    U+A&)+TUUUrA   c                   |t        d      z   }|t        d      z   }t        d | D              }t        |      dk7  ry |d   }t        t	        |d            }t	        |d      }t        t	        |d	d
            }t        t	        |dd            }	t        t	        |ddd            }
|dk7  s|dk7  s||k7  s|	|k7  s|
|
|kD  ry |S )N   r  7   c              3  v   K   | ]1  }t        |      d k(  r!t        |      dk(  rt        |d      dk(  r| 3 yw)r-  payment_recordkindr@  N)r1  r0  r4  )r   r   s     rB   r   z+_effective_exact_renewal.<locals>.<genexpr>  sB      Eu%3e$(885&)Y6	 	s   79r   r   
amount_krwmethodperiod_start_onperiod_startperiod_end_on
period_endpaid_on	paid_datepaid_atiI bank_transfer)r   r   r   r3  r4  r[  )r   r   expected_startexpected_endrenewalsr   amountr  r  r  r  s              rB   rD  rD    s    !33Nyb11L  H 8}QKE^E<89FE8,FN52C^TUL.NOJ~eYYOPG&_$>)%?\!LrA   c                F   g }| j                   -|j                  d| j                   d| j                   dd       | j                   |j                  d| j                  dd       | j                   |j                  d| j                  dd	       t        |      xs d
S )Nu   체중 z (z+.2fzkg)u   평균 수면 z.2fu   시간u   평균 컨디션 z/5)u   관찰 가능한 추세 없음)r*   r   r,   r'   r)   r   )r"  r  s     rB   r   r     s    F* 3 34Bv7N7Nt6TTWXY!!-v'A'A#&FfMN$$0)&*G*G)LBOP=???rA   c                0    | r
|rd|d    S | rd| d    S y)Nu   조정: r   uK   유지: 현재 행동을 유지하고 다음 주 추세를 확인합니다.r@   )r   r  s     rB   r!  r!    s/    ''!*&&&)%%XrA   c                L    t        t        j                  d | D                    S )Nc              3  d   K   | ](  }t        |t              s|j                         s%| * y wr\   r   r   s     rB   r   z"_unique_strings.<locals>.<genexpr>  s&     e&Juc<RW\WbWbWdu&r   )r   dictfromkeysr   s    rB   r  r    s    e&eeffrA   c                    | j                         syt        d | j                  d      j                         D              S )Nr@   c              3  J   K   | ]  }|rt        j                  |        y wr\   )r   model_validate_json)r   lines     rB   r   z_read_events.<locals>.<genexpr>  s&      AD 	!!$'As   !#zutf-8)encoding)existsr   	read_text
splitlines)paths    rB   r   r     s<    ;;= NNGN4??A  rA   )
rE   r   rF   r+   rG   zWeeklyActionOutcome | NonerH   r   rn   r   )r   r   r   r   r   r   rr   PlanWeek | Noners   TwelveWeekPlan | Nonert   CustomerProfile | Nonern   r   )r   tuple[Event, ...]r   r+   rn   tuple[float | int, ...])r   r  r   r+   rn   r7   )r   r  r   r+   rn   r3   )r   )r   r  r   r   rn   r   )r   r  rn   r!   )r   r  r   r+   rr   r  rs   r  r   rP   rn   r!   )r   r   rn   r+   )r   r   r   r   r   r!   r   r!   r   float | int | Nonert   r  rn   r7   )r   r   r   r   r   r!   r   r!   r   r  r   r  r  r7   rt   r  rn   r7   )r   r7   rn   r7   r\   )r   r   r   r   r   date | Nonerr   r  rs   r  rt   r  rn   r   )NN)
r   r   r   "date | TwelveWeekPlan | Any | Noner   r  rs   r  rn   rJ   )r   r  r   r  rs   r  rn   ztuple[date, date])r   r   r   r   rn   rK   )rg  r   rh  r   rn   rN   )r   r   rn   r+   )r   r   rn   r
   )r   r   ry  r+   rn   r
   )r   r
   rn   r  )r   r   rn   r  )r   r
   rn   r   )r   r  rn   r  )r   r  r   r   r   r   rn   rK   )r   r  r   r   rn   ztuple[tuple[int, int], ...])r   r  r   r   rn   rT   )r   r  r   r   rn   zEvent | None)r"  r   rn   r7   )r   r7   r  r7   rn   r+   )r   r7   rn   r7   )r  r   rn   r  )Sro   
__future__r   dataclassesr   datetimer   r   pathlibr   collectionsr   
statisticsr	   typingr
   collections.abcr   checkin_cli.customer_coachingr   r   r   checkin_cli.modelsr   r   r   .checkin_cli.weekly_operations_domain_reportingr   r   r   r   r   rD   r   rJ   CustomerWeeklySummaryCustomerWeeklyReviewKPIJudgementCustomerKPIReportPilotKPIReportCustomerKPIJudgementr   r   r   r   r   r   r   r   r   r   r   r%  r'  rM  rQ  rT  rV  rA  r  r  r0  r1  rv  r4  r[  r2  r3  r  r  r  rB  r  rD  r   r!  r  r   build_weekly_customer_summaryjudge_customer_kpisevaluate_customer_kpisbuild_customer_kpi_judgementbuild_pilot_kpi_reportr@   rA   rB   <module>r     s   5 " ! $     # S S ? ?  $d#" " $"<

 
 .	

 
  
 $d#," ," $,"` & 1  % "(   $"&&*NNN N
 N  N $N NbO
X
*
(4<<&)<4C<
</3< <&8'1<F(>  '1<F*<  *@ 	*	9  2
  $"&&*222 2
 2  2 $2 2p  
  $"&&* 
    $ , 59I
 #'II1I I
  I I\ 59H
 #'HH1H H
  H H 59H
 #'HH1H H
  H H 59H
 #'HH1H H
  H H$1$$  $ 	$63/
/
""g&      	 .EEE !E4V<@Yg !5 & ) / ) rA   