
    =qh'                     ~   d dl mZ d dlmZmZmZmZ d dlZd dlZd dl	m
Z
 d dlmZ d dlZd dlZd dlmZ d dlZd dlZd dlmZmZmZmZmZ d dlmZ d dlmZ d d	l	m
Z
mZ  ej2                  e      Zd
edededee   dee   deeef   fdZd
ededeeeef      fdZdedefdZ d
edejB                  fdZ"d Z#d Z$y)    )Session)DictAnyOptionalListN)models)datetime)parseformat_numberPhoneNumberFormatNumberParseExceptionis_valid_number)
send_email)SALES_EMAIL_CREDENTIALS)r   schemasdbpage	page_size	startDateendDatereturnc                    	 |dz
  |z  }| j                  t        j                        }t        j                  d      |rJ|rH|j                  t        j                  j                  |k\  t        j                  j                  |k        }|j                         }|j                  t        j                  j                  j                               j                  |      j                  |      j                         }fd}	d }
|D cg c]
  } |
|       c}|||z   dz
  |z  ||ddS c c}w # t        $ r5}t        j!                  dt#        |              g dd||ddcY d }~S d }~ww xY w)	N   z
US/Pacificc                     | rK| j                   t        j                  j                  |       } | j	                        j                  d      S y )N%b %d, %Y %I:%M %p)tzinfopytzutclocalize
astimezonestrftime)dtpsts    K/var/www/html/DP/alpha_backend/auth_service/services/appointment_service.py
format_pstz.get_paginated_appointments.<locals>.format_pst9   sB    99$**2.B}}S)223GHH    c                    | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  r| j                  j                  d      d	S d d	S )N%Y-%m-%d %H:%M:%S)	idnameemailphonetwilio_call_idscheduleoff_time
created_at
updated_at)
r)   r*   r+   r,   r-   r.   r/   r0   r1   r!   )appointments    r$   	serializez-get_paginated_appointments.<locals>.serialize@   s    !nn#(($**$**"-"<"<'00'00)44 #--  **334GH   r&   )totaltotal_pagesr   r   )data
paginationzError retrieving appointments: r   )queryr   AppointmentSchedulingr   timezonefilterr0   countorder_byr)   descoffsetlimitall	Exceptionloggererrorstr)r   r   r   r   r   r?   r8   r4   appointmentsr%   r3   aer#   s                @r$   get_paginated_appointmentsrI      s\   C
(i'556mmL) LL,,779D,,777BE
  NN677::??ABVF^U9SU	 			$ ,88<aYq\<8 %	 1A 5)C&	
 	
8  

6s1vh?@ &	
 	


