o
    �h�jr	  ã                   @   sº   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZ eddgd	�Zejd
eejd�defdd„ƒZejdeejd�eeƒfdefdd„ƒZdS )é    )Ú	APIRouterÚstatusÚHTTPExceptionÚDepends)Úget_database)Úget_agent_by_email)Úverify_passwordÚcreate_access_token)ÚAgentLoginRequestÚAgentLoginResponseÚSafeAgentInfo)Úget_current_agentz/api/v1/authÚauth)ÚprefixÚtagsz/login)Úresponse_modelÚstatus_codeÚpayloadc           	   	   Ã   s¸   �t ƒ }t|| jƒI dH }ttjdddid�}|r | d¡dkr"|‚| dd	¡}t| j|ƒs0|‚|d
 }| dd¡}t	||dœd�}t
|| dd¡|d || dd¡d�}t|d|d�S )z´
    Authenticates an agent using email and password.
    Returns a JWT access token and safe agent profile info.
    Generic 401 Unauthorized returned on invalid credentials.
    NzInvalid email or passwordzWWW-AuthenticateÚBearer)r   ÚdetailÚheadersr   ÚACTIVEÚpasswordHashÚ ÚagentIdÚroleÚAGENT)Úsubr   )ÚdataÚnameÚAgentÚemail©r   r   r!   r   r   Úbearer)ÚaccessTokenÚ	tokenTypeÚagent)r   r   r!   r   r   ÚHTTP_401_UNAUTHORIZEDÚgetr   Úpasswordr	   r   r   )	r   Údbr&   Úgeneric_errorÚpassword_hashÚagent_idÚ
agent_roleÚaccess_tokenÚ	safe_info© r1   ú1/web/html/dev/chatbot/backend/app/routers/auth.pyÚlogin_agent
   s>   €ý
þ

ûýr3   z/meÚcurrent_agentc              	   Ã   s4   �t | d |  dd¡| d |  dd¡|  dd¡d	�S )
z|
    Returns profile information for the currently authenticated agent.
    Requires Bearer token authorization header.
    r   r   r    r!   r   r   r   r   r"   )r   r(   )r4   r1   r1   r2   Úget_authenticated_agent_profile9   s   €


ûr5   N)Úfastapir   r   r   r   Úapp.databaser   Ú!app.repositories.agent_repositoryr   Úapp.utils.securityr   r	   Úapp.models.authr
   r   r   Úapp.dependencies.authr   ÚrouterÚpostÚHTTP_200_OKr3   r(   Údictr5   r1   r1   r1   r2   Ú<module>   s    .