
    i              
           d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	  ej
        e          ZdefdZddededz  d	edz  d
edz  fdZdeded         defdZdS )    N)text)Literal)get_async_db)HTTPExceptionquestionc                   K   	 t                      4 d {V }t          t          j                              }|                    t          d          || d           d {V  |                                 d {V  d d d           d {V  n# 1 d {V swxY w Y   |S # t          $ r(}t          	                    d|            Y d }~d S d }~ww xY w)Nz@INSERT INTO conversations (id, first_message) VALUES (:id, :msg))idmsgzError creating conversation: )
r   struuiduuid4executer   commit	Exceptionlogerror)r   dbconversation_ides       =/var/www/html/ls-ibe-bot-apis/app/utils/store_conversation.pycreate_conversationr   %   s     >> 	 	 	 	 	 	 	R!$*,,//O**WXX&x88        
 ))++	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    		5!55666ttttts;   B$ A'B?B$ 
BB$ BB$ $
C.CCr   answercustomer_idc                 <  K   	 | st          |           d {V } g }|r|                    | |d|d           |r|                    | |d|d           t          |          dk    r| S t                      4 d {V }t	          d          }|                    ||           d {V  |                                 d {V  d d d           d {V  n# 1 d {V swxY w Y   | S # t          $ r(}t          	                    d|            Y d }~d S d }~ww xY w)Nhuman)r   messageroler   air   z
                INSERT INTO messages (conversation_id, message, role, customer_id)
                VALUES (:conversation_id, :message, :role, :customer_id)
            zError saving conversation: )
r   appendlenr   r   r   r   r   r   r   )r   r   r   r   messagesr   queryr   s           r   save_conversationr#   7   s     ' 	B$7$A$AAAAAAAO 	OO#2#*	      	OO#2!*	     x==A"">> 	 	 	 	 	 	 	R   E
 **UH---------))++	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	    		333444tttttsC   A%C) *C) >ACC) 
C  C) #C $C) )
D3DDfeedback)updownreturnc                 ^  K   t                      4 d{V }|                    t          d          d| i           d{V }|                                st	          dd          |                    t          d          d| i           d{V }|                                }|st	          dd          |d	         }|                    t          d
          ||d           d{V  |                                 d{V  	 ddd          d{V  dS # 1 d{V swxY w Y   dS )z
    Update feedback directly in `messages` table.

    - feedback: "up" -> 1, "down" -> 0
    - Targets latest AI message in the conversation
    - Raises HTTPException if conversation or message not found
    Nz
                SELECT id
                FROM conversations
                WHERE id = :conversation_id
                AND is_deleted = 0
                LIMIT 1;
            r   i  zConversation not found)status_codedetailz
                SELECT id
                FROM messages
                WHERE conversation_id = :conversation_id
                AND role = 'ai'
                ORDER BY created_at DESC
                LIMIT 1;
            zNo AI message foundr   z~
                UPDATE messages
                SET feedback = :feedback
                WHERE id = :message_id;
            )r$   
message_idT)r   r   r   fetchoner   r   )r   r$   r   conv_result
msg_resultmsg_rowr+   s          r   save_conversation_feedbackr0   b   sK      ~~ / / / / / / /JJ    0	
 	
 	
 	
 	
 	
 	
 	
 ##%% 	RC8PQQQQ ::    0

 

 

 

 

 

 

 


 %%'' 	OC8MNNNNQZ
jj    %( 

 

 
	
 
	
 
	
 
	
 
	
 
	
 
	
 iikk_/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / /s   C2D
D&)D&)NNN)r   logging
sqlalchemyr   typingr   app.models.connectionr   app.exceptions.httpr   	getLogger__name__r   r   r   r#   boolr0        r   <module>r;      s                 . . . . . . - - - - - -: g!!    $( (S (C$J (WZ]aWa (wz  ~B  xB ( ( ( (V::l#: 
: : : : : :r:   