s0   DD2 
D-D2 -D2 2	E0;*E+%E0+E0r-   c                 f   	 | j                  t        j                        j                  t        j                  j                  |k(        j                         }|sy i d|j                  d|j                  r|j                  j                  d      nd d|j                  d|j                  d|j                  d|j                  d|j                  d	|j                  d
|j                  d|j                  d|j                   d|j"                  d|j$                  d|j&                  d|j(                  d|j*                  d|j,                  |j.                  |j0                  |j2                  dS # t4        $ r+}t6        j9                  dt;        |              Y d }~y d }~ww xY w)Nr)   call_date_timer(   call_duration	call_costcall_summarytransferred_totransfer_statuscall_idsourcer-   caller_numbercaller_name
end_reasonsentiment_scorerecording_url
transcriptcall_status)booking_intentbooking_intent_statustwilio_recording_textz,Failed to fetch call log by twilio_call_id: )r8   r   CallsLogr;   r-   firstr)   rK   r!   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   rB   rC   rD   rE   )r   r-   callrH   s       r$   fetch_call_log_by_twilio_idr`   h   s   &HHV__%VFOO22nDEUW 	 
$''
&& ##,,-@A
 T//
 
 D--
 d11
 t33
 t||
 dkk
 d11
 T//
  4++!
" $//#
$ t33%
& T//'
( $//)
* 4+++
, #11%)%?%?%)%?%?1
 	
4  CCF8LMs   AE< DE< <	F0!F++F0r,   c                 @   	 t        | d       }t        |      s| S t        |t        j                        }|j                  d      r/t        |      dk(  r!|d d }|dd }|dd }|dd  }| d| d| d| S t        |t        j                        S # t        $ r | cY S w xY w)	Nz+1            z (z) -)	r
   r   r   r   E164
startswithlenINTERNATIONALr   )r,   phone_numberrawcountry_code	area_codecentral_officeline_numbers          r$   format_phone_numberrq      s    UD)|,L L*;*@*@A >>$CHNr7LAaI 1XNab'K"^2i[>2B!K=QQ \+<+J+JKK s   B AB 5B BBr2   c                    | j                  t        j                        j                  |j                        j                         }|rX|j                         j                         D ]  \  }}t        |||        | j                          | j                  |       |dfS t        j                  di |j                         }| j                  |       | j                          | j                  |       |dfS )N)r-   TF )r8   r   r9   	filter_byr-   r^   dictitemssetattrcommitrefreshadd)r   r2   existingfieldvaluedb_appointments         r$   create_appointmentr      s    xx445??{OiOi?jpprH',,.446LE5HeU+ 7
		


8~  55K8H8H8JK
~
		


>"u$$r&   c           	         t        j                  | d         }t        j                  j	                  d      dk(  rd}t        d       nd}t        d       t        |d       t        j                  d	      }t        j                  |      j                  d
      }|j                  d
      }d}t        | d         }d| d| d    d| d| d	}t        |d||t               y)Nr.   ENVIRONMENTLIVEninda@surfcitynissan.com, Kgomez@puentehillsnissan.com, Kbarrita@coronanissan.com, dlopez@surfcitynissan.com, shahzad@fluten.ai, tmckinney@gkmotors.comLive emailsQshahzad@fluten.ai, smalik@fluten.ai, zaidnexrupt@gmail.com, hananali423@gmail.com
Dev emails------------------------emailsAmerica/Los_Angelesr   z>Dear Team, A new appointment has been scheduled on DealerPulser,   z<html>
<body>
<p>Dear Team,</p>
<p style="margin-bottom:0px!important">A new appointment has been scheduled on DealerPulse:</p>
<p style="margin:0px!important;"><strong>Timestamp:</strong> <br>
<strong>Name:</strong> r*   <br>
<strong>Phone:</strong> z <br>
<strong>Schedule:</strong> ><br>
</p>
<p>Regards,<br>
DealerPulse Team</p>
</body>
</html>8DealerPulse Appointment Schedule Alert [Surfcity Nissan]	recipientsubjectbody	html_bodycredentialsTr	   fromisoformatosenvirongetprintr   r:   nowr!   rq   r   r   	r2   appSchedulemanager_emails_strr#   current_timeformatted_schedule_date
email_bodyconvr   s	            r$   send_appointment_emailr      s   ((Z)@AK 
zz~~m$. wm ql	
>?
---
.C<<$--.BCL)223GHSJ{734D> ?K^ L#F+, - 34 5I $J+ r&   c           	         t        j                  | d         }t        j                  j	                  d      dk(  rd}t        d       nd}t        d       t        |d       t        j                  d	      }t        j                  |      j                  d
      }|j                  d
      }d}t        | d         }d| d| d    d| d| d	}t        |d||t               y)Nr.   r   r   r   r   r   r   r   r   r   z9Dear Team, An appointment has been updated on DealerPulser,   z<html>
<body>
<p>Dear Team,</p>
<p style="margin-bottom:0px!important">An appointment has been updated on DealerPulse:</p>
<p style="margin:0px!important;"><strong>Timestamp:</strong> r   r*   r   z(<br>
<strong>Updated Schedule:</strong> r   r   r   Tr   r   s	            r$   send_appointment_email_updater      s   ((Z)@AK 
zz~~m$. wm ql	
>?
---
.C<<$--.BCL)223GHNJ{734D> ?K^ L#F+, - $$;#< =I $J+ r&   )%sqlalchemy.ormr   typingr   r   r   r   jsonlogging
app.commonr   r	   r   r   phonenumbersr
   r   r   r   r   !app.common.services.email_servicer   app.common.constantsr   r   	getLogger__name__rC   intrE   rI   r`   rq   AppointmentCreater   r   r   rs   r&   r$   <module>r      s   " , ,     	      9 8 & 
		8	$J
J

J
 J
 !	J

 hJ
 
#s(^J
Z))!$)d38n)Xs s 4%7 %1J1J %&/d/r&   