o
    i                     @   s   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mZmZmZ ddlmZmZmZ G d	d
 d
ejZG dd dejZG dd dejZdS )    )viewsetsstatus)action)Response)IsAuthenticated)get_object_or_404   )ConversationMemoryConversationInsight)ConversationSerializerMemorySerializerConversationInsightSerializerc                   @   sR   e Zd ZdZg Zdd Zdd Zeddgddd
dZeddgddd Z	d	S )ConversationViewSetzy
    ViewSet for viewing conversation data.
    
    Provides read-only access to conversation records and analysis.
    c                 C      t j ddS )z$Return all conversations for testingcallcall__senior)r	   objectsallselect_relatedself r   ,/var/www/html/philips/conversations/views.pyget_queryset      z ConversationViewSet.get_querysetc                 C      t S N)r   r   r   r   r   get_serializer_class      z(ConversationViewSet.get_serializer_classTgetdetailmethodsNc                 C   s6   |   }|j d}t|dd}t|j|jdS )zs
        Get memories from a specific conversation.
        
        GET /api/conversations/{id}/memories/
        z-importance_scoreTmany)conversation_idmemories)
get_objectr'   r   order_byr   r   iddata)r   requestpkconversationr'   
serializerr   r   r   r'      s   zConversationViewSet.memoriesFc                 C   s  |   }| }|jdd }|jdd }|jddd }|jdd }|jdd }|jd	d }	g }
|D ]}|
|j q=i }|
D ]}||d
d ||< qJt| dd dddd }|ddd }t	|ddj
}t||||d|||	d||dS )z{
        Get conversation analytics for the authenticated user.
        
        GET /api/conversations/analytics/
        g?)sentiment_score__gtg)sentiment_score__lt)sentiment_score__gtesentiment_score__ltehigh)engagement_levelmediumlowr   r   c                 S   s   | d S )Nr   r   )xr   r   r   <lambda>P   s    z/ConversationViewSet.analytics.<locals>.<lambda>T)keyreverseN   z-created_atr$   )positivenegativeneutral)r4   r6   r7   )total_conversationssentiment_breakdownengagement_breakdownmost_discussed_topicsrecent_conversations)r   countfilterextendtopics_discussedr    sorteditemsr)   r   r+   r   )r   r,   conversationsr@   positive_conversationsnegative_conversationsneutral_conversationshigh_engagementmedium_engagementlow_engagement
all_topicsconvtopic_countstopicrC   rD   recent_datar   r   r   	analytics+   sR   zConversationViewSet.analyticsr   )
__name__
__module____qualname____doc__permission_classesr   r   r   r'   rW   r   r   r   r   r      s    r   c                   @   sP   e Zd ZdZg Zdd Zdd Zeddgdd	d
 Zeddgddd Z	dS )MemoryViewSetz`
    ViewSet for viewing memory data.
    
    Provides read-only access to memory records.
    c                 C   r   )zReturn all memories for testingseniorr.   )r
   r   r   r   r   r   r   r   r   r   r   zMemoryViewSet.get_querysetc                 C   r   r   )r   r   r   r   r   r   v   r   z"MemoryViewSet.get_serializer_classFr    r!   c                 C   V   |j d}|stdditjdS |  j|d}| j|dd}t||j|	 dS )	zg
        Get memories filtered by type.
        
        GET /api/memories/by_type/?type=story
        typeerrorType parameter is requiredr   )memory_typeTr$   )r`   r'   rE   
query_paramsr    r   r   HTTP_400_BAD_REQUESTr   rF   get_serializerr+   rE   )r   r,   rd   r'   r/   r   r   r   by_typey      zMemoryViewSet.by_typec                 C   r_   )	zo
        Get memories for a specific senior.
        
        GET /api/memories/by_senior/?senior_id=1
        	senior_idra   zSenior ID parameter is requiredrc   )rk   Tr$   )rk   r'   rE   re   )r   r,   rk   r'   r/   r   r   r   	by_senior   rj   zMemoryViewSet.by_seniorN)
rX   rY   rZ   r[   r\   r   r   r   ri   rl   r   r   r   r   r]   i   s    
r]   c                   @   s:   e Zd ZdZg Zdd Zdd Zeddgdd	d
 ZdS )ConversationInsightViewSetzh
    ViewSet for viewing conversation insights.
    
    Provides read-only access to insight data.
    c                 C   s   t j dS )zReturn all insights for testingr^   )r   r   r   r   r   r   r   r   r      s   z'ConversationInsightViewSet.get_querysetc                 C   r   r   )r   r   r   r   r   r      r   z/ConversationInsightViewSet.get_serializer_classFr    r!   c                 C   r_   )	zl
        Get insights filtered by type.
        
        GET /api/insights/by_type/?type=mood_trend
        r`   ra   rb   rc   )insight_typeTr$   )r`   insightsrE   re   )r   r,   rn   ro   r/   r   r   r   ri      rj   z"ConversationInsightViewSet.by_typeN)	rX   rY   rZ   r[   r\   r   r   r   ri   r   r   r   r   rm      s    rm   N)rest_frameworkr   r   rest_framework.decoratorsr   rest_framework.responser   rest_framework.permissionsr   django.shortcutsr   modelsr	   r
   r   serializersr   r   r   ReadOnlyModelViewSetr   r]   rm   r   r   r   r   <module>   s    ^?