
    B(i2J                         S SK JrJr  S SKJr  S SKJr  S SKJr  SSK	J
r
JrJrJrJrJrJrJr  SSKJrJrJrJrJr  SSKJr  SS	KJr  S S
Kr\R:                  " \5      r " S S\R@                  5      r!g
)    )viewsetsstatus)action)Response)timezone   )UserFCMTokenUserPatternMLModelAnomalyAlertScheduledNotificationBurnoutAnalysisBurnoutNotificationComplianceNotification)UserFCMTokenSerializerUserPatternSerializerMLModelSerializerAnomalyAlertSerializerScheduledNotificationSerializer)PushNotificationService)%schedule_user_notifications_for_todayNc                   h   \ rS rSrSr\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S	 5       r\" SS/S9S
 5       r	\" SS/S9S 5       r
\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S 5       r\" SS/S9S 5       rSrg)	MLViewSet   z"API endpoints for ML functionalityFpost)detailmethodsc                     UR                   R                  S5      nUR                   R                  S5      nU(       d  [        SS0[        R                  S9$ U(       d  [        SS0[        R                  S9$ [
        R                  R                  UUSSS	.S
9u  pE [        U5        [        SSU[        UR                  5      S.5      $ ! [         a%  n[        R                  SU SU 35         SnANMSnAff = f! [         aE  n[        R                  SU 35        [        S[        U5      0[        R                  S9s SnA$ SnAff = f)z0Register user's FCM token for push notifications	fcm_tokenuser_iderrorzFCM token is requiredr   user_id is requiredmobileT)device_type	is_active)r    r   defaultsz*Could not schedule notifications for user z: Nsuccessz!FCM token registered successfully)r   messagecreatedtoken_idzError registering FCM token: )datagetr   r   HTTP_400_BAD_REQUESTr	   objectsupdate_or_creater   	Exceptionloggerwarningstridr!   HTTP_500_INTERNAL_SERVER_ERROR)selfrequestr   r    	token_objr*   es          !D:\TimeStamps\ml_service\views.pyregister_fcm_tokenMLViewSet.register_fcm_token   sY   ,	((5Ill&&y1G56!66 
 34!66  ".!5!5!F!F##+!% "G "I\5g> #>"	-	    \!KG9TVWXVYZ[[\  	LL8<=#a&!<< 	sT   AC?  C? :$C? C *"C? 
C<C72C? 7C<<C? ?
E	:E	E	Er-   c                     [         R                  R                  UR                  R                  S9n[        USS9n[        UR                  UR                  5       (       a  UR                  5       R                  OSUR                  5       S.5      $ ! [         aE  n[        R                  SU 35        [        S[        U5      0[         R"                  S9s SnA$ SnAff = f)	zGet user's learned patternsr    TmanyN)patternslast_updatedtotal_patternszError getting user patterns: r!   r"   )r
   r/   filteruserr5   r   r   r,   existsfirstrC   countr1   r2   r!   r4   r   r6   )r7   r8   rB   
serializerr:   s        r;   get_user_patternsMLViewSet.get_user_patternsD   s    	"**11',,//1JH.xdCJ&OOAIARAR 0 = =X\"*.."2    	LL8<=#a&!<< 	s   BB 
C":CC"C"c                     [         R                  R                  UR                  R                  [
        R                  " 5       S9R                  S5      n[        USS9n[        UR                  UR                  5       S.5      $ ! [         aE  n[        R                  SU 35        [        S[        U5      0[         R"                  S9s S	nA$ S	nAff = f)
z"Get user's scheduled notifications)r    scheduled_time__gtescheduled_timeTr@   )scheduled_notificationstotal_scheduledz'Error getting scheduled notifications: r!   r"   N)r   r/   rE   rF   r5   r   noworder_byr   r   r,   rI   r1   r2   r!   r4   r   r6   )r7   r8   notificationsrJ   r:   s        r;   get_scheduled_notifications%MLViewSet.get_scheduled_notificationsX   s    	199@@$,LLN A  h'( 
 9TRJ+5??#0#6#6#8  
  	LLB1#FG#a&!<< 	s   BB 
C:C
CCc           
      6    UR                   R                  S5      nU(       d  [        SS0[        R                  S9$ [        U5      n[        5       n[        R                  R                  USS9R                  5       nU(       d  [        SSUS	S
.[        R                  S9$ [        R                  R                  USSSSSSS.S9nUR                  USSS[        UR                  5      SSS.S9nU(       a4  [        SSU[        UR                  5      UR                   SS S-   S.5      $ [        SSUS .[        R"                  S9$ ! [$         aE  n[&        R)                  S!U 35        [        S[        U5      0[        R"                  S9s SnA$ SnAff = f)"zTest push notification for userr    r!   r#   r"   Tr    r&   no_tokenz User has no FCM token registeredz"Open the app to register FCM token)r   r)   r    hintburnout_alertu   🧪 Test Burnout Alertz\This is a test burnout notification from Flexwise. Your work patterns show signs of burnout.g     `U@z$Consider using flex time or vacation	flex_time)r   balance)r    notification_typetitler)   
risk_scorerecommendationtrueburnoutz85.5)testnotification_idr^   r`   )r    r_   bodyr,   r(   z#Test notification sent successfullyN   z...)r   r)   r    re   delivered_tofailedz4Failed to send test notification - check server logs)r   r)   r    z!Error sending test notification: )r,   r-   r   r   r.   r4   r   r	   r/   rE   rH   HTTP_404_NOT_FOUNDr   createsend_notificationr5   r   r6   r1   r2   r!   )r7   r8   r    push_servicefcm_token_objtest_notificationr(   r:   s           r;   ro   MLViewSet.test_notificationo   s   H	ll&&y1G34!66  'lG24L )0077 8  eg 
 !(A&@	!
 !335 5 !4 ; ; B B"1/v*P]hi !C ! #44/s"'*+<+?+?'@)2"(		 5 
G 'D&'*+<+?+?'@$1$;$;CR$@5$H!    &U&! !??	A A  	LL<QC@A#a&!<< 	s0   ;E	 A$E	 #B	E	 -E	 	
F:FFFc                     [         R                  R                  UR                  R                  SS9R                  5       n[        R                  R                  UR                  R                  S9n[        R                  R                  UR                  R                  [        R                  " 5       SS9R                  5       n[        USLUR                  5       U[        5       R                  S.5      $ ! [         aE  n[        R!                  SU 35        [        S	[#        U5      0[$        R&                  S
9s SnA$ SnAff = f)zGet user's notification statusTrX   r?   F)r    rN   is_sentN)fcm_token_registeredpatterns_learnednotifications_scheduledpush_service_availablez#Error getting notification status: r!   r"   )r	   r/   rE   rF   r5   rH   r
   r   r   rR   rI   r   r   firebase_initializedr1   r2   r!   r4   r   r6   )r7   r8   r   rB   scheduled_countr:   s         r;   get_notification_status!MLViewSet.get_notification_status   s   	$,,33 4  eg  #**11',,//1JH 4;;BB$,LLN C  eg	  (1(=$,NN$4+:*A*C*X*X	    	LL>qcBC#a&!<< 	s   C6C9 9
E:E=EEc                     UR                   R                  S[        US5      (       a  UR                  R                  OS5      nU(       d  [        SS0[        R                  S9$ [        R                  R                  US9R                  S5      n/ nU H  nUR                  [        UR                  5      UR                  UR                  UR                   UR"                  UR$                  UR&                  UR(                  R+                  5       UR,                  (       a  UR,                  R+                  5       OSS	.	5        M     UR                  S
S9R/                  5       n[        UUUR/                  5       S.5      $ ! [0         aE  n[2        R5                  SU 35        [        S[        U5      0[        R6                  S9s SnA$ SnAff = f)z Get user's burnout notificationsr    rF   Nr!   r#   r"   r?   z-sent_at)	r5   typer_   r)   r`   ra   is_readsent_atread_atFr}   rT   unread_counttotal_countz%Error getting burnout notifications: )GETr-   hasattrrF   r5   r   r   r.   r   r/   rE   rS   appendr4   r^   r_   r)   r`   ra   r}   r~   	isoformatr   rI   r1   r2   r!   r6   )r7   r8   r    rT   notification_datanotifr   r:   s           r;   get_burnout_notifications#MLViewSet.get_burnout_notifications   s   )	kkooiGGU[D\D\bfgG34!66 
 077>> ? hz" 
 !#&!((ehh-!33"[[$}}"'"2"2&+&:&:$}}$}}668<AMMu}}668t
* 
 ' )///>DDFL!2 ,,224    	LL@DE#a&!<< 	s%   A#F &DF 
G:GGGc                 |    UR                   R                  S5      nUR                   R                  S[        US5      (       a  UR                  R                  OS5      nU(       d  [        SS0[        R                  S9$ [        R                  R                  UUS9nUR                  5         [        S	S
S.5      $ ! [        R                   a    [        SS0[        R                  S9s $ [         aE  n[        R                  SU 35        [        S[!        U5      0[        R"                  S9s SnA$ SnAff = f)z!Mark burnout notification as readre   r    rF   Nr!   notification_id is requiredr"   r5   r    r(   Notification marked as readr   r)   Notification not found$Error marking notification as read: )r,   r-   r   rF   r5   r   r   r.   r   r/   mark_as_readDoesNotExistrj   r1   r2   r!   r4   r6   r7   r8   re   r    notificationr:   s         r;   mark_burnout_notification_read(MLViewSet.mark_burnout_notification_read  s,    	%ll../@AOll&&yWWV\E]E]',,//cghG";<!66 
 /66::" ; L
 %%'#8  
 #// 	2300   	LL?sCD#a&!<< 	*   A>B= ;B= =.D;-	D;6:D60D;6D;c           	          UR                   R                  S[        US5      (       a  UR                  R                  OS5      nU(       d  [        SS0[        R                  S9$ [        R                  R                  US9R                  SS	5      R                  5       nU(       d  [        S
SS.5      $ [        UR                  UR                  UR                  UR                   UR"                  R%                  5       UR&                  R%                  5       S.5      $ ! [(         aE  n[*        R-                  SU 35        [        S[/        U5      0[        R0                  S9s SnA$ SnAff = f)zGet user's burnout analysisr    rF   Nr!   r#   r"   r?   z-datez-created_atno_analysisz!No burnout analysis available yetr   )r`   severityrisk_factorsrecommendationsdate
created_atz Error getting burnout analysis: )r   r-   r   rF   r5   r   r   r.   r   r/   rE   rS   rH   r`   r   r   r   r   r   r   r1   r2   r!   r4   r6   )r7   r8   r    analysisr:   s        r;   get_burnout_analysisMLViewSet.get_burnout_analysis1  sI   "	kkooiGGU[D\D\bfgG34!66  '..55 6 hw.uuw  +B!  
 &11$-- ( 5 5#+#;#; //1&11;;=    	LL;A3?@#a&!<< 	s,   A#D  &AD  6A)D   
E/*:E*$E/*E/c                 |    UR                   R                  S5      nUR                   R                  S[        US5      (       a  UR                  R                  OS5      nU(       d  [        SS0[        R                  S9$ [        R                  R                  UUS9nUR                  5         [        S	S
S.5      $ ! [        R                   a    [        SS0[        R                  S9s $ [         aE  n[        R                  SU 35        [        S[!        U5      0[        R"                  S9s SnA$ SnAff = f)zDelete a burnout notificationre   r    rF   Nr!   r   r"   r   r(   !Notification deleted successfullyr   r   Error deleting notification: )r,   r-   r   rF   r5   r   r   r.   r   r/   deleter   rj   r1   r2   r!   r4   r6   r   s         r;   delete_burnout_notification%MLViewSet.delete_burnout_notificationX  s,    	%ll../@AOll&&yWWV\E]E]',,//cghG";<!66 
 /66::" ; L
 !#>  
 #// 	2300   	LL8<=#a&!<< 	r   c                     UR                   R                  S[        US5      (       a  UR                  R                  OS5      nU(       d  [        SS0[        R                  S9$ [        R                  R                  US9nUR                  SS	9R                  5       nU Vs/ s H  n[        UR                  5      UR                  UR                  UR                  UR                   UR"                  UR$                  UR&                  R)                  5       UR*                  (       a  UR*                  R)                  5       OSS
.	PM     nn[        UUUR                  5       S.5      $ s  snf ! [,         aE  n[.        R1                  SU 35        [        S[        U5      0[        R2                  S9s SnA$ SnAff = f)z#Get user's compliance notificationsr    rF   Nr!   r#   r"   r?   Fr   )	r5   r|   r_   r)   r   time_remainingr}   r~   r   r   z(Error getting compliance notifications: )r   r-   r   rF   r5   r   r   r.   r   r/   rE   rI   r4   
alert_typer_   r)   r   r   r}   r~   r   r   r1   r2   r!   r6   )r7   r8   r    rT   r   nr   r:   s           r;   get_compliance_notifications&MLViewSet.get_compliance_notifications}  s   &	kkooiGGU[D\D\bfgG34!66  3::AA'ARM )///>DDFL %
!& %! !$$i99JJ"#"2"29999..045II199..04
" %  
!& !2 ,,224  
!&$  	LLCA3GH#a&!<< 	s7   A#E0 &>E0 $B(E+E0 +E0 0
F?::F:4F?:F?c                 ,    UR                   R                  S5      nUR                   R                  S5      nU(       d  [        SS0[        R                  S9$ [
        R                  R                  UUS9nUR                  5         [        SSS	.5      $ ! [
        R                   a    [        SS
0[        R                  S9s $ [         aE  n[        R                  SU 35        [        S[        U5      0[        R                  S9s SnA$ SnAff = f)z$Mark compliance notification as readre   r    r!   r   r"   r   r(   r   r   r   r   N)r,   r-   r   r   r.   r   r/   r   r   rj   r1   r2   r!   r4   r6   r   s         r;   !mark_compliance_notification_read+MLViewSet.mark_compliance_notification_read  s    	%ll../@AOll&&y1G";<!66 
 299==" > L
 %%'#8  
 &22 	2300   	LL?sCD#a&!<< 	*   AB ;B .D	D:DDDc                 ,    UR                   R                  S5      nUR                   R                  S5      nU(       d  [        SS0[        R                  S9$ [
        R                  R                  UUS9nUR                  5         [        SSS	.5      $ ! [
        R                   a    [        SS
0[        R                  S9s $ [         aE  n[        R                  SU 35        [        S[        U5      0[        R                  S9s SnA$ SnAff = f)z Delete a compliance notificationre   r    r!   r   r"   r   r(   r   r   r   r   N)r,   r-   r   r   r.   r   r/   r   r   rj   r1   r2   r!   r4   r6   r   s         r;   delete_compliance_notification(MLViewSet.delete_compliance_notification  s    	%ll../@AOll&&y1G";<!66 
 299==" > L
 !#>  
 &22 	2300   	LL8<=#a&!<< 	r    N)__name__
__module____qualname____firstlineno____doc__r   r<   rK   rU   ro   ry   r   r   r   r   r   r   r   __static_attributes__r       r;   r   r      sj   ,56(+. ,.` 55'* +& 55'* +, 56(+J ,JX 55'* +B 55'*+ ++Z 56(+" ,"H 55'*$ +$L 56(+" ,"H 55'*( +(T 56(+" ,"H 56(+" ,"r   r   )"rest_frameworkr   r   rest_framework.decoratorsr   rest_framework.responser   django.utilsr   modelsr	   r
   r   r   r   r   r   r   serializersr   r   r   r   r   rm   r   tasksr   logging	getLoggerr   r2   ModelViewSetr   r   r   r;   <module>r      s\    + , , ! Z  Z  Z  2 8 			8	$`%% `r   