o
    di                     @   sh   d dl mZ d dlmZ d dlmZ d dlmZmZ G dd deZ	G dd deZ
G d	d
 d
eZdS )    )Q)serializers)models)DynamicFieldsSerializerDynamicFieldsModelSerializerc                   @   sH   e Zd ZdZejddddZejddddZejddddZ	dd Z
dS )!UserLoginOrRegistrationSerializeraR  
    This is the UserLoginOrRegistrationSerializer.

    It is used for serializing user data during login or registration.

    Fields:
        mobile_number (str): The mobile number of the user.
        mobile_country_code (str): The mobile country code associated with the user.
        email (str): The email address of the user.
    Frequiredallow_blank
allow_nullc                 C      | d}| d}| d}|s|r|stddi|r)|s"|r)tddi|sQ| s6tddit|d	ksCtdd
i|d sQtddi|S Nemailmobile_numbermobile_country_codeZfield_requiredzAEither email or mobile number with country code must be provided.Zfield_conflictzBProvide either email or mobile number with country code, not both.z'Mobile number must contain only digits.
   z%Mobile number must be 10 digits long.+z-Mobile country code must contain only digits.getr   ValidationErrorisdigitlenlstripselfattrsr   r   r    r   g/var/www/html/evchargy.com/django-station-ev/station_ev/apps/account/serializers/account_serializers.pyvalidate      


z*UserLoginOrRegistrationSerializer.validateN)__name__
__module____qualname____doc__r   	CharFieldr   r   
EmailFieldr   r   r   r   r   r   r      s    r   c                   @   sX   e Zd ZdZejddddZejddddZejddddZ	ejddddZ
dd ZdS )VerifyOtpSerializera`  
    This serializer is used for validating and deserializing OTP verification data.

    Fields:
        email (str): The user's email.
        otp (str): The one-time password for verification.

    These fields are required and should not be blank or null.

    Inherits from DynamicFieldsSerializer to provide flexibility in specifying fields.
    Fr   Tc                 C   r   r   r   r   r   r   r   r   @   r   zVerifyOtpSerializer.validateN)r    r!   r"   r#   r   r$   r   r   r%   r   otpr   r   r   r   r   r&   .   s    r&   c                   @   s&   e Zd ZdZG dd dZdd ZdS )ProfileSerializera(  
    This serializer is used for validating and deserializing OTP verification data and user profile updates.

    Fields:
        mobile_number (str): The user's mobile number.
        mobile_country_code (str): The country code of the user's mobile number.
        otp (str): The one-time password for verification.

    These fields are required and should not be blank or null.

    Inherits from DynamicFieldsModelSerializer to provide flexibility in specifying fields.

    Meta:
        model (User): The model used for serialization and deserialization.
        fields (str): The fields to include in the serialization.
        read_only_fields (str): The fields that are read-only.

    Methods:
        update(instance, validated_data): Updates the user profile, validating and saving changes.
    c                   @   s   e Zd ZejZdZdgZdS )zProfileSerializer.Meta)idnamer   profile_picr   r   r)   N)r    r!   r"   r   Usermodelfieldsread_only_fieldsr   r   r   r   Metal   s    
r0   c           	      C   s  | j d}||jkr|jjstd|d}|d}|d}|r>tjj	t
|dt
|jjd @  r>td|r`|r`tjj	t
|d	t
|d
@ t
|jjd @  r`td| D ]\}}|dkrp||_qd|dkry|jjryqdt||| qd|  |S )a  
        Update the user profile, validating and saving changes.

        Args:
            instance (User): The user profile instance to update.
            validated_data (dict): Validated data containing user profile details.

        Returns:
            User: The updated user profile.

        This method handles the update of user profile data, validates changes, and saves the updated profile.
        It ensures that the user has permission to update their own profile and checks for email uniqueness.
        requestz2You have no permission to update this user profiler   r   r   )r   )r)   zThis email is already in use.)r   )r   z%This mobile number is already in use.r+   )contextr   useris_superuserr   r   r   r,   objectsfilterr   r)   existsitemsr+   setattrsave)	r   instancevalidated_datar1   r   r   r   attrvaluer   r   r   updatex   sL   




	zProfileSerializer.updateN)r    r!   r"   r#   r0   r?   r   r   r   r   r(   V   s    r(   N)django.db.modelsr   rest_frameworkr   Zapps.accountr   Zbase.serializersr   r   r   r&   r(   r   r   r   r   <module>   s    &(