a
    AWe                     @   s*   d dl mZ d dlmZ G dd dZdS )    )Q)Userc                   @   sx   e Zd Zedd Zedd Zedd Zedd Zed	d
 Zedd Z	edd Z
edd Zedd ZdS )AuthServicec                 C   s(   t f i | }|| d  |  |S )z 
        Create a new user.
        :param data: A dictionary containing user data.
        :return: The created user document.
        password)r   set_passwordsave)datauser r
   /app/app/services/auth.pycreate_user   s    zAuthService.create_userc                 C   s   t j| d }|S )z
        Get a user by their ID.
        :param user_id: The ID of the user.
        :return: The user document or None if not found.
        )	public_idr   objectsfirst)user_idr	   r
   r
   r   get_user_by_id   s    zAuthService.get_user_by_idc                 C   s"   t t| dt| dB  }|S )z
        Get a user by their email or username.
        :param email_or_username: The email or username of the user.
        :return: The user document or None if not found.
        email)username)r   r   r   r   )email_or_usernamer	   r
   r
   r   get_user_by_email_or_username   s    z)AuthService.get_user_by_email_or_usernamec                 C   s   t j| d }|S )z
        Get a user by their email.
        :param email: The email of the user.
        :return: The user document or None if not found.
        r   r   )r   r	   r
   r
   r   get_user_by_email&   s    zAuthService.get_user_by_emailc                 C   s   t t| dt| dB }|S )z
        Search for users containing a keyword in their name, email, or other attributes.
        :param keyword: The keyword to search for.
        :return: A list of matching user documents.
        )Zusername__icontains)Zemail__icontains)r   r   r   )keywordZusersr
   r
   r   search_users_by_keyword0   s    z#AuthService.search_users_by_keywordc                 C   s    t | }|r||r|S dS )a  
        Authenticate a user using their email and password.
        :param email: The email of the user.
        :param password: The password provided by the user.
        :return: The authenticated user document if successful, None if authentication fails.
        N)r   r   Zcheck_password)r   r   r	   r
   r
   r   authenticate:   s    
zAuthService.authenticatec                 C   s,   t | }|r$|| |  dS dS dS )z
        Change the password of a user.
        :param user_id: The ID of the user to update the password for.
        :param new_password: The new password.
        :return: True if the password is updated, False if the user is not found.
        TFNr   r   r   r   r   Znew_passwordr	   r
   r
   r   change_passwordG   s    

zAuthService.change_passwordc                 C   s,   t | }|r$||_|  | S dS dS )z
        Promote a user by changing their role.
        :param user_id: The ID of the user to promote.
        :param new_role: The new role for the user.
        :return: The updated user document with the new role, or None if not found.
        N)r   r   Zroler   to_dict)r   Znew_roler	   r
   r
   r   promote_userW   s    
zAuthService.promote_userc                 C   s,   t | }|r$|| |  dS dS dS )z
        Reset the password of a user.
        :param user_id: The ID of the user to reset the password for.
        :param new_password: The new password.
        :return: True if the password is reset, False if the user is not found.
        TFNr   r   r
   r
   r   reset_passwordg   s    

zAuthService.reset_passwordN)__name__
__module____qualname__staticmethodr   r   r   r   r   r   r   r    r!   r
   r
   r
   r   r      s$   

	
	
	
	


r   N)mongoengine.queryset.visitorr   Z
app.modelsr   r   r
   r
   r
   r   <module>   s   