o
    š"\i]
  ã                   @   sV   d 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
 G dd„ dƒZd	S )
zPrediction repositoryé    )ÚOptionalÚList)Údatetime)ÚSession)Ú
Prediction)ÚMatchc                
   @   s´   e Zd ZdZdefdd„Zddedee dee	 fd	d
„Z
ddedee dee	 fdd„Z			ddee dee dedee	 fdd„Zde	de	fdd„Zdedede	fdd„ZdS )ÚPredictionRepositoryz$Repository for prediction operationsÚdbc                 C   s
   || _ d S ©N)r	   )Úselfr	   © r   úm/var/www/www-root/data/www/176.119.141.140/sports-predictor/app/core/db/repositories/prediction_repository.pyÚ__init__   s   
zPredictionRepository.__init__NÚmatch_idÚmodel_idÚreturnc                 C   s4   | j  t¡ tj|k¡}|r| tj|k¡}| ¡ S )zGet predictions for a match)r	   Úqueryr   Úfilterr   r   Úall)r   r   r   r   r   r   r   Úget_by_match_id   s   z$PredictionRepository.get_by_match_idc                 C   sD   | j  t¡ tj|k¡}|dur| tj|k¡}| tj ¡ ¡ 	¡ S )zDGet latest prediction for a match (optionally for a specific model).N)
r	   r   r   r   r   r   Úorder_byÚcalculated_atÚdescÚfirst)r   r   r   Úqr   r   r   Úget_latest_for_match   s   z)PredictionRepository.get_latest_for_matchéd   Ú	league_idÚlimitc                 C   s\   | j  t¡ t¡ tjdk¡}|r| tj|k¡}|r#| tj|k¡}| 	tj
¡ |¡ ¡ S )zGet upcoming predictionsÚ	scheduled)r	   r   r   Újoinr   r   Ústatusr   r   r   Ú
match_dater   r   )r   r   r   r   r   r   r   r   Úget_upcoming   s   ÿz!PredictionRepository.get_upcomingÚ
predictionc                 C   s&   | j  |¡ | j  ¡  | j  |¡ |S )zCreate a new prediction)r	   ÚaddÚcommitÚrefresh)r   r$   r   r   r   Úcreate0   s   
zPredictionRepository.createc                 K   sŒ   | j  t¡ tj|ktj|k¡ ¡ }|r)| ¡ D ]
\}}t|||ƒ qt	 
¡ |_ntd||dœ|¤Ž}| j  |¡ | j  ¡  | j  |¡ |S )zUpdate or create prediction)r   r   Nr   )r	   r   r   r   r   r   r   ÚitemsÚsetattrr   Úutcnowr   r%   r&   r'   )r   r   r   Úkwargsr$   ÚkeyÚvaluer   r   r   Úupdate_or_create7   s   þý
z%PredictionRepository.update_or_creater
   )NNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Úintr   r   r   r   r   r#   r(   r/   r   r   r   r   r   	   s&      	üþýü
ûr   N)r3   Útypingr   r   r   Úsqlalchemy.ormr   Úapp.core.db.models.predictionr   Úapp.core.db.models.matchr   r   r   r   r   r   Ú<module>   s    