
    Cj	                         d Z ddlmZ ddlmZmZ 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  e       Z ee      Zej-                  d
ddeii       ee      fdededefd       Zy)u  
GET /api/v1/tasks/{task_id}

Polling endpoint for both real Celery tasks and synthetic cache-hit
"tasks" (see analyze.py for why cache hits get a synthetic task id —
it keeps the client integration to a single polling pattern regardless
of whether the result came from cache or a fresh computation).

Accepts either a logged-in customer (JWT) or a service API key — same
dual-auth pattern as /analyze (see app/core/dual_auth.py), since this
is the immediate next call a SaaS dashboard user makes after submitting
an analysis.
    )AsyncResult)	APIRouterDepends)AuthContextget_auth_context)TaskNotFoundError)
get_logger)ErrorResponse)cache_service)
celery_appz/tasks/{task_id}i  model)	responsestask_idauthreturnc                   K   | j                  d      r5t        j                  d|         d {   }|t        d      | d|d dS t	        | t
              }|j                  dk(  r| d	d d dS |j                  d
k(  r| dd d dS |j                  dk(  r| d|j                  d dS |j                  dk(  r t        j                  d|  d       | dd ddS | dd d dS 7 w)Nzcached-ztask_result:zWThis cached result has expired or does not exist. Please submit a new analysis request.)message	completed)r   statusresulterror)appPENDINGqueuedSTARTEDrunningSUCCESSFAILUREzTask z reported failure statefailedz]Analysis could not be completed. Please try again or contact support if the problem persists.)

startswithr   getr   r   r   stater   loggerwarning)r   r   cachedr   s       GC:\Crop_Prediction\Backend\crop-ai-system\app\api\v1\endpoints\tasks.pyget_task_statusr'      s    
 )$$((<y)ABB>#@  #kVVZ[[j1F||y "h$QUVV||y "i4RVWW||y "kV]]]abb||y wi'>?@0	
 	
 )tdSS; Cs   -CCB-CN)__doc__celery.resultr   fastapir   r   app.core.dual_authr   r   app.core.exceptionsr   app.core.logging_configr	   app.schemas.responser
   app.services.cache_servicer   app.workers.celery_appr   router__name__r#   r!   strdictr'        r&   <module>r7      s    & & < 1 . . 4 -		H	 Wm,-   =DDT<U T3 Tk TZ^ T	Tr6   