
    lh7                        d dl mZmZmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlmZ d d	lmZmZ d d
lmZ d dlm Z m!Z!m"Z" d dl#Z#d dl$m%Z% d dl&m'Z'm(Z( d dlm!Z!m)Z)m*Z*m+Z+m,Z, d dl-Z-d dlm.Z.m/Z/  eddg      Z0dZ1ddiZ2g dZ3e0ji                  d      edede.defd              Z5e0ji                  d      de6defd       Z7e0ji                  d      de6defd       Z7y)     )	APIRouterstatusHTTPException)handles_authenticationuser_dependency)db_dependency)AuthenticationError)AgentCreateAgentResponseSampleAgentSchema	AgentBaseAgentCreateSampleAgentCreateResponseAgentLiveResponseAgentListResponse)agent_repositoryN)Retell)PhoneNumberCreatePhoneNumberResponse)List)SampleAgentCompanyUsers)UserRepository)BusinessTypeRegisterStep)r   ServiceWeeklyScheduleAgentBooking)
BotRequestBookingRequestz/api/agentsAgents)prefixtagszhttps://api.vapi.ai/Authorizationz+Bearer dd612bd6-92e6-4bb4-a0d1-37152cdb9ea3)MondayTuesday	WednesdayThursdayFridaySaturdaySundayz/createAgentuserreqdbc                   K   | t         |j                  t              j                  t        j                  |j
                  k(        j                         }|st        dd      |j                  t              j                  t        j
                  |j
                  k(        j                         }dj                  |D cg c]  }d|j                   d|j                   d! c}      xs d}|j                  t              j                  t        j
                  |j
                  k(        j                         }g }|D ]  }	|	j                  d	k(  r&|j                  t         |	j"                      d
       8|j                  t         |	j"                      d|	j$                  j'                  d       d|	j(                  j'                  d               dj                  |      }
d|j                   d|j*                   d|j,                   d|j.                  xs d d|j0                   d|j2                   d| d|
 d|j4                   d}|j6                  |j8                  dd|j:                  d|dgdddddd d!id d!id d!id d!id"g d#d$d%gd&|j<                  |j>                  |j@                  d'd(d)d*d+|jB                  d,	}tE        jF                  tH         d-|tJ        .      }|jL                  d/k7  r%t        |jL                  |jO                               |jO                         }tQ        d0||jL                         	 tS        |j
                  |jU                  d1      |jU                  d2|j6                        |j8                  |j:                  ||j<                  |j>                  |j@                  |jB                  |3      }|jW                  |       |jY                          |j[                  |       ||j                  d7S c c}w # t\        $ rC}|j_                          tQ        d4ta        |             t        d5d6ta        |             d }~ww xY ww)8Ni  zCompany not foundstatus_codedetail
z- z ($)zNo services listed.   z: Closedz: z%H:%Mz - z2
# Company Info
You are a virtual assistant for **z**.

Business type: z  
Address: z,  z  
Official phone: z0  

# Services
Here are the services offered:  
z,

# Weekly Schedule
The office hours are:  
z

# Assistant Instructions
vapi)voiceIdprovidersystem)rolecontentopenaicreate_bookingz0https://pm.backend.fluten.xyz/api/agents/bookingzCapture booking detailsobjecttypestring)customer_namephoneappointment_timeservice)rD   rE   rF   )rB   
propertiesrequired)name	serverUrldescription
parameters)modelmessagesr;   	functionsznova-2endeepgram)rN   languager;   office)	rJ   voicerN   firstMessagevoicemailMessageendCallMessagetranscriberbackgroundSoundrK   z
/assistant)jsonheaders   zVAPI Response:idrJ   )
company_idbot_idrJ   voice_idrN   system_promptfirst_messagevoicemail_messageend_call_messagerK   raw_responsez	DB Error:i  
DB Error: )agent	db_record)1r	   queryr   filterr^   r_   firstr   r   alljoinrJ   pricer   
is_day_offappenddaysday_of_week
start_timestrftimeend_timebusiness_typeaddress1address2zip_codeofficial_phonebot_system_promptbot_namebot_voiceId	bot_modelbot_firstMessagebot_voicemailMessagebot_endCallMessagerK   requestspostVAPI_BASE_URLr\   r3   r[   printr   getaddcommitrefresh	Exceptionrollbackstr)r.   r/   r0   companyservicessservices_text	schedulesschedule_textschschedule_strenriched_promptpayloadresponse	resp_datarh   es                    B/var/www/html/phonemate/phone_mate_backend/app/api/agent/router.pycreate_agentr     s+     |!! hhw&&wzzS^^'CDJJLG4GHH xx ''(:(:cnn(LMQQSH		x@x!Rxs177)1-x@A 	!   	 	))S^^;	<	 
 M>>Q  D$9#:(!CD  ()CNN,C,CG,L+MSQTQ]Q]QfQfgnQoPpq	  99]+L##*<<. 1%%& '



	2g..4"5R8H8H7I J''( )       #O,  __&A]]"*GH  -!S#< (.4h-?&,h%7170B(.'9	' %S	#		
, ,,4400!)tT#]]=GD }}/$7GH s"(<(<X]]_UUI	
Ix';';<K~~==&vs||4__--)..!66 33mm"
 	u
		


5 UXX66[ AP  K
k3q6"jQ4IJJKs8   CQ&$P'I$Q&B7P Q&	Q# >QQ##Q&z/bookingc                    K   	 | j                  di       }t        d       t        | d       t        |d       d| dS # t        $ r.}|j                          t	        ddt        |       	      d }~ww xY ww)
N	argumentszbookings UrlFullRequestargsTsuccessr/     rg   r2   )r   r   r   r   r   r   )r/   r0   r   r   s       r   r@   r@     sw     Kww{B'nc=!dF   ,, K
jQ4IJJKs%   A89> A8	A5)A00A55A8z/vapi-webhookc                    K   	 t        d       t        | d       d| dS # t        $ r.}|j                          t        ddt	        |             d }~ww xY ww)NzVapi-webhookr   Tr   r   rg   r2   )r   r   r   r   r   )r/   r0   r   s      r   r@   r@     s]     
Knc=!  ,, K
jQ4IJJKs$   A  A	A)AAA)8fastapir   r   r   utils.auth_dependencyr   r   utils.databaser   utils.exceptionsr	   schemas.agentr
   r   r   r   r   r   r   r   repositoriesr   httpxosretellr   schemas.phone_numberr   r   typingr   modelsr   r   r   r[   repositories.user_repositoryr   utils.constantsr   r   r   r   r   r    r   r!   r"   routerr   r\   rr   r   r   dictr@        r   <module>r      s   4 4 I ( 0	 	 	 *  	  G  . .  7 6 C C  4 
-xj	9z 'I
JU ^y7_ y7: y7= y7  y7x ZKd K K K> _Kd K K Kr   