o
    Ñ²]fÃ  ã                   @   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
T d dlmZ d d	lmZ d d
lZd dlmZ d dlmZmZ d d
lZdd„ Zdd„ Zedd„ ƒZdd„ Zd
S )é    )Útimezone)Ú	timedelta)ÚDistance)ÚDriverLocationé   )ÚRide)Ú*)Úsync_to_async)ÚDecimalN)ÚValidationError)r
   ÚROUND_UPc                 C   sŒ   d}| › d|› �}|› d|› �}d|› d|› d|› �}t  |¡}| ¡ }	|	d dkr0t|	d ƒ‚tt|	d	 d
 d d
 d d d ƒƒ}
|
S )Nz'AIzaSyAEWPwKw_tv81JSa0x_glii-pz51JCX_PUú,zAhttps://maps.googleapis.com/maps/api/distancematrix/json?origins=z&destinations=z&key=ÚstatusÚOKÚerror_messageÚrowsr   ÚelementsÚdistanceÚvalueiè  )ÚrequestsÚgetÚjsonr   r
   Ústr)Úfrom_latitudeÚfrom_longitudeÚto_latitudeÚto_longitudeÚapi_keyÚoriginsÚdestinationsÚendpointÚresponseÚdataÚdistance_in_km© r$   ú%/var/www/html/loop/booking/process.pyÚcalculate_distance   s   
"ÿr&   c                 O   sJ   zt  ¡ }W n ty   t  ¡ }t  |¡ Y nw | | |i |¤Ž¡ d S )N)ÚasyncioÚget_running_loopÚRuntimeErrorÚnew_event_loopÚset_event_loopÚcreate_task)Ú
async_funcÚargsÚkwargsÚloopr$   r$   r%   Úsetup_async_tasks    s   þr1   c                 C   sô   t dƒ t ¡ tdd� }t dƒ || j  kr!|tdd� krtn nQt dƒ t| j| jƒ}t dƒ t| jj	| jj
| jj	| jj
ƒ}|| jj }|jtdƒtd	�}|| _|| _|  ¡  |rrt d
ƒ t|| ƒ t dƒ d| _|  ¡  d S d S t dƒ d S )Nz>Step 1: Adjusting current time to account for potential delaysé   )Úminutesz?Step 2: Checking if the ride time is within the next 15 minutesé   zAStep 3: Finding the nearest available driver for the vehicle typez/Step 4: Calculating distance and estimated farez0.01)ÚroundingzBStep 6: Sending a notification to the driver with the ride detailsz0Step 7: Updating the ride status to 'processing'Ú
processingz0The ride time is not within the next 15 minutes.)Úprintr   Únowr   Ú	ride_timeÚfind_nearest_available_driverÚpickup_locationÚvehicle_typer&   ÚyÚxÚdropoff_locationÚper_km_chargeÚquantizer
   r   r   Úestimated_fareÚsaveÚnotify_driver_new_rider   )ÚrideÚadjusted_current_timeÚnearest_driverr   rB   Úrounded_farer$   r$   r%   Úprocess_new_ride)   s@   ÿ
þÿü
úrI   c                 C   s6   t jjd|dd�jtd| ƒd� d¡}|r| ¡ S d S )NFT)Ú"driver__assigned_vehicles__on_rideÚ0driver__assigned_vehicles__vehicle__vehicle_typeÚ!driver__assigned_vehicles__activeÚlast_known_location)r   r   )r   ÚobjectsÚfilterÚannotater   Úorder_byÚfirst)r;   r<   Úavailable_driversr$   r$   r%   r:   O   s   ý
ûù	r:   )Údjango.utilsr   Údatetimer   Ú&django.contrib.gis.db.models.functionsr   Úaccounts.modelsr   Úmodelsr   ÚnotificationsÚasgiref.syncr	   Údecimalr
   r   Úrest_framework.exceptionsr   r   r'   r&   r1   rI   r:   r$   r$   r$   r%   Ú<module>   s"    	
%