
    19i;'                     f    S SK Jr  S SKJrJr  S SKJrJr  S SKJ	r	  S SK
J
r
  S SKr " S S\5      rg)	    )BaseCommand)schedule_daily_notificationssend_notification_at_exact_time)UserPatternScheduledNotification)timezone)datetimeNc                   0    \ rS rSrSrS rS rS rS rSr	g)	Command	   zManually trigger notification scheduling or send notifications immediately. Use --schedule to schedule notifications for today, or --send-now to send notifications immediately for a specific user.c                     UR                  SSSS9  UR                  SSSS9  UR                  S[        SS	9  UR                  S
[        SS	9  UR                  SSSS9  g )Nz
--schedule
store_truezHSchedule all notifications for today (runs schedule_daily_notifications))actionhelpz
--send-nowzBSend notifications immediately for all users (bypasses scheduling)z	--user-idz?Specific user ID to send notification for (use with --send-now))typer   z--question-typez`Specific question_type to send notification for (use with --send-now, e.g., clock_in, lunch_out)z	--verbosezShow detailed output)add_argumentstr)selfparsers     ED:\TimeStamps\ml_service\management\commands\trigger_notifications.pyadd_argumentsCommand.add_arguments   s    [ 	 	

 	U 	 	

 	R 	 	

 	s 	 	

 	' 	 	
    c                    UR                  SS5      nUR                  SS5      nUR                  SS 5      nUR                  SS 5      nUR                  SS5      nU(       a  U R                  U5        g U(       a  U R                  XVU5        g U R                  R	                  U R
                  R                  S5      5        U R                  R	                  S5        U R                  R	                  S	5        U R                  R	                  S
5        U R                  R	                  S5        U R                  R	                  S5        g )NscheduleFsend_nowuser_idquestion_typeverbosez'Please specify --schedule or --send-now z	Examples:z3  python manage.py trigger_notifications --schedulez3  python manage.py trigger_notifications --send-nowz^  python manage.py trigger_notifications --send-now --user-id USER_ID --question-type clock_in)get_schedule_notifications_send_notifications_nowstdoutwritestyleERROR)r   argsoptionsr   r   r   r   r   s           r   handleCommand.handle'   s    ;;z51;;z51++i.OT:++i/((1((IKKdjj../XYZKKb!KKk*KKSTKKSTKK~r   c           	         U R                   R                  U R                  R                  S5      5        U R                   R                  S5        U R                   R                  S5         [	        5       nU(       a  U R                   R                  S5        U R                   R                  U R                  R                  S5      5        U R                   R                  S5        U R                   R                  S5        U R                   R                  [        R                  " US[        S95        gUR                  SS	5      nUS
:X  Ga5  UR                  SS5      nUR                  SS5      nU R                   R                  S5        U R                   R                  U R                  R                  S5      5        U R                   R                  S5        U R                   R                  SU 35        U R                   R                  SU 35        U R                   R                  S5        U R                   R                  U R                  R                  S5      5        U R                   R                  S5        gU R                   R                  S5        U R                   R                  U R                  R                  SU 35      5        SU;   a.  U R                   R                  SUR                  S5       35        gg! [         a  nU R                   R                  S5        U R                   R                  U R                  R                  S[        U5       35      5        U(       ah  SSKnU R                   R                  S5        U R                   R                  S5        U R                   R                  UR                  5       5         SnAg SnAgSnAff = f)z Schedule notifications for todayz%Scheduling notifications for today...z=This will schedule notifications for all users with patterns.r    zTask completed successfully!zResult:   )indentdefaultstatusunknownsuccessnotifications_scheduledr   users_processedu)   ✓ Notifications scheduled successfully!zUsers processed: zNotifications scheduled: z:Note: Notifications will be sent at their scheduled times.z0Make sure Celery worker is running to send them.zTask failed with status: errorzError: u&   ✗ Failed to schedule notifications: N
Traceback:)r$   r%   r&   WARNINGr   SUCCESSjsondumpsr   r!   r'   	Exception	traceback
format_exc)r   r   resultr0   	scheduleduserser<   s           r   r"   Command._schedule_notifications:   s   $**,,-TUVYZ"%	:13F!!"%!!$**"4"45S"TU!!"%!!),!!$**VAs"KLHi8Y& &

+Da HI"JJ'8!<EKK%%b)KK%%djj&8&89d&efKK%%b)KK%%(9%&ABKK%%(A)&MNKK%%b)KK%%djj&8&8T'  KK%%&XYKK%%b)KK%%djj&6&69RSYRZ7[&\]&())GFJJw4G3H*IJ ) 	:KKb!KKdjj..1WX[\]X^W_/`ab !!"%!!,/!!)"6"6"899	 	:s(   ,C	L 6EL BL 
O)CO$$O)c                 p   SSK Jn  U R                  R                  U R                  R                  S5      5        U R                  R                  S5         U" 5       nSnSnSnU(       Ga@  U(       Ga8  U R                  R                  SU SU S35         [        R                  R                  XS9n	UR                  X5      (       a:  U R                  R                  U R                  R                  S	5      5        US
-  ngU	R                  R                  S5      n
UR                  XU
5      nU(       a:  U R                  R                  U R                  R                  S5      5        US
-  ngU R                  R                  U R                  R                  S5      5        US
-  ngU R                  R                  S5        U R                  R                  S5        [        R                  R!                  5       nUR#                  5       nU GH  n	 UR                  [%        U	R&                  5      U	R(                  5      (       aC  US
-  nU(       a5  U R                  R                  SU	R&                   SU	R(                   35        M|  U	R                  R                  S5      n
UR                  [%        U	R&                  5      U	R(                  U
5      nU(       a`  US
-  nU(       aQ  U R                  R                  U R                  R                  SU	R&                   SU	R(                   35      5        GM+  GM.  US
-  nU(       aQ  U R                  R                  U R                  R                  SU	R&                   SU	R(                   35      5        GM  GM     U R                  R                  S5        U R                  R                  U R                  R                  S5      5        U R                  R                  S5        U R                  R                  SU 35        U R                  R                  SU 35        U R                  R                  SU 35        U R                  R                  SU 35        g! [        R                   aB    U R                  R                  U R                  R                  SU SU 35      5        US
-  n gf = f! [*         al  nUS
-  nU(       aT  U R                  R                  U R                  R                  SU	R&                   S[%        U5       35      5         SnAGM6   SnAGM=  SnAff = f! [*         a  nU R                  R                  S5        U R                  R                  U R                  R                  S[%        U5       35      5        U(       ah  SSKnU R                  R                  S5        U R                  R                  S5        U R                  R                  UR/                  5       5         SnAg SnAgSnAff = f)zSend notifications immediatelyr   )PushNotificationServicez$Sending notifications immediately...r    zSending notification for user z, type z...)r   r   z-User already completed this action - skipping   z%H:%Mu#   ✓ Notification sent successfully!u   ✗ Failed to send notificationzPattern not found for user z4Sending notifications for all users with patterns...zSkipped: User z already completed u   ✓ Sent to user z for u   ✗ Failed to send to user u   ✗ Error sending to user z: Nu#   ✓ Notification sending completed!zTotal patterns: zNotifications sent: zSkipped (already done): zFailed: u"   ✗ Failed to send notifications: r6   )ml_service.push_servicerD   r$   r%   r&   r7   r   objectsr!   user_already_did_actionaverage_timestrftimesend_smart_reminderr8   r'   DoesNotExistallcountr   r   r   r;   r<   r=   )r   r   r   r   rD   push_service
sent_countskipped_countfailed_countpatterntypical_time_strr2   patternstotal_patternsrA   r<   s                   r   r#   Command._send_notifications_nowg   s   C$**,,-STU"^	:24LJML=!!$B7)7S`Raad"ef&)1155g5cG $;;GSS))$***<*<=l*mn%*+2+?+?+H+H+Q("."B"B7[k"l" KK--djj.@.@Af.gh&!OJ KK--djj.>.>?`.ab(A-L !!"XY!!"%&..224!)!1'G''??GOO@TV]VkVkll)Q.M& $ 1 1N7??BSSfgng|g|f}2~ $ ,3+?+?+H+H+Q("."B"B0#11,# #&!OJ& $ 1 1$(JJ$6$6*;GOO;LERYRgRgQh(i%&!"  ' )A-L& $ 1 1$(JJ$4$4*EgooEVV[\c\q\q[r(s%&!"  '7  (T !!"%!!$**"4"45Z"[\!!"%!!$4^4D"EF!!$8"EF!!$<]O"LM!!H\N";<w #// &KK%%djj&6&69TU\T]]derds7t&uv A%L&X % $)" KK-- $

 0 0&@@QQSTWXYTZS[$\!"  #"  	:KKb!KKdjj..1STWXYTZS[/\]^ !!"%!!,/!!)"6"6"899	 	:s   ?U A,R A-R 29R ,A*U A5SU B,S:U  ASC*U ASU SU 
U(AUU UU 
X5!CX00X5 N)
__name__
__module____qualname____firstlineno__r   r   r*   r"   r#   __static_attributes__rX   r   r   r   r   	   s!     RD
6@&+:Ze:r   r   )django.core.management.baser   ml_service.tasksr   r   ml_service.modelsr   r   django.utilsr   r	   r9   r   rX   r   r   <module>rb      s%    3 Z @ !  C:k C:r   