
    !+i	                     `    d Z ddlZddlmZ ddlmZ  ej                  e      Z G d d      Z	y)zF
Data Processor Service
Orchestrates daily data synchronization jobs.
    N)Session)
SyncEnginec                   6    e Zd ZdZdefdZd Zd Zd	defdZ	y)
DataProcessorServicezt
    Service that orchestrates daily sync operations.
    Used by scheduler/jobs.py for the run_daily_sync job.
    dbc                 0    || _         t               | _        y )N)r   r   sync_engine)selfr   s     I/var/www/hypershopcomercio.com.br/hyper-ai/app/services/data_processor.py__init__zDataProcessorService.__init__   s    %<    c                    t         j                  d       	 t         j                  d       | j                  j                          t         j                  d       | j                  j	                          t         j                  d       | j                  j                          t         j                  d       | j                  j                          t         j                  d       | j                  j                          t         j                  d       y	# t        $ r}t         j                  d|         d	}~ww xY w)
a  
        Runs the complete daily sync pipeline:
        1. Sync Ads (listings, prices, status)
        2. Sync Visits (CRITICAL - fetches visit data from ML API)
        3. Sync Orders (recent orders)
        4. Sync Stock (Tiny ERP stock levels)
        5. Recalculate Margins
        z(========== DAILY SYNC STARTED ==========z[1/5] Syncing Ads...z[2/5] Syncing Visits...z[3/5] Syncing Orders...z[4/5] Syncing Stock...z[5/5] Recalculating Margins...z*========== DAILY SYNC COMPLETED ==========zDaily sync failed: N)
loggerinfor	   sync_adssync_visitssync_orderssync_tiny_stocksync_margins	Exceptionerror)r
   es     r   sync_daily_dataz$DataProcessorService.sync_daily_data   s     	>?	KK./%%' KK12((* KK12((* KK01,,. KK89))+KKDE 	LL.qc23	s   D D 	D?!D::D?c                     t         j                  d       | j                  j                          t         j                  d       y)zQ
        Syncs only visits data. Useful for manual trigger or debugging.
        zSyncing visits only...zVisits sync completed.N)r   r   r	   r   )r
   s    r   sync_visits_onlyz%DataProcessorService.sync_visits_only<   s0     	,-$$&,-r   lookback_hoursc                     t         j                  d| d       | j                  j                  |       t         j                  d       y)z?
        Syncs only orders with configurable lookback.
        zSyncing orders (lookback=zh)...)r   zOrders sync completed.N)r   r   r	   sync_orders_incremental)r
   r   s     r   sync_orders_onlyz%DataProcessorService.sync_orders_onlyD   s?     	//?uEF000O,-r   N)0   )
__name__
__module____qualname____doc__r   r   r   r   intr    r   r   r   r      s*    
(7 ($L..s .r   r   )
r$   loggingsqlalchemy.ormr   app.services.sync_enginer   	getLoggerr!   r   r   r&   r   r   <module>r+      s1     " /			8	$>. >.r   