o
    ppi$                     @   s   d dl mZmZ d dlmZ d dlmZ G dd dejZG dd dejZ	G dd	 d	ejZ
G d
d dejZG dd dejZdS )    )modelstransaction)User)Seniorc                   @   s^   e Zd ZdZejddddZejdddZejdd	d
Z	ej
ddZG dd dZdd ZdS )VapiAssistantzBModel for storing VAPI assistant IDs that can be managed by adminsd   TzDThe VAPI assistant ID (e.g., '7c0ac894-0739-48b9-a974-6131e5217d18'))
max_lengthunique	help_text   zGHuman-readable name for this assistant (e.g., 'German Voice Assistant')r   r
   z7Whether this assistant is available for users to selectdefaultr
   auto_now_addc                   @      e Zd ZdgZdZdZdS )zVapiAssistant.Meta-created_atzVAPI AssistantzVAPI AssistantsN__name__
__module____qualname__orderingverbose_nameverbose_name_plural r   r   %/var/www/html/philips/calls/models.pyMeta       r   c                 C   s    | j rdnd}| j d| dS )NActiveInactive ())	is_activename)selfstatusr   r   r   __str__   s   zVapiAssistant.__str__N)r   r   r   __doc__r   	CharFieldassistant_idr#   BooleanFieldr"   DateTimeField
created_atr   r&   r   r   r   r   r      s$    r   c                   @   sZ   e Zd ZdZejeejdddZej	e
ejdddZejddZG d	d
 d
Zdd ZdS )UserAssistantPreferencez1Model for storing user's preferred VAPI assistantassistant_preferencez User who selected this assistant	on_deleterelated_namer
   user_preferenceszThe selected VAPI assistantTauto_nowc                   @   s   e Zd ZdZdZdS )zUserAssistantPreference.MetazUser Assistant PreferencezUser Assistant PreferencesN)r   r   r   r   r   r   r   r   r   r   3   s    r   c                 C   s   | j j d| jj S )Nz -> )userusername	assistantr#   r$   r   r   r   r&   7   s   zUserAssistantPreference.__str__N)r   r   r   r'   r   OneToOneFieldr   CASCADEr5   
ForeignKeyr   r7   r+   
updated_atr   r&   r   r   r   r   r-   "   s"    r-   c                       s   e Zd ZdZejdddZejdddZejdd	Z	ej
dd
dZej
dddZejddZejddZejeejdddddZG dd dZdd Z fddZ  ZS )
CallPromptzDModel for storing reusable call prompts that can be managed in adminr   zNA descriptive name for this prompt (e.g., 'General Check-in', 'Birthday Call')r   Tz.Description of when and how to use this promptblankr
   zThe prompt template. Use {senior_name}, {senior_age}, {interests}, {health_notes}, {call_purpose}, and {conversation_context} as placeholders.r
   z(Whether this prompt is available for user   FzZUse this prompt as the default when no prompt is selected. Only one prompt can be default.r   r3   created_promptszUser who created this promptr0   nullr?   r1   r
   c                   @   r   )zCallPrompt.Metar   zCall PromptzCall PromptsNr   r   r   r   r   r   \   r   r   c                 C   s.   | j rdnd}| j d| jrdnd d| S )Nz
 (Default) r    r   r   r!   )
