
    pljF                         d dl Z 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i fdededed	efd
Zddede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y)    N)text)Literal)get_async_db)HTTPExceptionquestionhotel_idsub_hotel_idmetadatac                   K   	 t               4 d {   }t        t        j                               }|j	                  t        d      || |||rt        j                  |      nd d       d {    |j                          d {    d d d       d {    |S 7 7 07 7 # 1 d {  7  sw Y   S xY w# t        $ r"}t        j                  d|        Y d }~y d }~ww xY ww)NzINSERT INTO conversations (id, first_message, hotel_id, sub_hotel_id, metadata) VALUES (:id, :msg, :hotel_id, :sub_hotel_id, :metadata))idmsgr   r	   r
   zError creating conversation: )r   struuiduuid4executer   jsondumpscommit	Exceptionlogerror)r   r   r	   r
   dbconversation_ides          =/var/www/html/ls-ibe-bot-apis/app/utils/store_conversation.pycreate_conversationr   (   s     >>R!$**,/O**  _  `&xX_k  RZy}  zD  zD  EM  zN  `d  e  
 ))+ ">  "
  ">>>  		1!56s   C*B< BB< AB&/B 0B&B"B&B< B$B< C*B<  B&"B&$B< &B9,B/-B94B< 8C*9B< <	C'C"C*"C''C*r   answercustomer_idc                 p  K   	 | st        |||       d {   } g }|r|j                  | |d|d       |r|j                  | |d|d       t        |      dk(  r| S t               4 d {   }t	        d      }t	        d      }	|j                  ||       d {    |j                  |	d| i       d {    |j                          d {    d d d       d {    | S 7 7 |7 N7 47 7 # 1 d {  7  sw Y   | S xY w# t        $ r"}
t        j                  d|
        Y d }
~
y d }
~
ww xY w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
                UPDATE conversations
                SET updated_at = NOW()
                WHERE id = :conversation_id
            r   zError saving conversation: )
r   appendlenr   r   r   r   r   r   r   )r   r   r	   r   r   r   messagesr   queryquery1r   s              r   save_conversationr)   :   sG    +$7(L$YYOOO#2#*	  OO#2!*	  x=A"">>R  E
   F
 **UH---**V&7%IJJJ))+ "> M Z. " .J ">>>  		/s34s   D6D C&AD D6D +C(,D /,C2C*C27C,8C2C.C2D C0 D %D6&D (D *C2,C2.C20D 2D8C;9D D D6D 	D3D.)D6.D33D6feedback)updownreturnc                 (  K   t               4 d{   }|j                  t        d      d| i       d{   }|j                         st	        dd      |j                  t        d      d| i       d{   }|j                         }|st	        dd      |d	   }|j                  t        d
      ||d       d{    |j                          d{    	 ddd      d{    y7 7 7 |7 47 7 # 1 d{  7  sw Y   yxY ww)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_rowr1   s          r   save_conversation_feedbackr6   g   s!     ~~JJ   0	
 	
 ##%C8PQQ ::   0

 


 %%'C8MNNQZ
jj   %(

 
	
 
	
 iik_ ~~	


$
	
 	] ~~~s   DC1D!C=C3AC=8C59A	C=C7C=C9C= D+C;,D3C=5C=7C=9C=;D=DDDD)NNN)r   r   logging
sqlalchemyr   typingr   app.models.connectionr   app.exceptions.httpr   	getLogger__name__r   r   dictr   r)   boolr6        r   <module>rB      s         . -> g!`b  s # Y] $,S ,C ,s ,^adh^h ,y|  @D  zD ,  Z]  `d  Zd ,Z::l#: 
:rA   