o
    di                      @   s   d dl mZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZ G d	d
 d
eZG dd deZG dd deZG dd deZdS )    )QAvg)serializers)models)ProfileSerializer)UserVehicleSerializer)master_serializers)StationMasterSerializer)DynamicFieldsModelSerializerc                   @   s   e Zd ZdZG dd dZdS )RatingMediaSerializera  
    This serializer is used to serialize and deserialize media assets associated with station rating records.

    Fields:
        id (int): The unique identifier for the media asset.
        asset (File): The media asset file (e.g., image or video).
        asset_type (str): The type or format of the media asset.

    Meta:
        model (StationRatingMedia): The model used for serialization and deserialization.
        fields (tuple): The fields to include in the serialization.
    c                   @   s   e Zd ZejZdZdS )zRatingMediaSerializer.Meta)idasset
asset_typeis_verifiedN)__name__
__module____qualname__r   StationRatingMediamodelfields r   r   v/var/www/html/evchargy.com/django-station-ev/station_ev/apps/station/serializers/station_checkIn_rating_serializers.pyMeta   s    r   N)r   r   r   __doc__r   r   r   r   r   r      s    r   c                   @   s\   e Zd ZdZejddZejddgddZ	e
g dddZG dd	 d	Zd
d Zdd ZdS )StationRatingSerializeraC  
    Serializer for station rating records, including associated media assets.

    Fields:
        - id (int): Unique identifier for the check-in record.
        - station (int): Identifier of the associated station.
        - user (int): Identifier of the user who made the rating.
        - rating (float): User's rating for the station.
        - label (str, optional): User-provided label or title for the rating.
        - description (str, optional): User-provided description or comment.
        - media (list of dict): List of dictionaries with media asset details.
    T	read_onlylabelsourcer   r   )r   nameprofile_pic)r   r   c                   @   s(   e Zd ZejZdZdgZdddiiZdS )zStationRatingSerializer.Meta)	r   stationuserratingr   descriptionmediacreated
label_datar'   r   
write_onlyTN)	r   r   r   r   StationRatingr   r   read_only_fieldsextra_kwargsr   r   r   r   r   5   s    r   c                 C   s$   |j jdd}t|d| jd}|jS )z
        Retrieve and format associated media assets.

        Args:
            obj: The station rating record.

        Returns:
            List of dictionaries with media asset details.
        T)r   )manycontext)rating_station_mediasfilterr   r.   data)selfobjmedias
serializerr   r   r   	get_mediaG   s   
z!StationRatingSerializer.get_mediac                 C   s:   | d}| d}|du r|du s|dkrtd|S )a  
        Validate the presence of a label or description.

        Args:
            data: The input data to validate.

        Returns:
            Validated data.

        Raises:
            serializers.ValidationError: If both label and description are missing.
        r   r%   N zDescription field is required)getr   ValidationError)r2   r1   r   r%   r   r   r   validateV   s
   


z StationRatingSerializer.validateN)r   r   r   r   r   SerializerMethodFieldr&   r   RatingLabelsSerializersr(   r   r#   r   r6   r:   r   r   r   r   r       s    r   c                   @   sF   e Zd ZdZejddZejddZG dd dZdd Z	dd	 Z
d
S )#StationCheckInCalculationSerializera  
    A serializer for calculating Station Check-In statistics.

    Fields:
        rating_counts (dict): Dictionary containing rating statistics for a station.
        checkin_count (int): Total number of check-ins for the station.

    Meta:
        model (StationMaster): The model used for serialization.
        fields (tuple): The fields to include in the serialization.
        read_only_fields (tuple): Fields that are read-only.
    Tr   c                   @   s   e Zd ZejZdZeZdS )z(StationCheckInCalculationSerializer.Meta)r   rating_countscheckin_countN)r   r   r   r   StationMasterr   r   r+   r   r   r   r   r   {   s    r   c                 C   s   |j  }dd tddD }|t|j tdd pddd	}t|dD ]0\}}|j | |d
| d< |dkrLtd|d
| d  | d}nd}||d
| d< q&|S )z
        Calculate rating statistics for a station.

        Args:
            obj (StationMaster): The StationMaster object.

        Returns:
            dict: A dictionary with rating statistics.
        c                 S   s   g | ]}t |d qS ))r$   )r   ).0r$   r   r   r   
<listcomp>   s    zIStationCheckInCalculationSerializer.get_rating_counts.<locals>.<listcomp>      r$   rating__avgr      )Ztotal_rating_countZtotal_rating_averageZrating__countd   Z_average)station_ratingscountrangeround	aggregater   	enumerater0   )r2   r3   total_count
conditionsZrating_statsr$   	conditionaverage_ratingr   r   r   get_rating_counts   s(   
z5StationCheckInCalculationSerializer.get_rating_countsc                 C   s   |j  }|S )z
        Calculate the total number of check-ins for a station.

        Args:
            obj (StationMaster): The StationMaster object.

        Returns:
            int: The total number of check-ins.
        )station_checkinrJ   )r2   r3   rO   r   r   r   get_checkin_count   s   

z5StationCheckInCalculationSerializer.get_checkin_countN)r   r   r   r   r   r;   r>   r?   r   rS   rU   r   r   r   r   r=   j   s    )r=   c                       sR   e Zd ZdZedg dddZedddgddZG d	d
 d
Z fddZ	  Z
S )StationCheckInSerializera  
    This serializer is used to serialize and deserialize station check-in records.

    Fields:
        id (int): The unique identifier for the media asset.
        station (int): The station associated with the check-in.
        user (int): The user who performed the check-in.

    Meta:
        model (StationCheckIn): The model used for serialization and deserialization.
        fields (tuple): The fields to include in the serialization.
        read_only_fields (tuple): Fields that are read-only.
    r"   )r   r    r%   coordinatesassetsTr   user_vehicler   vehicle_numberc                   @   s,   e Zd ZejZdZdZddiddidZdS )zStationCheckInSerializer.Meta)r   r"   r#   rY   r'   station_datauser_vehicle_data)r   r#   r)   T)rY   r"   N)	r   r   r   r   StationCheckInr   r   r+   r,   r   r   r   r   r      s    	
r   c                    sv   t t| j|i | d|v r5d|d v r7|d d }d|jv r9|jd }tjjj||jd| j	d _
dS dS dS dS )a%  
        Initialize the VehicleInsuranceSerializers instance.

        Dynamically filter the 'insurance_type' field queryset based on the selected 'insurance_company'.

        Args:
            *args: Variable length argument list.
            **kwargs: Arbitrary keyword arguments.
        r.   requestrY   )pkr#   N)superrV   __init__r1   account_modelsUserVehicleobjectsr0   r#   r   queryset)r2   argskwargsr^   rY   	__class__r   r   ra      s   


z!StationCheckInSerializer.__init__)r   r   r   r   r	   r[   r   r\   r   ra   __classcell__r   r   rh   r   rV      s    rV   N)django.db.modelsr   r   rest_frameworkr   Zapps.accountr   rb   Z,apps.account.serializers.account_serializersr   Z1apps.account.serializers.user_vehicle_serializersr   Zapps.master.serializersr   apps.station,apps.station.serializers.station_serializersr	   base.serializersr
   r   r   r=   rV   r   r   r   r   <module>   s    JM