
    ;i                     x    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	Z	 e	j                  e      Z G d d      Zy)    )NotificationRepository)NotificationType)UserCompanyRoleRole)UserNc                       e Zd Zedd       Zed        Zed        Zed        Zed        Zed        Z	ed        Z
ed	        Zedd
       Zy)NotificationServiceNc                     |t        j                         }| j                  r|j                  d      S t        j                  |       S )Nz-created_at)r   base_querysetis_superuserorder_bylist_for_user)userqss     Q/var/www/html/dp2/backend_v2.1/server/apps/calls/services/notification_service.pylist_notificationsz&NotificationService.list_notifications   s>    :'557B;;}--%33D99    c                 .    t        j                  ||       S N)r   	get_by_id)r   notif_ids     r   get_notificationz$NotificationService.get_notification   s    %//$??r   c                 ^    t         j                  | |      }|y t        j                  |      S r   )r	   r   r   mark_as_read)r   r   notifs      r   r   z NotificationService.mark_as_read   s-    #44T8D=%22599r   c           	         	 t         j                  j                  | dd      j                         }|s$t        j                  d| j                          g S t        j                  j                  | |d      j                  d      }|D cg c]%  }|j                  j                  s|j                  ' }}|S c c}w # t        $ r:}t        j                  d| j                   dt        |              g cY d	}~S d	}~ww xY w)
z7Get all users with Chief Admin role for a given companyzChief AdminTcompanyname	is_activez'No Chief Admin role found for company: r   roler    r   z'Error getting chief admins for company : Nr   objectsfilterfirstloggerwarningr   r   select_relatedr   r    	Exceptionerrorstr)r   chief_admin_roleuser_company_rolesucrchief_adminses         r   get_chief_admins_for_companyz0NotificationService.get_chief_admins_for_company"   s    	#||22"  3   eg	  $!HWX	!0!8!8!?!?% "@ " nV$	  1CY0BchhFXFXCHH0BLY Z 	LLB7<<.PRSVWXSYRZ[\I	<   AC  5C  B;)B;7C  ;C   	D	/C>8D>Dc           	         	 t         j                  j                  | dd      j                         }|s$t        j                  d| j                          g S t        j                  j                  | |d      j                  d      }|D cg c]%  }|j                  j                  s|j                  ' }}|S c c}w # t        $ r:}t        j                  d| j                   dt        |              g cY d	}~S d	}~ww xY w)
z;Get all users with Service Manager role for a given companyzService ManagerTr   z+No Service Manager role found for company: r!   r   z+Error getting service managers for company r#   Nr$   )r   service_manager_roler/   r0   service_managersr2   s         r    get_service_managers_for_companyz4NotificationService.get_service_managers_for_company<   s    	#'<<#6#6& $7 $ eg	 ! (!LW\\N[\	!0!8!8!?!?) "@ " nV$	  5G]4FS#((J\J\4F]##  ^ 	LLFw||nTVWZ[\W]V^_`I	r4   c                     | sy	 t        | d      r"| j                  r| j                  j                  S | j                  xs | j                  S # t
        $ r | j                  xs | j                  cY S w xY w)z(Helper method to safely get advisor nameUnknownprofile)hasattrr;   	full_nameusernameemailr+   )r   s    r   _get_advisor_namez%NotificationService._get_advisor_nameV   sd     	/tY'DLL||---}}2

2 	/==.DJJ.	/s   -A A #A32A3c                    | D ]  }|r|j                   |j                   k(  r	 t        j                  |      }	|r|j                  nd}
d|	 d|
 d| }t	        j
                  |t        j                  |||       t        j                  d| d|j                           y
# t        $ r;}t        j                  d| d	|j                   dt        |              Y d
}~d
}~ww xY w)z6Helper method to send notifications to managers/adminsUnknown CompanyzAdvisor z from z missed a call from 	recipientnotification_typetitlemessagecallz!Missed call notification sent to r#   zError sending notification to  N)idr	   r@   r   r   create_notificationr   MISSED_CALLr(   infor?   r+   r,   r-   )managersr   rH   display_namecustomer_phonerF   	role_namer   manageradvisor_namecompany_namemanager_messager2   s                r   _send_notification_to_managersz2NotificationService._send_notification_to_managersc   s      G

dgg-e2DDTJ/6w||<M$,\N&Nbcobp"q&::%&6&B&B+ ?	{"W]]O\]     e=i['--XZ[^_`[aZbcddes   A=B##	C',1C""C'c           
         	 |r|nd}|r|j                   nd}d| }d}|rK	 t        j                  |t        j                  |||        t
        j                  d|j                          t        j                  |      }
t        j                  |
|| |||d
|       t        j                  |      }t        j                  ||| |||d|       y	# t        $ r8}	t
        j                  d|j                   dt        |	              Y d	}	~	d	}	~	ww xY w# t        $ r+}	t
        j                  dt        |	              Y d	}	~	y	d	}	~	ww xY w)ax  
        Send missed call notifications to advisor, all chief admins, and service managers of the company.
        
        Args:
            call: Call object
            user: User object (the user who missed the call)
            company: Company object
            customer_name: str (customer name or None)
            customer_phone: str (customer phone number)
        zUnknown CustomerrB   zMissed call from zMissed CallrC   z*Missed call notification sent to advisor: z&Error sending notification to advisor r#   Nzchief adminzservice managerz(Error in send_missed_call_notification: )r   r   rK   r   rL   r(   rM   r?   r+   r,   r-   r	   r3   rV   r8   )rH   r   r   customer_namerP   rO   rT   rG   rF   r2   r1   r7   s               r   send_missed_call_notificationz1NotificationService.send_missed_call_notificationy   sJ   	N,9=?QL+27<<8IL),8G!E
b*>>"&*:*F*F# '! KK"LTZZL YZ /KKGTL>>dD,}^e  3SST[\>> $lNESdfm ! bLL#I$**UWX[\]X^W_!`aab  	NLLCCF8LMM	NsB   D A
C ,A"D 	D.DD DD 	E!EEc                 H   | t         j                  d       y	 t        j                  | t        j
                  ||       t         j                  d| j                          y# t        $ r4}t         j                  d| j                  t        |             Y d}~yd}~ww xY w)z
        Send message notifications to advisor of company.

        Args:
            user: User object (the user who missed the call)
            title: str (notification title)
            message: str (notification message)
        Nu)   Cannot send notification — user is None)rD   rE   rF   rG   zMessage notification sent to z-Failed to send message notification to %s: %s)r(   r,   r   rK   r   MESSAGErM   r?   r+   	exceptionr-   )r   rF   rG   r2   s       r   send_message_notificationz-NotificationService.send_message_notification   s     <LLDE	"66"2":":	 KK7

|DE 	?

CF 	s   A	A$ $	B!-*BB!r   ) r^   )__name__
__module____qualname__staticmethodr   r   r   r3   r8   r@   rV   rY   r]    r   r   r	   r	   
   s    : : @ @ : :  2  2 
/ 
/ e e* *N *NX  r   r	   )apps.calls.repositoriesr   apps.calls.modelsr   apps.permissions.modelsr   r   apps.core.modelsr   logging	getLoggerr_   r(   r	   rc   r   r   <module>rj      s2    : . 9 ! 			8	$w wr   