
    hJ                        d dl mZmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ  ed	d
g      Zej;                  de       ee       ee      fdededefd       Zej;                  dee          ee       ee      fdededefd       ZejA                  dee          ed d        eddd       ed       ed       ee       ee      fde!de!de	e!   de	e"   dedefd       Z#ejA                  de       ee       ee      fd e!dedefd!       Z$ej;                  d"e       ee       ee      fd#ededefd$       Z%ejA                  d"ee          ed d        eddd       ed       ee       ee      fde!de!de	e"   dedef
d%       Z&ej;                  d&       ee      fd'edefd(       Z'y))    )	APIRouterDependsHTTPExceptionQuery)Session)ListOptional)get_db)CommunicationCommunicationTemplateUser	Candidate)CommunicationCreateCommunicationBulkCreateCommunicationResponseCommunicationTemplateCreateCommunicationTemplateResponseAIMessageCompositionRequest)get_current_active_user)	AIServicez/communicationsCommunications)prefixtags/)response_modelcommunicationdbcurrent_userc                   K   |j                  t              j                  t        j                  | j                  k(        j                         }|st        dd      t        | j                  |j                  | j                  | j                  | j                        }|j                  |       |j                          |j                  |       |S w)z#Send a communication to a candidate  zCandidate not foundstatus_codedetailcandidate_iduser_idsubjectbodytype)queryr   filteridr%   firstr   r   r'   r(   r)   addcommitrefresh)r   r   r   	candidatedb_communications        G/home/www/40-admission.kofcorporation.com/app/routers/communications.pysend_communicationr4      s      #**9<<=;U;U+UV\\^I4IJJ$"//%% FFIIKJJ s   CCz/bulkc                   K   g }| j                   D ]  }|j                  t              j                  t        j                  |k(        j                         }|sIt        ||j                  | j                  | j                  | j                        }|j                  |       |j                  |        |j                          |D ]  }|j                  |        |S w)z*Send communications to multiple candidatesr$   )candidate_idsr*   r   r+   r,   r-   r   r'   r(   r)   r.   appendr/   r0   )r   r   r   communicationsr%   r1   r2   comms           r3   send_bulk_communicationr:   )   s      N%33 4HHY'..y|||/KLRRT	,)$%--"''"''  FF#$!!"234 IIK 


4 s   AC BC )ged      i  )r;   leNskiplimitr%   r)   c                 $  K   |j                  t              }|r"|j                  t        j                  |k(        }|r"|j                  t        j                  |k(        }|j                  |       j                  |      j                         }|S w)z"List communications with filtering)r*   r   r+   r%   r)   offsetr@   all)r?   r@   r%   r)   r   r   r*   r8   s           r3   list_communicationsrD   G   sv      HH]#E]77<GH]//478\\$'--e488:Ns   BBz/{communication_id}communication_idc                    K   |j                  t              j                  t        j                  | k(        j	                         }|st        dd      |S w)zGet a specific communicationr    zCommunication not foundr!   )r*   r   r+   r,   r-   r   )rE   r   r   r   s       r3   get_communicationrG   \   sM      HH]+22=3C3CGW3WX^^`M4MNNs   AAz
/templatestemplatec                    K   t        | j                  | j                  | j                  | j                  |j
                        }|j                  |       |j                          |j                  |       |S w)zCreate a communication template)namer'   r(   r)   
created_by)	r   rJ   r'   r(   r)   r,   r.   r/   r0   )rH   r   r   db_templates       r3   create_templaterM   i   s`      (]]  ]]]]??K FF;IIKJJ{s   A7A9c                   K   |j                  t              j                  t        j                  dk(        }|r"|j                  t        j                  |k(        }|j                  |       j                  |      j                         }|S w)zList communication templatesT)r*   r   r+   	is_activer)   rB   r@   rC   )r?   r@   r)   r   r   r*   	templatess          r3   list_templatesrQ   }   st      HH*+223H3R3RVZ3Z[E2774?@T"((/335Is   B
Bz/ai-composerequestc                 P   K   t        j                  |        d{   }d|iS 7 w)z!Generate message content using AINgenerated_message)r   compose_message)rR   r   messages      r3   compose_message_with_airW      s+      --g66G)) 7s   &$	&)(fastapir   r   r   r   sqlalchemy.ormr   typingr   r	   app.core.databaser
   
app.modelsr   r   r   r   app.schemas.communicationr   r   r   r   r   r   app.services.authr   app.services.ai_servicesr   routerpostr4   r:   getintstrrD   rG   rM   rQ   rW        r3   <module>rg      s   < < " ! $ L L 
 6 .	+3C2D	ES!67 &/ !89&  80 WT*?%@A &/ !89*  B: C%: ;<aAsqT*"'++&/ !89
 3- 3-	
 	  =( !2GH &/ !89			 	 I	 \*GH &/ !89)  I& L.K)LMaAsqT*+&/ !89
 3- 		
  N" ] !!89*(** *rf   