
    ;iC                     |    d dl Z d dlmZmZmZ d dlmZ d dlmZ d dl	m
Z
  e j                  e      Z G d d      Zy)    N)DictAnyOptional)CompanyBotSettings)BotType)AppointmentRepositoryc                       e Zd ZdZedeeef   deeef   fd       Zede	e   fd       Z
edeeef   deeef   fd       Zedeeef   deeef   fd	       Zed
efd       Zy)AppointmentServicez
    Service layer that handles webhook payload normalization and uses the repository.
    Services accept validated data (e.g. BookAppointmentInputSerializer output).
    webhook_payloadreturnc                     | j                  d      xs i }| j                  d      xs i }|j                  d      xs i }||j                  d      |j                  d      dS )zD
        Normalize webhook payload into predictable fields.
        argscalltelephony_identifiertwilio_call_sid	to_number)r   r   r   )get)r   r   	call_infotel_identifiers       W/var/www/html/dp2/backend_v2.1/server/apps/appointments/services/appointment_service.py_extract_common_webhook_dataz/AppointmentService._extract_common_webhook_data   sl    
 ""6*0b#''/52	#--(>?E2 -112CD"{3
 	
    phone_numberc                     | st         j                  d       yt        j                  j	                  |       j                         }|st         j                  d|        y|j                  S )aL  
        Return Company instance matched from CompanyBotSettings or None if not found.
        This is defensive: original code used .first().company and would AttributeError
        if missing; returning None prevents server 500s. If your business rules require
        raising on missing setting, change this accordingly.
        z6No phone number provided in webhook to lookup company.N)r   z*CompanyBotSettings not found for phone: %s)loggerwarningr   objectsfilterfirstcompany)r   cbss     r   _get_company_from_phonez*AppointmentService._get_company_from_phone    sX     NNST ((//\/JPPRNNGV{{r   validated_payloadc                 "   t         j                  |       }|d   xs i }t         j                  |j                  d            }|j                  d      |j                  d      |j                  d      |j                  d      |dt        j
                  j                  d}t        j                  |      }t        j                  d	|j                  |j                  d      |j                  d
      t        |dd             d|j                  dS )z
        Book an appointment for sales bot using normalized payload.
        Returns a small dict with created flag and appointment id.
        r   r   namescheduleoffTimer   N)r%   scheduled_dateofftimer   r    r   bot_typezISales appointment created (id=%s) name=%s scheduled_date=%s company_id=%sr(   idTcreatedappointment_id)r
   r   r"   r   r   	SALES_BOTvaluer   create_appointmentr   infor+   getattrr#   datar   r    payloadappointments         r   book_sales_bot_appointmentz-AppointmentService.book_sales_bot_appointment2   s     ">>?PQF|!r$<<TXXk=RS HHV$"hhz2xx	*#xx(9:))//
 ,>>wGWNNKKKK()GT4(	
  ;>>BBr   c                 "   t         j                  |       }|d   xs i }t         j                  |j                  d            }|j                  d      |j                  d      |j                  d      |j                  d      |dt        j
                  j                  d}t        j                  |      }t        j                  d	|j                  |j                  d      |j                  d
      t        |dd             d|j                  dS )zO
        Book an appointment for service bot using normalized payload.
        r   r   r%   r   appointment_datetimer   N)r%   appointment_phoner(   r   r    r   r*   zKService appointment created (id=%s) name=%s scheduled_date=%s company_id=%sr(   r+   Tr,   )r
   r   r"   r   r   SERVICE_BOTr0   r   r1   r   r2   r+   r3   r4   s         r   book_service_bot_appointmentz/AppointmentService.book_service_bot_appointmentR   s    
 ">>?PQF|!r$<<TXXk=RS HHV$!%.!9"hh'=>#xx(9:++11
 ,>>wGYNNKKKK()GT4(	
  ;>>BBr   r.   c                 ,    t        j                  |       S )z4
        Retrieve an appointment by its ID.
        )r   	get_by_id)r.   s    r   get_appointment_by_idz(AppointmentService.get_appointment_by_idq   s    
 %..~>>r   N)__name__
__module____qualname____doc__staticmethodr   strr   r   r   r"   r8   r=   intr@    r   r   r
   r
      s    
 
d38n 
cSVh 
 
 hsm  " Cd38n CcSVh C C> CS#X C4PSUXPX> C C< ?c ? ?r   r
   )loggingtypingr   r   r   apps.companies.modelsr   apps.calls.constantsr   5apps.appointments.repositories.appointment_repositoryr   	getLoggerrA   r   r
   rH   r   r   <module>rO      s5     & & 4 ( W			8	$k? k?r   