o
    d6i                     @   s   d Z 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
mZmZmZmZmZ eeZdeded	ed
efddZdededed	efddZdededed	efddZdeded	efddZdS )z
Utility functions for Karishye AI.

This module contains:
- Puja state management functions (get/save user puja state)
- Phone number validation and saving
- Puja booking utilities (cancellation, completion response)
- Knowledge base helpers
    N)datetime)DictAny)general_agentChatContext)create_puja_sessionget_puja_sessionupdate_puja_sessionpuja_session_to_dictsave_user_phone_numbervalidate_indian_phone_numberuser_id	puja_type
session_idreturnc                 C   s&   t | ||}|rt|S |di ddS )z
    Get puja session state from database.
    Returns dict for backward compatibility with existing code.
    Session ID is required - no default fallback.
    r   F)r   current_questioncollected_info	completed)r   r
   r   r   r   puja_session r   3/var/www/html/karishye-ai-python/app/utils/utils.pyget_user_puja_state   s   r   statec                    sz   h d  fdd|  D }t| ||}|r!t| |||d dS |d|}t| |||d}|r;t| |||d dS dS )z
    Save puja session state to database.
    Accepts dict for backward compatibility with existing code.
    Session ID is required - no default fallback.
    >   r   
kb_checkedawaiting_phoner   phone_collectedr   invalid_attemptsc                    s   i | ]\}}| v r||qS r   r   ).0kvVALID_FIELDSr   r   
<dictcomp>@   s    z(save_user_puja_state.<locals>.<dictcomp>r   r   r   updates	puja_name)r   r   r   r&   N)itemsr   r	   getr   )r   r   r   r   filtered_stateexisting_sessionr&   new_sessionr   r!   r   save_user_puja_state4   s2   

r,   phone_numberc              
   C   sR   z
t | ||| W dS  ty( } ztdt|j  W Y d}~dS d}~ww )z@
    Save user's phone number to database (NOT sent to AI).
    Tz[DB] Failed to save phone: NF)save_phone_to_db	Exceptionloggererrortype__name__)r   r-   r   r   er   r   r   r   g   s   r   c                 C   sJ   t d|  d| d|  t| ||}|r#t| ||dddd dS dS )	zNCancel the ongoing puja collection by marking it as cancelled in the database.z$Cancelling puja collection for user z, puja z
, session TF)r   r   r$   N)r0   infor   r	   r   r   r   r   cancel_puja_collectionw   s   
r6   )__doc__loggingr   typingr   r   app.agents.agentsr   r   app.utils.puja_session_utilsr   r   r	   r
   r   r.   r   	getLoggerr3   r0   strdictr   r,   r6   r   r   r   r   <module>   s    
 
	3