a
    AWe0                     @   s\   d dl m Z  d dlmZmZmZmZmZmZmZ d dl	m
Z
 d dlmZ G dd deZdS )    )datetime)StringField
EmailFieldDateTimeFieldBooleanField	ListFieldReferenceFieldCASCADE)bcrypt)Basec                       sR  e Zd ZdZedddddZeddddZeddd	Zed
ddZ	edd
g dddZ
edddZedddZedddZedddZedddZedddZedddZedddZedddZedddZeejdddZedddZd d! Zed"d#d$Zed"d%d&Zd'd( Zd)d* Z d+d, Z!d-d. Z"d/d0 Z#d1d2 Z$ fd3d4Z%  Z&S )5Usera  
    Professional User model for managing user information.

    Attributes:
        username (str): The unique username of the user (required).
        email (str): The email address of the user (required, unique).
        password (str): The user's password (required).
        first_name (str): The user's first name.
        last_name (str): The user's last name. 
        is_verified (bool): A flag indicating if the user's email is verified.
        is_superuser (bool): A flag indicating if the user is a superuser.
        is_admin (bool): A flag indicating if the user is an admin.
        is_active (bool): A flag indicating if the user account is active.
        is_staff (bool): A flag indicating if the user is staff.
        last_login (datetime): The date and time of the user's last login.
        date_joined (datetime): The date and time of user registration (auto-generated).
        roles (list of Role): User roles or permissions (related to Role model).
        last_seen_at (datetime): The date and time when the user was last seen.
        notif_enabled (bool): A flag indicating if notifications are enabled for the user.
        is_online (bool): A flag indicating if the user is currently online.
        is_blocked (bool): A flag indicating if the user is blocked.

    Methods:
        __str__(): Return the username as a string representation of the user.
        set_password(password: str): Set the user's password (typically after hashing).
        check_password(password: str): Check if the provided password matches the user's password.
        add_role(role: Role): Add a role to the user's list of roles.
        remove_role(role: Role): Remove a role from the user's list of roles.
        mark_as_online(): Mark the user as online.
        mark_as_offline(): Mark the user as offline.
        block_user(): Block the user, preventing certain actions.
        unblock_user(): Unblock the user, allowing them to resume normal activities.
        update_last_seen(): Update the user's last seen timestamp to the current time.

    T2   z The unique username of the user.)requiredunique
max_length	help_textzThe email address of the user.)r   r   r   zThe user's password.)r   r      zThe user's Company name.)r   r   )zAnonymous CompanyzBrands CompanyzAd Agency CompanyzTV Channels CompanyzThe user's Company type.)r   r   choicesr   zThe user's rols.Fz2A flag indicating if the user's email is verified.)defaultr   z-A flag indicating if the user is a superuser.z*A flag indicating if the user is an admin.z0A flag indicating if the user account is active.z'A flag indicating if the user is staff.z2A flag indicating if the user is currently online.z)A flag indicating if the user is blocked.z<A flag indicating if notifications are enabled for the user.Nz+The date and time of the user's last login.z&The date and time of user registration)r   readonlyr   z.The date and time when the user was last seen.c                 C   s   | j S )z
        Return the username as a string representation of the user.

        Returns:  
            str: The username of the user.
        )usernameself r   /app/app/models/user.py__str__z   s    zUser.__str__)passwordc                 C   s   t |d| _dS )z
        Set the user's password, typically after hashing it.

        Args:
            password (str): The user's password.

        Returns:
            None
        zutf-8N)r
   generate_password_hashdecoder   r   r   r   r   r   set_password   s    
zUser.set_passwordc                 C   s   t | j|S )z
        Check if the provided password matches the user's password.

        Args:
            password (str): The password to check.

        Returns:
            bool: True if the password matches, False otherwise.
        )r
   check_password_hashr   r   r   r   r   check_password   s    
zUser.check_passwordc                 C   s
   d| _ dS )zM
        Mark the user as online.

        Returns:
            None
        TN	is_onliner   r   r   r   mark_as_online   s    zUser.mark_as_onlinec                 C   s
   d| _ dS )zN
        Mark the user as offline.

        Returns:
            None
        FNr#   r   r   r   r   mark_as_offline   s    zUser.mark_as_offlinec                 C   s
   d| _ dS )zj
        Block the user, preventing them from certain actions.

        Returns:
            None
        TN
is_blockedr   r   r   r   
block_user   s    zUser.block_userc                 C   s
   d| _ dS )zq
        Unblock the user, allowing them to resume normal activities.

        Returns:
            None
        FNr'   r   r   r   r   unblock_user   s    zUser.unblock_userc                 C   s   t  | _dS )zo
        Update the user's last seen timestamp to the current time.

        Returns:
            None
        N)r   utcnowlast_seen_atr   r   r   r   update_last_seen   s    zUser.update_last_seenc                 C   s   t  | _dS )zp
        Update the user's last login timestamp to the current time.

        Returns:
            None
        N)r   r+   
last_loginr   r   r   r   update_last_login   s    zUser.update_last_loginc                    sz   t t|  }g d}|dds:|dd |dd | |d|d< | |d|d< | |d|d< |S )	z7
        Convert the document to a dictionary.
        )idr   r$   _clsZrolesdeletedFN
deleted_atdate_joinedr.   r,   )superr   to_dictgetpop_format_datetime)r   Z	user_dictZexclude_fields	__class__r   r   r6      s    zUser.to_dict)'__name__
__module____qualname____doc__r   r   r   emailr   company_namecompany_typeroler   is_verifiedis_superuseris_admin	is_activeis_staffr$   r(   Znotif_enabledr   r.   r   r+   r4   r,   r   strr    r"   r%   r&   r)   r*   r-   r/   r6   __classcell__r   r   r:   r   r      s   $							r   N)r   mongoenginer   r   r   r   r   r   r	   Zapp.config.extensionsr
   
app.modelsr   r   r   r   r   r   <module>   s   $