o
    ‚h�jI  ã                   @   s  d dl 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ed	e	d
e	de	de	de	de
fdd„Zded	e	de
dB fdd„Zdede	de
dB fdd„Zdedee
 fdd„Zdedefdd„Zdede	de
de
dB fdd„Zdede	de	de
dB fdd„ZdS )!é    N)ÚdatetimeÚtimezone)ÚReturnDocument)ÚAsyncDatabase)Úhash_passwordÚAGENTÚACTIVEÚdbÚemailÚpasswordÚnameÚroleÚstatusÚreturnc           
   	   Ã   s|   �|  ¡  ¡ }dt ¡ jdd… › �}t tj¡}|||t	|ƒ|dv r%|nd|dv r,|nd||dœ}	| d	  
|	¡I dH  |	S )
zG
    Creates a new agent record in MongoDB with a hashed password.
    Úagent_Né   )r   ÚADMINr   )r   ÚINACTIVEr   )ÚagentIdr   r
   ÚpasswordHashr   r   Ú	createdAtÚ	updatedAtÚagents)ÚstripÚlowerÚuuidÚuuid4Úhexr   Únowr   Úutcr   Ú
insert_one)
r	   r
   r   r   r   r   Únormalized_emailÚagent_idr   Ú	agent_doc© r$   úB/web/html/dev/chatbot/backend/app/repositories/agent_repository.pyÚcreate_agent   s   €ør&   c                 Ã   s*   �|sdS | d   d| ¡  ¡ i¡I dH S )z+Finds an agent by normalized email address.Nr   r
   )Úfind_oner   r   )r	   r
   r$   r$   r%   Úget_agent_by_email$   s   € r(   r"   c                 Ã   s"   �|sdS | d   d|i¡I dH S )z!Finds an agent by unique agentId.Nr   r   )r'   )r	   r"   r$   r$   r%   Úget_agent_by_id*   s   €r)   c                 Ã   s@   �| d   i ¡}t|dƒr|jdd�I dH S dd„ |2 ƒI dH S )z'Lists all registered agents in MongoDB.r   Úto_listéè  ©ÚlengthNc                 Ó   ó   �g | z3 d H W }|‘q6 S ©Nr$   ©Ú.0Údocr$   r$   r%   Ú
<listcomp>6   ó   € zlist_agents.<locals>.<listcomp>)ÚfindÚhasattrr*   )r	   Úcursorr$   r$   r%   Úlist_agents0   s
   €
r8   c                 Ã   sV   �| d   dddœ¡}t|dƒr|jdd�I dH }t|ƒS d	d
„ |2 ƒI dH }t|ƒS )z;Counts active ADMIN accounts to prevent accidental lockout.r   r   r   )r   r   r*   r+   r,   Nc                 Ó   r.   r/   r$   r0   r$   r$   r%   r3   ?   r4   z'count_active_admins.<locals>.<listcomp>)r5   r6   r*   Úlen)r	   r7   Údocsr$   r$   r%   Úcount_active_admins8   s   €
r;   Úupdate_fieldsc                 ƒ   sh   �h d£‰ ‡ fdd„|  ¡ D ƒ}|st| |ƒI dH S t tj¡|d< | d jd|id|itjd	�I dH S )
z�
    Updates safe fields (name, role, status) for an agent.
    Filters out passwordHash, agentId, createdAt from direct update payload.
    >   r   r   r   c                    s&   i | ]\}}|ˆ v r|d ur||“qS r/   r$   )r1   ÚkÚv©Úallowed_keysr$   r%   Ú
<dictcomp>H   s   & z update_agent.<locals>.<dictcomp>Nr   r   r   ú$set©Úreturn_document)	Úitemsr)   r   r   r   r   Úfind_one_and_updater   ÚAFTER)r	   r"   r<   Úfiltered_updatesr$   r?   r%   Úupdate_agentB   s   €ýrI   Únew_passwordc                 Ã   sH   �|sdS t |ƒ}t tj¡}| d jd|id||dœitjd�I dH S )z<
    Hashes and updates an agent's password atomically.
    Nr   r   rB   )r   r   rC   )r   r   r   r   r   rF   r   rG   )r	   r"   rJ   Únew_hashr   r$   r$   r%   Úreset_agent_passwordU   s   €þÿørL   )r   r   )r   r   r   Úpymongor   Úpymongo.asynchronous.databaser   Úapp.utils.securityr   ÚstrÚdictr&   r(   r)   Úlistr8   Úintr;   rI   rL   r$   r$   r$   r%   Ú<module>   s8    úÿþýüûú
ù
"