
    i              	          d dl Z d dlmZ d dl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mZ d d	lmZ  e            Z e j        e          Z G d
 de
          Z G d de
          Ze                    ddgee         dd           edd           ed d           fdededee         fd            Ze                    ddgee         dd          dedee         fd            Ze                     ddgeedz           d d          d!efd"            Z!e                     d#dged         d$d          d!eded         fd%            Z"dS )&    N)text)OptionalLiteral)	APIRouterQuery)	BaseModel)HTTPException)ResponseWrapper)save_conversationsave_conversation_feedback)get_async_dbc                   x    e Zd ZU dZee         ed<   dZee         ed<   dZee         ed<   dZ	ee         ed<   dS )SaveConversationRequestNconversation_idquestionanswercustomer_id)
__name__
__module____qualname__r   r   str__annotations__r   r   r        =/var/www/html/ls-ibe-bot-apis/app/controllers/conversation.pyr   r      sb         %)OXc])))"Hhsm""" FHSM   !%K#%%%%%r   r   c                   0    e Zd ZU eed<   ed         ed<   dS )RateConversationRequestr   )updownfeedbackN)r   r   r   r   r   r   r   r   r   r   r      s/         l######r   r   z/conversationsConversationszGet all conversations   )tagsresponse_modelsummarystatus_code
   )le)gelimitoffsetreturnc                   K   t                      4 d {V }|                    t          d          | |d           d {V }|                                }|s&t	          dddg i          cd d d           d {V  S 	 d d d           d {V  n# 1 d {V swxY w Y   d |D             }t	          ddd|i          S )	Na;  
                SELECT 
                    id AS conversation_id,
                    first_message,
                    created_at,
                    updated_at
                FROM conversations
                ORDER BY updated_at DESC
                LIMIT :limit
                OFFSET :offset;
            )r*   r+   TzNo conversations foundconversationsstatusmessagedatac                     g | ]S}|j         |j        |j        r|j                                        nd |j        r|j                                        nd dTS )N)r   first_message
created_at
updated_at)r   r4   r5   	isoformatr6   .0rows     r   
<listcomp>z)get_all_conversations.<locals>.<listcomp>=   sw          #2 .8;P#.22444D8;P#.22444D		
 	
  r   OK)r   executer   fetchallr
   )r*   r+   dbresultrowsr.   s         r   get_all_conversationsrB      s      ~~       zz 
 
 
 v..
 
 
 
 
 
 
 
    	"0%r*  '             $	%                          2    M }-   s   AB
B Bz /conversations/{conversation_id}ConversationzGet conversationr   c                   K   t                      4 d {V }|                    t          d          d| i           d {V }|                                }|st	          dd          	 d d d           d {V  n# 1 d {V swxY w Y   |d         j        }t          d |D             d           }d |D             }|||d	}t          d
d|          S )Na  
                SELECT 
                    c.id AS conversation_id,
                    m.customer_id,
                    m.message,
                    m.role,
                    m.created_at,
                    m.updated_at
                FROM conversations c
                LEFT JOIN messages m 
                    ON m.conversation_id = c.id
                WHERE c.id = :id
                ORDER BY m.created_at ASC;
            idi  zConversation not foundr&   detailr   c              3   2   K   | ]}|j         	|j         V  d S )N)r   )r9   rs     r   	<genexpr>z#get_conversation.<locals>.<genexpr>p   s+      EE!q}EEEEEEEr   c                 R    g | ]$}|j         	|j        |j         |j        |j        d%S )N)roler   r5   r6   )r1   rL   r5   r6   r8   s     r   r;   z$get_conversation.<locals>.<listcomp>s   sH        3;2 HK..		
 	
 322r   )r   r   messagesTr<   r/   )r   r=   r   r>   r	   r   nextr
   )r   r?   r@   rA   conv_idr   rM   response_datas           r   get_conversationrQ   N   s      ~~ R R R R R R Rzz    ?#
 
 
 
 
 
 
 
$    	RC8PQQQQQ-R R R R R R R R R R R R R R R R R R R R R R R R R R R2 1g%GEEtEEEtLLK    H #" M $=IIIIs   AA==
B
Bz/save/conversationzSave conversationargsc                    K   t          | j        | j        | j        | j                   d {V | _        t          dd| j                  S )NTr<   r/   )r   r   r   r   r   r
   )rR   s    r   save_conversationsrT      sU       "343GX\Xceieu!v!vvvvvvvD$4;OPPPPr   z/rate/conversationz3Save thumbs up/down feedback for latest AI responsec                    K   t          | j        | j                   d {V }|st          dd          t	          ddd           S )N)r   r    i  zFailed to save feedbackrF   Tr<   r/   )r   r   r    r	   r
   )rR   saveds     r   rate_conversationrW      sy       -,        E  O4MNNNN$4@@@@r   )#logging
sqlalchemyr   typingr   r   fastapir   r   pydanticr   app.exceptions.httpr	   app.schemasr
   app.utils.store_conversationr   r   app.models.connectionr   router	getLoggerr   loggerr   r   getdictintrB   r   rQ   postrT   rW   r   r   r   <module>rh      s          $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $       - - - - - - ' ' ' ' ' ' V V V V V V V V . . . . . .			8	$	$& & & & &i & & &$ $ $ $ $i $ $ $ 
	"4(#    .3U2"-=-=-=UUSTYZ^^^ ( (s (s (`opt`u ( ( ( (V &
	"4(   .JC .JOD4I .J .J .J .J` 
	"3:.   Q#: Q Q Q Q 
	"4(A   	A"9 	Aod>S 	A 	A 	A 	A 	A 	Ar   