
    !+i                         d Z ddlmZmZ ddlZddlmZ ddlmZ ddlm	Z	  ej                  e      Z e       Z e       ZdZ e	j"                  dd	g
      d        Z e	j"                  ddg
      d        Zy)zt
Webhook endpoint for Mercado Livre notifications.
Receives real-time order updates and queues them for processing.
    )requestjsonifyN)Queue)datetime)api_bpi'  z/webhooks/mlPOST)methodsc            	         	 t        j                  d      xs i } | j                  dd      }| j                  dd      }| j                  d      }| j                  dd      }| j                  d	d      }| d
| d
| }|t        v r't        j                  d|        t        ddi      dfS t        j                  d| d| d|        g d}||vr't        j                  d|        t        ddi      dfS t        j                  |||||t        j                         j                         d       t        j                  |       t        t              t        kD  r5t!        t              dt        dz   }|D ]  }	t        j#                  |	        t        ddi      dfS # t$        $ r;}
t        j'                  d|
        t        dt)        |
      d      dfcY d}
~
S d}
~
ww xY w)a  
    Receive Mercado Livre webhook notification.
    Must respond within 500ms to avoid retries.
    
    Expected payload:
    {
        "resource": "/orders/2000014403184862",
        "user_id": 123456789,
        "topic": "orders_v2",
        "application_id": 7890123456789012,
        "attempts": 1,
        "sent": "2025-12-23T12:00:00.000-03:00",
        "received": "2025-12-23T12:00:00.500-03:00"
    }
    T)silentresource topicuser_idattempts   sent:z#[WEBHOOK] Duplicate event ignored: status	duplicate   z[WEBHOOK] Received: topic=z, resource=z, attempts=)	orders_v2items	questionsmessagespaymentsz[WEBHOOK] Unsupported topic: unsupported_topic)r   r   r   r   r   received_atN   queuedz$[WEBHOOK] Error processing webhook: error)r   message)r   get_jsongetprocessed_eventsloggerinfor   warningwebhook_queueputr   utcnow	isoformataddlenMAX_PROCESSED_CACHElistdiscard	Exceptionr    str)datar   r   r   r   r   event_idsupported_topics	to_removeitemes              H/var/www/hypershopcomercio.com.br/hyper-ai/app/api/endpoints/webhooks.pyreceive_ml_webhookr:      s   "2Dt,288J+"%((9%88J*xx# WAhZq/''KK=hZHIHk23S88 	0{8*KX`Wabc W((NN:5'BCH&9:;S@@ 	  #??,668
 	 	X&  #66-./H0Cq0HII! / ((./ (+,c11 D;A3?@'c!f=>CCDs,   B+F& .AF& ;B*F& &	G*/0G%G*%G*z/webhooks/statusGETc                      ddl m}  ddlm} ddlm}  |        }d}	 |j                  |      j                  |j                  dk(  |j                  dk(        j                   ||j                              j                         }|r&|j                  r|j                  j                         }|j                          t        t         j#                         t%        t&              |d      S # |j                          w xY w)	zGet webhook processing status.r   )SessionLocal)SyncJob)descNorders	completed)
queue_sizeprocessed_countlast_processed_at)app.core.databaser=   app.models.syncr>   
sqlalchemyr?   queryfilterentityr   order_byfinished_atfirstr+   closer   r(   qsizer-   r$   )r=   r>   r?   dblast_tslast_jobs         r9   webhook_statusrS   \   s     /'	BG88G$++GNNh,FZeHefooptu|  vI  vI  qJ  K  Q  Q  S,,++557W

#))+/0$   	
s   BC+ +C=)__doc__flaskr   r   loggingqueuer   r   app.apir   	getLogger__name__r%   r(   setr$   r.   router:   rS        r9   <module>r_      s    #    			8	$  5   nvh/BD 0BDJ  5'2 3r^   