
    !+i                     |    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlZ e j                  e
      Z G d d      Zy)    N)Session)CompetitorAd)MeliApiService)datetimec                   P    e Zd ZdefdZdedefdZdedefdZd	efd
Zd	efdZ	y)CompetitionEnginedbc                 4    || _         t        |      | _        y )N)
db_session)r	   r   meli_service)selfr	   s     M/var/www/hypershopcomercio.com.br/hyper-ai/app/services/competition_engine.py__init__zCompetitionEngine.__init__   s    *b9    urlreturnc                     t        j                  d|      }|st        j                  d|      }|r!|j                  d      j                  dd      S y )Nz(MLB\d{9,})z(MLB-\d{9,})   - )researchgroupreplace)r   r   matchs      r   extract_id_from_urlz%CompetitionEngine.extract_id_from_url   sF    		.#.IIos3EKKN**333r   my_ad_idcompetitor_urlc                    | j                  |      }|sK|j                         j                  d      r!|j                         j                  dd      }nt	        d      | j
                  j                  t              j                  t        j                  |k(  t        j                  |k(        j                         }|r|S 	 | j                  j                  |g      }|st	        d      |d   }t        |||j                  d      t        |j                  dd            |j                  d	      rt        |j                  d	            nd |j                  d
      d|j                  d      t!        j"                         	      }| j
                  j%                  |       | j
                  j'                          |S # t(        $ r}t*        j-                  d| d|        t        ||d| dd|ddt!        j"                               }| j
                  j%                  |       | j
                  j'                          |cY d }~S d }~ww xY w)NMLBr   r   z"Could not extract MLB ID from LinkzCompetitor ID not found in APIr   titlepriceoriginal_price	permalink
Competitorstatus)	competitor_idad_idr!   r"   r#   r$   seller_namer&   last_updatedz!Failed to fetch initial data for z: u   Aguardando Sincronização ()g        pending)r'   r(   r!   r"   r$   r)   r&   r*   )r   upper
startswithr   
ValueErrorr	   queryr   filterr(   r'   firstr   get_item_detailsgetfloatr   nowaddcommit	Exceptionloggererror)	r   r   r   comp_idexistsdetails_listitemcompes	            r   add_competitorz CompetitionEngine.add_competitor   s   **>:##%007)//199#rB!"FGG |,33(*&&'1
 %' 	
 M%	,,==wiHL !ABB?D%hhw'DHHWa01DHHHM]D^uTXX.>%?@dh((;/(xx)%\\^
D GGKKGGNNK 	LL<WIRsKL%4WIQ?(( %\\^	D GGKKGGNNK	s    C;F? ?	IA:IIIr(   c                     | j                   j                  t              j                  t        j                  |k(        j                         S )N)r	   r0   r   r1   r(   all)r   r(   s     r   get_competitorsz!CompetitionEngine.get_competitors\   s4    ww}}\*11,2D2D2MNRRTTr   c                    | j                  |      }|syd}|D cg c]  }|j                  s|j                   }}|sy	 | j                  j                  |      }|st        j                  d|        y|D ci c]  }|s|j                  d      | }}|D ]  }|j                  |j                        }|rt        |j                  dd            }	|j                  d      rt        |j                  d            nd}
|j                  dd      }|j                  d	|j                        }|	dkD  r|	|_	        |
r|
|_
        ||_        ||_        t        j                         |_        |d
z  }t        j                  d|j                           | j                  j!                          |S c c}w c c}w # t"        $ r#}t        j%                  d|        Y d}~|S d}~ww xY w)zW
        Updates prices for all competitors of a specific ad using the ML API.
        r   z-No data returned from API for competitors of idr"   r#   Nr&   activer!   r   zNo API data for competitor z$Error updating competitors via API: )rE   r'   r   r3   r:   warningr4   r5   r!   r"   r#   r&   r   r6   r*   r	   r8   r9   r;   )r   r(   competitorsupdated_countr@   comp_idsr>   r?   details_map	new_pricenew_original
new_status	new_titlerA   s                 r   update_competitor_pricesz*CompetitionEngine.update_competitor_prices_   s    **51 4?U4$BTBTD&&UU"	E,,==hGL!NugVW =IQDD488D>4/QKQ# W"t'9'9: %dhhw&: ;IHLQaHb52B)C#DhlL!%(H!=J $$** =I !1}%.
#.:+",DK!*DJ(0D%!Q&MNN%@ASAS@T#UV%W( GGNN
 S V R0  	ELL?sCDD	EsA   F3F35F= 7F= ;F8F8DF= 8F= =	G)G$$G)N)
__name__
__module____qualname__r   r   strr   rB   rE   rR    r   r   r   r      sO    :7 :	s 	s 	?s ?C ?BUS U4c 4r   r   )loggingsqlalchemy.ormr   app.models.competitor_adr   app.services.meli_apir   r   r   	getLoggerrS   r:   r   rW   r   r   <module>r]      s5     " 1 0  				8	$H Hr   