is_defaultr#   r"   )r$   default_textr   r   r   r&   c   s    zCallPrompt.__str__c                    sL   | j r| j}tjjdd}|r|j|d}|jdd t j|i | d S )NT)rE   )pkF)	rE   rG   r=   objectsfilterexcludeupdatesupersave)r$   argskwargs
current_pkqueryset	__class__r   r   rM   g   s   zCallPrompt.save)r   r   r   r'   r   r(   r#   	TextFielddescriptionprompt_templater*   r"   rE   r+   r,   r<   r;   r   SET_NULL
created_byr   r&   rM   __classcell__r   r   rR   r   r=   ;   sD    	r=   c                   @   s|  e Zd ZdZg dZejeejdddZ	ej
dddddd	Zej
d
edddZejddddZejddddZejddddZej
dddddZej
dddddZej
dddddZejeejdddddZejeejdddddZejdddZej
ddddZejdd Zejdd!Zejeejddd"d#Z ej!d$d%d&Z"G d'd( d(Z#d)d* Z$e%d+d, Z&e%d-d. Z'e%d/d0 Z(d1S )2Callz*Model representing a call made to a senior))	initiated	Initiated)ringingRinging)answeredAnswered)	completed	Completed)failedFailed)busyBusy)	no_answerz	No Answer)	cancelled	Cancelledcallsz The senior who received the callr/   r   TzTwilio Call SID)r   r	   rC   r?   r
      r[   zStatus of the call)r   choicesr   r
   zCall duration in seconds)rC   r?   r
   zWhen the call actually startedzWhen the call endedzVapi.ai call ID)r   rC   r?   r
   z*Vapi.ai assistant ID created for this call2   zVapi.ai call statusz%The VAPI assistant used for this callrB   z*The prompt template selected for this callzNThe AI prompt used for this call (includes memory context and prompt template)r>   r   general_checkinz>Purpose of the call (e.g., 'general_checkin', 'birthday_call'))r   r   r
   r   r3   zUser who initiated the call)r0   rC   r?   r
   FzBWhether the time limit enforcement (goodbye message) was triggeredr   c                   @      e Zd ZdgZdS )z	Call.Metar   Nr   r   r   r   r   r   r   r   r          
r   c                 C   s&   d| j j d| j d| jd dS )NzCall to  - r    z%Y-%m-%d %H:%Mr!   )seniorr#   twilio_statusr,   strftimer8   r   r   r   r&      s   &zCall.__str__c                 C   s   | j S )z%Return the current status of the callrt   r8   r   r   r   r%      s   zCall.statusc                 C   s
   | j dkS )z&Return True if the call was successfulra   rv   r8   r   r   r   is_successful   s   
zCall.is_successfulc                 C   s.   | j sdS | j d }| j d }| d| dS )z Return formatted duration stringzN/A<   zm s)duration)r$   minutessecondsr   r   r   formatted_duration   s
   

zCall.formatted_durationN))r   r   r   r'   STATUS_CHOICESr   r;   r   r:   rs   r(   
twilio_sidrt   PositiveIntegerFieldrz   r+   call_start_timecall_end_timevapi_call_idvapi_assistant_idvapi_statusr   rW   r7   r=   promptrT   	ai_promptcall_purposer,   r<   r   initiated_byr*   is_limit_enforcedr   r&   propertyr%   rw   r}   r   r   r   r   rZ   z   s    


rZ   c                   @   sx   e Zd ZdZg dZejeejdddZ	ej
deddZejd	d
ZejeddZejddZG dd dZdd ZdS )CallLogz0Model for logging call events and status changes))r[   zCall Initiated)twilio_createdzTwilio Call Created)vapi_startedzVapi Call Started)r_   zCall Answered)ra   zCall Completed)rc   zCall Failed)status_changedzStatus Changed)webhook_receivedzWebhook Receivedlogsz"The call this log entry belongs tor/   rk   zType of event logged)r   rl   r
   zLog messager@   z$Additional data related to the eventr   Tr   c                   @   ro   )zCallLog.Metar   Nrp   r   r   r   r   r   -  rq   r   c                 C   s"   | j  d| j d| jd d  S )Nrr   z: rm   )call
event_typemessager8   r   r   r   r&   0  s   "zCallLog.__str__N)r   r   r   r'   EVENT_TYPESr   r;   rZ   r:   r   r(   r   rT   r   	JSONFielddictdatar+   r,   r   r&   r   r   r   r   r   
  s,    r   N)	django.dbr   r   django.contrib.auth.modelsr   seniors.modelsr   Modelr   r-   r=   rZ   r   r   r   r   r   <module>   s    ? 