
    eh                        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 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mZ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' d dl(Z(d dl)m*Z*  eddg      Z+ ejX                  d      Z-e+j]                  dej^                        dedefd       Z0y)    )	APIRouterstatusRequest)handles_authenticationuser_dependency)db_dependency)AuthenticationError)RetellWebhookResponse)agent_repositoryN)Retell)PhoneNumberCreatePhoneNumberResponse)List)SampleAgentCompanyUsersPhoneNumberr   AgentWeeklySchedule)UserRepository)BusinessTypeRegisterStep)datetime	timedeltatimezone)JSONResponsez/api/retellr   )prefixtagsRETELL_API_KEYz/inbound-webhook)status_coderequestdbc                 B  K   | j                          d {   }t        d|       |j                  di       }|j                  d      }|st        i d      S |j	                  t
              j                  t
        j                  |k(        j                         }|st        i d      S |j	                  t              j                  t        j                  |j                  k(        j                         }|j	                  t              j                  t        j                  |j                  k(        j                         }|j	                  t              j                  t        j                  |j                  k(        j                         }t!        j"                  d      }	t%        j&                  |	      }
|
j)                         }|dz   dz  |
j+                         }|
j-                         j/                         d	z  }t1        fd
|D        d       }d}|r0|j2                  s$|j4                  |cxk  r|j6                  k  rn nd}dt9        |      j;                         |
j=                  d      t9        |      dd|rdndidi}t        |d      S 7 w)Nu   📞 Incoming Retell webhook:call_inbound	to_number   )contentr    zAsia/Karachi      i  c              3   B   K   | ]  }|j                   k(  s|  y w)N)day_of_week).0sdb_days     C/var/www/html/phonemate/phone_mate_backend/app/api/retell/router.py	<genexpr>z"inbound_webhook.<locals>.<genexpr>G   s     R&6!--6:Q1&6s   TFz%H:%M)is_off_hourscurrent_timetimezone_offsetbot_switch_reason	off_hoursbusiness_hours)dynamic_variablesmetadata)jsonprintgetr   queryr   filterphone_numberfirstr   idagent_idr   
company_idr   allpytzr   r   nowweekdaytime	utcoffsettotal_secondsnext
is_day_off
start_timeend_timestrlowerstrftime)r!   r"   bodyr$   r%   phone_entryagent_entrycompany_entryweekly_schedulesr   rE   current_dayr2   r3   today_scheduler1   response_bodyr.   s                    @r/   inbound_webhookrY      s8    D	
)4088NB/L  -IBC88 ((;'..  I-eg  BC88 ((5/((K(((eg  HHW%,,

k,,,eg  	 	))]-=-==	>	  }}^,H
,,x
 C++-KAo"F88:Lmmo335<O R&6RTXYNL((((LSN<S<SS$ 	 #L 1 7 7 9 #W 5#&#7" $L[FV	
M 3??M  s   JJJJ)1fastapir   r   r   utils.auth_dependencyr   r   utils.databaser   utils.exceptionsr	   schemas.retellr
   repositoriesr   httpxosretellr   schemas.phone_numberr   r   typingr   modelsr   r   r   r   r   r   r9   repositories.user_repositoryr   utils.constantsr   r   r   r   r   rD   fastapi.responsesr   routergetenvr   postHTTP_200_OKrY        r/   <module>ro      s    . . I ( 0 2 )  	  G  \ \ \  7 6 2 2  *	-xj	9+, V-?-?@G@7 G@ G@ AG@rn   