o
    î|i«  ã                   @   s¤   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mZ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dS )é    )Údatetime)ÚAPIView)ÚResponse©Ústatus)ÚNotFoundé   )ÚVehiclePricing)ÚVehiclePricingCreateSerializerÚVehiclePricingListSerializerÚVehiclePricingDetailSerializer)ÚVehicle)ÚMethodRolePermission)ÚJWTAuthenticationc                   @   sD   e Zd ZdZegZegZdgddgdœZdd„ Z	dd„ Z
d	d
„ ZdS )ÚVehiclePricingViewzŽ
    Handles creating (POST) and listing (GET) Vehicle Pricing entries.
    Each pricing record is directly linked to a specific Vehicle.
    ÚadminÚpartner)ÚPOSTÚGETc                 C   s4   |sdS t j|d� ¡ }|sdd|› d�fS |dfS )zd
        Validate and fetch the Vehicle by ID.
        Returns (vehicle_obj, error_message)
        )NzVehicle ID is required.©ÚidNúVehicle with ID 'ú' not found.)r   ÚobjectsÚfirst)ÚselfÚ
vehicle_idÚvehicle© r   ú:/var/www/html/Moveonwheels/move_on_wheels/pricing/views.pyÚget_vehicle%   s   zVehiclePricingView.get_vehiclec                 C   sž   |j }| d¡}|  |¡\}}|rtd|itjd�S tj|d� ¡ r+tdditjd�S t	|d�}| 
¡ s<t|jtjd�S |j|d�}tdt|jƒdœtjd�S )	z|
        Create a new Vehicle Pricing record.
        Ensures valid vehicle and prevents duplicate pricing entries.
        r   Úerrorr   ©r   z(Pricing for this vehicle already exists.)Údataz%Vehicle Pricing created successfully.)ÚmessageÚ
pricing_id)r#   Úgetr    r   r   ÚHTTP_400_BAD_REQUESTr	   r   r   r
   Úis_validÚerrorsÚsaveÚstrr   ÚHTTP_201_CREATED)r   Úrequestr#   r   r   Úvehicle_errÚ
serializerÚpricingr   r   r   Úpost3   s(   
þ
þûzVehiclePricingView.postc                 C   s`   t j ¡ }dd„ tj ¡ D ƒ}|D ]}| t|jƒ¡|_qt|dd�}t	t
|ƒ|jdœtjd�S )zˆ
        Retrieve all vehicles and their pricing data.
        If pricing not assigned, mark relevant fields as 'Not assigned'.
        c                 S   s   i | ]	}t |jjƒ|“qS r   )r+   r   r   )Ú.0Úpr   r   r   Ú
<dictcomp>_   s    z*VehiclePricingView.get.<locals>.<dictcomp>T)Úmany)ÚcountÚresultsr   )r   r   Úallr	   r&   r+   r   r0   r   r   Úlenr#   r   ÚHTTP_200_OK)r   r-   ÚvehiclesÚpricingsr   r/   r   r   r   r&   Y   s   
þûzVehiclePricingView.getN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úauthentication_classesr   Úpermission_classesÚallowed_rolesr    r1   r&   r   r   r   r   r      s    þ&r   c                   @   sD   e Zd ZdZegZegZddgdgdœZdd„ Z	dd„ Z
d	d
„ ZdS )ÚVehiclePricingDetailViewzQ
    Retrieve or update Vehicle Pricing by Vehicle ID instead of Pricing ID.
    r   r   )r   ÚPUTc                 C   sP   t j|d� ¡ }|std|› d�d�‚tj|d� ¡ }|s&td|› d�d�‚|S )z6Fetch pricing by Vehicle ID or raise 404 if not found.r   r   r   ©Údetailr"   úPricing for Vehicle ID ')r   r   r   r   r	   )r   r   r   r0   r   r   r   Úget_object_by_vehicle‚   s   z.VehiclePricingDetailView.get_object_by_vehiclec                 C   sú   t j|d� ¡ }|std|› d�d�‚tj|d� ¡ }|r;t|ƒ}|j}t|ddƒ|d< t|ddƒ|d< t|t	j
d	�S ti d
t|jƒ“dd“dt|ddƒ“dt|ddƒ“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“t	j
d	�S )z˜
        Retrieve a vehicle's pricing record by vehicle ID.
        If pricing is not assigned, return default values with NOT_ASSIGNED status.
        r   r   r   rF   r"   Ú
model_nameNÚpickup_point_namer   r   r   ÚNOT_ASSIGNEDÚhourly_price_sg        Úhourly_price_mÚhourly_price_lÚhourly_extra_price_per_km_sÚhourly_extra_price_per_km_mÚhourly_extra_price_per_km_lÚmonthly_price_sÚmonthly_price_mÚmonthly_price_lÚmonthly_extra_price_per_km_sÚmonthly_extra_price_per_km_mÚmonthly_extra_price_per_km_l)r   r   r   r   r	   r   r#   Úgetattrr   r   r:   r+   r   )r   r-   r   r   r0   r/   r#   r   r   r   r&   �   s\   
ÿþýüùø	÷
öõôóòñðïîëzVehiclePricingDetailView.getc                 C   sž   t j|d� ¡ }|std|› d�d�‚tj|d� ¡ }|s&td|› d�d�‚|j}t||dd�}| ¡ s<t|j	t
jd	�S |j|d�}td
t|ƒjdœt
jd	�S )zC
        Update pricing for a given vehicle by vehicle ID.
        r   r   r   rF   r"   rH   T)r#   Úpartialr   z%Vehicle Pricing updated successfully.)r$   r#   )r   r   r   r   r	   r#   r   r(   r   r)   r   r'   r*   r:   )r   r-   r   r   r0   r#   r/   Úupdated_pricingr   r   r   Úput¿   s"   þûzVehiclePricingDetailView.putN)r=   r>   r?   r@   r   rA   r   rB   rC   rI   r&   r\   r   r   r   r   rD   t   s    þ0rD   N)r   Úrest_framework.viewsr   Úrest_framework.responser   Úrest_frameworkr   Úrest_framework.exceptionsr   Úmodelsr	   Úserializersr
   r   r   Úvehicles.modelsr   Úpermissionsr   Úauthenticationr   r   rD   r   r   r   r   Ú<module>   s    _