o
    "it"                     @   sz   d Z ddl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mZ eeZG dd dZd	e	d
efddZdS )za
Expert Profile Manager Service
Manages expert profiles, contributions, and performance tracking
    N)OptionalListDictAny)datetime)Session)funcdescc                   @   s  e Zd ZdZdefddZ		d/dedee dee d	e	e d
e	e
 de
eef fddZ			d0dede	ee  de	ee  d	e	e de
eef f
ddZ	d1dedede	e de
eef fddZdedede
eef fddZdede
eef fddZ		d2dededee
eef  fddZ	d3ded edee
eef  fd!d"Z	d1ded#e	e dee
eef  fd$d%Zd&efd'd(Zd)d* Zde
eef fd+d,Zde
eef fd-d.ZdS )4ExpertProfileManagerz0Expert profile and contribution tracking servicedbc                 C   s
   || _ d S Nr   )selfr    r   U/lsinfo/ai/hellotax_ai/training_center/backend/app/services/expert_profile_manager.py__init__   s   
zExpertProfileManager.__init__Nuser_idexpertise_areasskillsbiometadatareturnc                 C   sf   ddl m} |||||dddd|pi d	}| j| | j  | j| td|  | |S )a*  
        Create expert profile

        Args:
            user_id: User ID
            expertise_areas: List of expertise areas
            skills: List of skills
            bio: Expert biography
            metadata: Additional metadata

        Returns:
            Created profile data
        r   ExpertProfile        )	r   r   r   r   total_annotationstotal_reviewsavg_quality_scorereputation_score	meta_dataz Created expert profile for user )	
app.modelsr   r   addcommitrefreshloggerinfo_profile_to_dict)r   r   r   r   r   r   r   profiler   r   r   create_profile   s"   

z#ExpertProfileManager.create_profilec                 C   s   ddl m} | j||j|k }|std| d|dur%||_|dur,||_	|dur3||_
t |_| j  | j| td|  | |S )zUpdate expert profiler   r   Profile for user 
 not foundNz Updated expert profile for user )r    r   r   queryfilterr   first
ValueErrorr   r   r   r   utcnow
updated_atr"   r#   r$   r%   r&   )r   r   r   r   r   r   r'   r   r   r   update_profile>   s&   


z#ExpertProfileManager.update_profiletask_idquality_scorec                 C   s   ddl m}m} | j||j|k }|s td| d||d||d}| j	| | j
d7  _
|rB| || | | | j  | j| td| d	|  | |S )
zRecord annotation contributionr   r   ExpertContributionr)   r*   
annotation)r   contribution_typer2   r3      zRecorded annotation for user , task )r    r   r5   r   r+   r,   r   r-   r.   r!   r   _update_avg_quality_update_reputationr"   r#   r$   r%   r&   )r   r   r2   r3   r   r5   r'   contributionr   r   r   record_annotation^   s.   


z&ExpertProfileManager.record_annotationc                 C   s   ddl m}m} | j||j|k }|s td| d||d|d}| j	| | j
d7  _
| | | j  | j| td| d	|  | |S )
zRecord review contributionr   r4   r)   r*   review)r   r7   r2   r8   zRecorded review for user r9   )r    r   r5   r   r+   r,   r   r-   r.   r!   r   r;   r"   r#   r$   r%   r&   )r   r   r2   r   r5   r'   r<   r   r   r   record_review   s(   


z"ExpertProfileManager.record_reviewc                 C   sF   ddl m} | j||j|k }|std| d| |S )zGet expert profiler   r   r)   r*   )	r    r   r   r+   r,   r   r-   r.   r&   )r   r   r   r'   r   r   r   get_profile   s   
z ExpertProfileManager.get_profiler   
   metriclimitc                    sL   ddl m} t|||j} j|t||	 } fdd|D S )z
        Get expert leaderboard

        Args:
            metric: Ranking metric (reputation_score, total_annotations, avg_quality_score)
            limit: Number of results

        Returns:
            List of top experts
        r   r   c                       g | ]}  |qS r   r&   .0pr   r   r   
<listcomp>       z8ExpertProfileManager.get_leaderboard.<locals>.<listcomp>)
r    r   getattrr   r   r+   order_byr	   rC   all)r   rB   rC   r   order_columnprofilesr   rI   r   get_leaderboard   s   
z$ExpertProfileManager.get_leaderboardr   expertise_areamin_quality_scorec                    sR   ddl m}  j||j|g|j|kt	|j
 } fdd|D S )zFind experts by expertise arear   r   c                    rD   r   rE   rF   rI   r   r   rJ      rK   zBExpertProfileManager.find_experts_by_expertise.<locals>.<listcomp>)r    r   r   r+   r,   r   containsr   rM   r	   r   rN   )r   rR   rS   r   rP   r   rI   r   find_experts_by_expertise   s   z.ExpertProfileManager.find_experts_by_expertiser7   c                    s^   ddl m}  j||j|k}|r||j|k}|t|j	
 } fdd|D S )zGet expert contributionsr   )r5   c                    rD   r   )_contribution_to_dict)rG   crI   r   r   rJ      rK   z:ExpertProfileManager.get_contributions.<locals>.<listcomp>)r    r5   r   r+   r,   r   r7   rM   r	   
created_atrN   )r   r   r7   r5   r+   contributionsr   rI   r   get_contributions   s   z&ExpertProfileManager.get_contributions	new_scorec                 C   s*   |j }|jpd}||d  | | |_dS )zUpdate average quality scorer   r8   N)r   r   )r   r'   r[   totalcurrent_avgr   r   r   r:      s   
z(ExpertProfileManager._update_avg_qualityc                 C   s(   |j d |jd  |jpdd  |_dS )zCalculate reputation scorerA      r   d   N)r   r   r   r   r   r'   r   r   r   r;      s   z'ExpertProfileManager._update_reputationc                 C   sR   |j |j|j|j|j|j|j|j|j|j	r|j	
 nd|jr#|j
 nd|jdS )zConvert profile model to dictN)idr   r   r   r   r   r   r   r   rX   r0   r   )ra   r   r   r   r   r   r   r   r   rX   	isoformatr0   r   r`   r   r   r   r&      s   z%ExpertProfileManager._profile_to_dictc                 C   s0   |j |j|j|j|j|jr|j dS ddS )z"Convert contribution model to dictN)ra   r   r7   r2   r3   rX   )ra   r   r7   r2   r3   rX   rb   )r   r<   r   r   r   rV     s   z*ExpertProfileManager._contribution_to_dict)NN)NNNr   )r   rA   )r   )__name__
__module____qualname____doc__r   r   intr   strr   r   r   r(   r1   floatr=   r?   r@   rQ   rU   rZ   r:   r;   r&   rV   r   r   r   r   r
      s    

-



$

%




r
   r   r   c                 C   s   t | S )z#Get expert profile manager instance)r
   r   r   r   r   get_expert_profile_manager  s   rj   )rf   loggingtypingr   r   r   r   r   sqlalchemy.ormr   
sqlalchemyr   r	   	getLoggerrc   r$   r
   rj   r   r   r   r   <module>   s    
  