
    ߶h                        d dl m Z  d dlmZ d dl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 d d
lmZmZmZmZmZ d dlmZmZ d dlmZmZmZmZm Z  d dl! 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+m,Z,m-Z- d dl.m/Z/m0Z0 d dl1m2Z2 d dl3m4Z4 d dl5m6Z6m7Z7m8Z8 d dl9m:Z:m;Z;m<Z< d dl=m>Z> dede;fdZ?dede<fdZ@dede:fdZAdededd fd!ZBded"edefd#ZCy )$    )datetime)UUID)Session)HTTPExceptionstatus)APIException)Country)Producer)QueryPaginator)Region)WineDbWineWineDuplication	WineNoiseWineKeyword)RetailerWineRetailer)funccastIntegerand_or_)*)API_PREFIXES)get_producer_by_id)update_wine)WineUpdateRequestSchema)WineCreateSchemaWineOutputSchemaWineUpdateSchema)create_wine_duplicationget_wine_duplications_for_wine)WineDuplicationCreateSchema)RetailerWineCreateSchema))find_retailer_wine_by_sku_and_bottle_sizeget_retailer_wines_by_wine_idcreate_retailer_wine)AcceptMatchSchemaAdditionMatchCheckSchemaUndoMatchSchema)get_wines_for_wine_dbdbpayloadc           	        K   	 | j                  t              j                  t        j                  |j                  k(  t        j                  j                  d            }|j                         }|s| j                  t              j                  t        j                  |j                  k(  t        j                  j                  d            j                         }|rk| j                  t              j                  t        j                  |j                  k(  t        j                  j                  d            j                         nd}|}|rt        j                  |      ni }|r|j                  nd|r|j                  j                  nd|r|j                  nd|r|j                  j                  dS ddS # t        $ r-}t!        dt"        j$                  dt'        |      id      d}~ww xY ww)zQ
    Check if a wine with the given mongo_id already exists in the database.
    N)id
