
    B(i<                         S SK r S SKJr  S SKJr  SSKJrJr  S SKJrJ	r	J
r
  \ R                  " \5      r " S S5      rg)	    N)settings)timezone   )UserFCMTokenUserPattern)datetimetime	timedeltac            	           \ rS rSrS rS rSS\S\S\S\4S	 jjrS\S
\S\4S jr	S\S
\S\
4S jrS\S\4S jrS\S\4S jrS\S\S\4S jrS\S\4S jrSrg)PushNotificationService	   c                 2    SU l         U R                  5         g )NF)firebase_initializedinitialize_firebase)selfs    (D:\TimeStamps\ml_service\push_service.py__init__ PushNotificationService.__init__
   s    $)!  "    c                 2    SSK nSSK JnJn  UR                  (       dN   UR	                  [
        R                  5      nUR                  U5        SU l        [        R                  S5        gSU l        [        R                  S5        g! [         a  n[        R                  SU 35         [        [
        SS5      nU(       a5  UR                  SU0S	9  SU l        [        R                  S
U 35         SnAgUR                  5         SU l        [        R                  S5         SnAg! [         a-  n[        R                  SU 35        SU l         SnA SnAgSnAff = fSnAff = f! [         a    [        R                  S5        SU l         gf = f)zInitialize Firebase Admin SDKr   N)credentials	messagingTz-Firebase initialized with service account keyz4Could not initialize Firebase with service account: FIREBASE_PROJECT_ID	projectId)optionsz&Firebase initialized with project ID: z-Firebase initialized with default credentialszCould not initialize Firebase: FzFirebase already initializedzAFirebase Admin SDK not installed. Run: pip install firebase-admin)firebase_adminr   r   _appsCertificater   FIREBASE_SERVICE_ACCOUNT_KEYinitialize_appr   loggerinfo	ExceptionwarninggetattrerrorImportError)r   r   r   r   crede
project_ide2s           r   r   +PushNotificationService.initialize_firebase   sY   !	.!=!'':&2283X3XYD"11$704D-KK OP$ -1):;% ! :NN%YZ[Y\#]^:%,X7Ld%S
%*99;PZB[9\8<D5"KK*PQ[P\(]^^*99;8<D5"KK(WXX$ :'Frd%KL4911::(  	.LL\](-D%	.sr   E- AB
 -E- 

E*E%-AD+5E- :,D+&E- +
E"5EE%E- E""E%%E**E- -&FFNuser_idtitlebodydatac                    U R                   (       d  [        R                  S5        g SSKJn  [
        R                  R                  USS9R                  5       nU(       d  [        R                  SU 35        gUR                  UR                  UUS9U=(       d    0 UR                  S	9nUR                  U5      n[        R                  S
U 35        g! [         a"  n	[        R                  SU	 35         Sn	A	gSn	A	ff = f)zSend push notification to userz2Firebase not initialized, cannot send notificationFr   )r   T)r-   	is_activezNo FCM token found for user r.   r/   )notificationr0   tokenzSuccessfully sent message: z!Error sending push notification: N)r   r!   r&   r   r   r   objectsfilterfirstr$   MessageNotification	fcm_tokensendr"   r#   )
r   r-   r.   r/   r0   r   r;   messageresponser)   s
             r   send_notification)PushNotificationService.send_notification3   s    ((LLMN	0 %,,33 4  eg 
 !=gYGH  ''&33 4  ZR)) ( G !~~g.HKK5hZ@A 	LL<QC@A	s   AC ;AC 
D!C>>Dquestion_typetypical_timec           	          SSU 3S.SSU 3S.SSU 3S.SS	U 3S.S
.nUR                  USSS.5      nU R                  UUS   US   UUUS.S9$ )z Send smart reminder notificationu   ⏰ Time to Start Your DayzYou usually clock in at r3   u   🍽️ Lunch TimezYou typically go to lunch at u   ⏰ Lunch Break Endingz!You usually return from lunch at u   🏠 End of DayzYou usually clock out at )clock_in	lunch_outlunch_in	clock_outzFlexwise ReminderzYou have a reminderr.   r/   )rA   rB   action)r-   r.   r/   r0   )getr?   )r   r-   rA   rB   messagesmessage_datas         r   send_smart_reminder+PushNotificationService.send_smart_reminderY   s     62<.A
 .7~F
 2;L>J
 +3L>B
&  ||M()4
 
 %%w'f%!. ,'	 & 	
 		
r   returnc                     SSK Jn  [        R                  " 5       R	                  5       nUR
                  R                  UUUS9R                  5       $ )z+Check if user already did this action todayr   )Stamp)user__idstamp_functiondate)stamps.modelsrP   r   nowrS   r6   r7   exists)r   r-   rA   rP   todays        r   user_already_did_action/PushNotificationService.user_already_did_action~   sG    '##%}}##( $ 
 &(		r   tc                 Z    UR                   S-  UR                  S-  -   UR                  -   $ )z&Convert time to seconds since midnight  <   )hourminutesecond)r   rZ   s     r   time_to_seconds'PushNotificationService.time_to_seconds   s&    vv}qxx"},qxx77r   secondsc                 >    US-  nUS-  S-  nUS-  n[        X#U5      $ )z&Convert seconds since midnight to timer\   r]   )r	   )r   rc   hoursminutessecss        r   seconds_to_time'PushNotificationService.seconds_to_time   s/    4T>b(|ED))r   time_objrf   c                 h    U R                  U5      US-  -
  nUS:  a  US-  nU R                  U5      $ )z!Subtract minutes from time objectr]   r   iQ )ra   rh   )r   rj   rf   total_secondss       r   subtract_minutes(PushNotificationService.subtract_minutes   s>    ,,X6'B,G1Y&M##M22r   c                     [         R                  " 5       R                  5       n[        R                  " X!5      nU[         R                  " 5       ::  a  U[        SS9-  nU$ )z'Get next occurrence of time as datetimer   )days)r   rU   rS   r   combiner
   )r   rj   rW   target_datetimes       r   get_next_occurrence+PushNotificationService.get_next_occurrence   sK    ##%"**5; hlln,ya00Or   )r   )N)__name__
__module____qualname____firstlineno__r   r   strdictr?   rL   boolrX   r	   intra   rh   rm   r   rs   __static_attributes__ r   r   r   r   	   s    ##.J$ $S $ $4 $L#
3 #
s #
RU #
J	s 	3 	4 	8 8# 8*s *t *3 3 3 3	D 	X 	r   r   )loggingdjango.confr   django.utilsr   modelsr   r   r   r	   r
   	getLoggerru   r!   r   r~   r   r   <module>r      s3       ! - . .			8	$[ [r   