
    i                     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
  edg       eeg      d               Zy	)
    )api_viewpermission_classes)Responsestatus)AllowAny   )conversationPOSTc                 <   | j                   j                  d      }t        d|       |st        ddit        j
                        S 	 t        j                  j                  |      j                  d      j                         }|s$t        dd	d
dd
d
g g dt        j                        S g }|j                  r|j                  d|j                          g }g }|j                  r|j                  j                  dg       }|j                  j                  dg       }|j                  j                  dg       }|r@t        |t               rdj#                  |      n
t%        |      }|j                  d|        |r@t        |t               rdj#                  |      n
t%        |      }|j                  d|        |r@t        |t               rdj#                  |      n
t%        |      }	|j                  d|	        |j&                  r|j                  d|j&                          |j(                  r|j                  d|j(                          |rdj#                  |      nd}
t        |
d|j*                  j-                         |j                  xs d|j&                  |j(                  |||j.                  |j                  d
t        j                        S # t0        $ r3}t        ddt%        |       it        j2                        cY d
}~S d
}~ww xY w)aB  
    Endpoint for Vapi to fetch the last conversation for a phone number.
    
    Expected request body:
    {
        "phone_number": "+49123456789"
    }
    
    Returns:
    {
        "context": "Summary of last conversation...",
        "has_previous_conversation": true,
        "last_conversation_date": "2024-01-15T10:30:00Z",
        "summary": "...",
        "sentiment": "positive",
        "outcome": "successful",
        "topics": ["topic1", "topic2"],  # from meta_data if available
        "emotions": ["happy", "calm"]  # from meta_data if available
    }
    phone_numberz=========================errorzphone_number is requiredr   )r   z-started_atz0This is the first conversation with this person.FN )contexthas_previous_conversationlast_conversation_datesummary	sentimentoutcometopicsemotionszSummary of last conversation: r   r   
key_pointsz, zTopics discussed: zEmotions detected: z; zKey points: zSentiment: z	Outcome: 
z6Previous conversation exists but no details available.T)
r   r   r   r   r   r   r   r   call_idr   zAn error occurred: )datagetprintr   r   HTTP_400_BAD_REQUESTr
   objectsfilterorder_byfirstHTTP_200_OKr   append	meta_data
isinstancelistjoinstrr   r   
started_at	isoformatr   	ExceptionHTTP_500_INTERNAL_SERVER_ERROR)requestr   last_conversationcontext_partsr   r   r   
topics_stremotions_strkey_points_strr   es               !/var/www/html/jeff/calls/views.pyget_last_conversationr6      s   . <<##N3L	
%l301..
 	
E
(0077% 8 

(=
!%%' 	 !M-2*.!	 ((	* 	* $$  #ABSB[B[A\!]^ &&&0044XrBF(2266z2FH*4488rJJ2<VT2JTYYv.PSTZP[
$$'9*%FG6@46Ptyy2VYZbVc$$':<.%IJ:DZQU:V:!6\_`j\k$$|N3C%DE&&  ;/@/J/J.K!LM$$  9->-F-F,G!HI.;$))M*Ay)-&7&B&B&L&L&N(006B*44(00 (00'..
 $$& 	&  
+CF84588
 	

s&   A"K +H3K 	L((LLLN)rest_framework.decoratorsr   r   rest_framework.responser   rest_frameworkr   rest_framework.permissionsr   modelsr
   r6        r5   <module>r>      s@    B , ! /   
6(XJd
   d
r=   