
    ph                     b   d dl Z d dlmZ d dlmZmZ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mZ d dlmZ  e j$                  e      Z	 	 	 dd	ed
ee   dee   deee      deeeef      deeef   fdZddededee   fdZ	 dd	ed
ee   dee   deee      dee   deeef   fdZy)    N)datetime)ListDictOptionalAny)Session)execute)get_db)MatchedTypeStatus)is_valid_crawl_outputdbcodesrun_keywordsdatesoutput_servicereturnc                   K   t        j                         }t        j                         }g g |j                         d|rt        |      ndd}	|sdt        j                  d       t        j                         j                         |	d<   t        j                         |z
  j                         |	d<   |	S t        |      D ]  \  }
}|
t        |      k\  s|
t        |      k\  s|
t        |      k\  r n||
   }||
   }t        ||      r:t        |||       |	d   j                  |       t        j                  d|        |r|j                  d	      nd
}t        j                  d| d|        |	d   j                  |        t        j                         }||z
  j                         }t        j                  d| d       |j                         |	d<   ||	d<   |	S w)a  
    Execute wine match job - Python version of the Groovy WineMatchJob.execute
    
    Args:
        db: Database session
        codes: List of retailer codes
        run_keywords: List of booleans indicating whether to run keyword matching
        dates: List of dates for crawl outputs
        output_service: Service for validating output files
        matcher_service: Service for matching
        logs_service: Service for logging matches
        
    Returns:
        Dict: Result summary
    Nr   )
successfulfailed
start_timeend_timetotal_countz$No codes provided for wine match jobr   duration_secondsr   zSuccessfully processed code z%Y-%m-%dnull[z] match input not found at: r   z*Wine Match Job completed. Execution time: z seconds)r   now	isoformatlenloggerwarningtotal_seconds	enumerater   execute_matcherappendinfostrftime)r   r   r   r   r   matcher_servicelogs_servicer   todayresultsicodedate
is_keyworddate_strr   durations                    `/var/www/html/wine-match-dev/backend/winematch-backend/src/apps/wine_match/job/wine_match_job.pyexecute_wine_match_jobr3      s    0 JLLNE  **,%*s5zG =>&lln668
'/||~
'B&Q&Q&S"# U# +4E
?a3|#44SZQx!!_
 !t,D*d3L!((.KK6tf=>48t}}Z0fHNNQtf$@
KLH$$T*%+0 ||~H:%446H
KK<XJhOP",,.GJ"*GNs   GG!r-   run_keywordr.   c                 |    t        t                     }t        j                  d|  d|        t	        || ||       y)z
    Execute the matcher service for the given code
    
    Args:
        code: The retailer code
        run_keyword: Whether to run keyword matching
        date: The date to use for matching (defaults to current date)
    zExecuting matcher for z
, keyword=N)nextr
   r    r&   r	   )r-   r4   r.   r   s       r2   r$   r$   [   s7     
fhB
KK(jFGBk4(    modec                    K   	 t        | |||       d{   }ddt        j                         j                          ||dS 7 .# t        $ r(}t
        j                  dt        |              |d}~ww xY ww)aw  
    Trigger the wine match job with the given parameters
    
    Args:
        db: Database session
        codes: List of retailer codes
        run_keywords: List of boolean flags indicating whether to run keyword matching
        dates: List of dates for crawl outputs
        mode: Operation mode
        
    Returns:
        Dict: Result of job trigger operation
    N	completedzwine-match-)statusjob_idr8   r+   z!Error triggering wine match job: )r3   r   r   	timestamp	Exceptionr    errorstr)r   r   r   r   r8   resultes          r2   trigger_wine_match_jobrC   p   s     (-b%uMM "#HLLN$<$<$>#?@	
 	
 N  8QABs8   A:A A-A A:A 	A7#A22A77A:)NNN)N)loggingr   typingr   r   r   r   sqlalchemy.ormr   ,src.apps.wine_match.services.matcher_servicer	   src.core.dependenciesr
   src.apps.match.enumsr   r   +src.apps.wine_match.services.output_servicer   	getLogger__name__r    r@   boolr3   r$   rC    r7   r2   <module>rO      s+     , , " @ " ( 4 M			8	$ 04JJ9J t*J "#	J
 T#s(^,J 
#s(^J\)# )D )8J )4   9  t*  "#	 
 3-  
#s(^ r7   