wine_db_idliteraldescriptioncheck_addition_matched	exceptionz Failed to check addition matchedmodulestatus_codeerrormessage)queryr   filtermongo_id
deleted_atis_firstr   r/   wine_idr   model_validatewine_dbr1   	wine_name	Exceptionr   r   HTTP_500_INTERNAL_SERVER_ERRORstr)r,   r-   r:   existing_winewine_duplicationwine_duplication_winees          m/var/www/html/wine-match-dev/backend/winematch-backend/src/apps/wine/external_input/service/external_input.pyr3   r3      s    
%%dmmw7G7G&GI\I\]aIbc!xx8??@X@X\c\l\l@ln}  oI  oI  oM  oM  NR  oS   T   Z   Z   \ FVBHHTN$9$9$''EUE]E]:]_c_n_n_r_rsw_x$y$$  %B  \`!1MJW(77F]_'4=##$6C-//220=},,4>K=00::	
 	
 RV	
 	
  
+==A'6	
 	

s5   G6F4F= 8G69F= <G6=	G3(G..G33G6c           	        K   	 | j                  t              j                  t        j                  |j                  k(  t        j
                  |j
                  k(  t        j                  j                  d            j                         }|s%| j                  t              j                  t        j
                  |j
                  k(  t        j                  j                  d            j                         }|s4t        dt        j                  |j                  |j
                  dd      t        | |       d{    |rk| j                  t              j                  t        j                  |j                  k(  t        j                  j                  d            j                         nd}nt        | |       d{   }|rt        j                   |      nd}|r|j                  nd|r|j"                  nd|r|j$                  j                  nd|r|j$                  j&                  dS ddS 7 7 t# t(        $ r-}t        dt        j*                  dt-        |      id      d}~ww xY ww)	z
    Undo the match for a given wine by its ID.
    This involves deleting associated retailer wines, wine duplications, and the wine itself.
    N
undo_match)r@   r<   z"Wine or Wine Duplication not foundr5   )r/   r1   r0   r2   r4   zFailed to undo match)r:   r   r;   r/   r@   r<   r=   r>   r?   r   r   r   HTTP_404_NOT_FOUNDsoft_delete_wine_duplicationclear_wine_match_identifiersr   rA   r1   rB   rC   rD   rE   rF   )r,   r-   winerH   rG   rJ   s         rK   rM   rM   ;   s5    %
xx~$$TWW%?RYRbRbAbdhdsdsdwdwx|d}~  E  E  G!xx8??@X@X\c\l\l@ln}  oI  oI  oM  oM  NR  oS   T   Z   Z   \#"' & 9 9&-oo7CSCST@	  /r3CDDD uE288D>((4D4L4L)LdooNaNabfNghnnp  KOD 6b$??DAE(77=4 (5=##$0=},,46C-//22>K=00::	
 	
 RV	
 	
 E
 @  
==A'*	
 	

s[   I2D:H9 >H5?BH9 H7A-H9 0I21H9 4I25H9 7H9 9	I/(I**I//I2c           
        K   	 t        | |j                  t        |j                  |j                  |j
                  |j                               d {   }|r|j                  nd |r|j                  nd |r|j                  nd |r$|j                  r|j                  j                  dS d dS 7 [# t        $ r-}t        dt        j                  dt!        |      id      d }~ww xY ww)N)r<   	wine_n_idvintagewine_literal)r,   r@   r-   )r/   r1   colorpatternaccept_matchr4   zFailed to accept matchr5   )update_wine_db_servicer0   r   r<   rS   rT   rU   r/   r1   rV   wine_keywordrW   rD   r   r   rE   rF   )r,   r-   datarJ   s       rK   rX   rX   k   s     
+&&+ ))!++$11		
 	
 #477'+t||#'TZZT48T=N=Nt((00	
 	
 UY	
 	
	
$  
!==A',	
 	

sH   C+AB2 B0AB2 +C+,B2 /C+0B2 2	C(;(C##C((C+rH   returnNc           	         K   	 t        j                         |_        | j                          | j	                  |       y# t
        $ r-}t        dt        j                  dt        |      id      d}~ww xY ww)zM
    Soft delete a wine duplication by setting its deleted_at timestamp.
    rO   r4   z!Failed to delete wine duplicationr5   N)
r   utcnowr=   commitrefreshrD   r   r   rE   rF   )r,   rH   rJ   s      rK   rO   rO      sh     

&.oo&7#
		


#$ 
1==A'7	
 	

s%   A8:? A8	A5(A00A55A8rQ   c           	         K   	 d|_         d|_        | j                          | j                  |       |S # t        $ r-}t        dt        j                  dt        |      id      d}~ww xY ww)zW
    Clear match identifiers (mongo_id, wine_n_id) for a wine and persist changes.
    NrP   r4   z&Failed to clear wine match identifiersr5   )	r<   rS   r_   r`   rD   r   r   rE   rF   )r,   rQ   rJ   s      rK   rP   rP      sj     

		


4 
1==A'<	
 	

s$   A.05 A.	A+(A&&A++A.)Dr   uuidr   sqlalchemy.ormr   fastapir   r   src.core.exceptionsr   src.apps.base.models.countryr	   &src.apps.wine.producer.models.producerr
   src.utils.paginationr   src.apps.base.models.regionsr   src.apps.wine.wine.models.winer   r   r   r   r   &src.apps.wine.retailer.models.retailerr   r   
sqlalchemyr   r   r   r   r   "src.apps.wine.wine.schemas.wine_dbsrc.utils.constantsr   (src.apps.wine.producer.services.producerr   #src.apps.wine.wine.services.wine_dbr   rY   r   src.apps.wine.wine.schemas.winer   r   r    ,src.apps.wine.wine.services.wine_duplicationr!   r"   +src.apps.wine.wine.schemas.wine_duplicationr#   ,src.apps.wine.retailer.schemas.retailer_winer$   -src.apps.wine.retailer.services.retailer_winer%   r&   r'   3src.apps.wine.external_input.schemas.external_inputr(   r)   r*    src.apps.wine.wine.services.winer+   r3   rM   rX   rO   rP        rK   <module>rz      s      " ) , 0 ; / / \ \ H 3 3 0 , G U F ` ` o S Q W  W z z B

%
@-
-
-
`


@
7 
o 
Z^ 
"
7 
$ 
4 
ry   