
    is               
          d dl Z d dlZd dl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 d dlmZ d dlmZmZmZ d dlmZmZmZmZmZ d d	lmZ  e       Ze j<                  j?                  e j<                  jA                  e j<                  jA                  e j<                  jA                  e!                  d
d      Z" e jF                  e"d       d(de$dede%dz  de$fdZ&ejO                  de       e
d       e	d       ee       ee      fde$dedz  dedefd       Z(ejS                  dee          ee       ee      fdedefd       Z*ejS                  de       ee       ee      fde%dedefd       Z+ejY                  de       ee       ee      fde%dededefd       Z-ej]                  d       ee       ee      fde%dedefd        Z/ejS                  d!ee          ee       ee      fde%dedefd"       Z0ejS                  d#e       ee      fde$defd$       Z1ejO                  d%e       ee      fde$d&edefd'       Z2y))    N)List)	APIRouterDependsHTTPException
UploadFileFileForm)Session)get_db)get_current_company)CompanyJobOfferJobApplication)JobOfferResponseJobOfferPublicResponseJobOfferUpdateJobApplicationCreateJobApplicationResponse)job_offer_serviceuploads
job_offersT)exist_oktagdb
exclude_idreturnc                     | }d}	 |j                  t              j                  t        j                  | k(        }|r"|j                  t        j                  |k7        }|j                         s| S | d| } |dz  }x)z7Ensure the tag is unique, appending a suffix if needed.   -   )queryr   filterr   idfirst)r   r   r   base_tagcounterr!   s         [/Users/theessential/Documents/Github/Quarante/therecruiter-backend/app/job_offers/router.py_ensure_unique_tagr(      sw    HG
"))(,,#*=>LL
!:;E{{}J
!G9%1     /)response_model.promptimagecurrent_companyc                 R  K   d}d}|r|j                    dt        t        j                                d|j                   }t        j
                  j                  t        |      }t        |d      5 }|j                          d{   }|j                  |       ddd       d| }t        j                  | |       d{   }	t        |	d   |      }
t        |j                   |
|	j                  dd      |	j                  dd      |	j                  d	g       |	j                  d
g       |	j                  d      |	j                  d      |	j                  d      |
      }|j!                  |       |j#                          |j%                  |       |S 7 # 1 sw Y   xY w7 w)uR   Créer une offre d'emploi via IA à partir d'un prompt et d'une image optionnelle.N_wbz/uploads/job_offers/r   title descriptionrequirementsresponsibilitieslocationsalary_rangecontract_type)

company_idr   r2   r4   r5   r6   r7   r8   r9   	image_url)r#   inttimefilenameospathjoin
UPLOAD_DIRopenreadwriter   generate_job_offerr(   r   getaddcommitrefresh)r,   r-   r   r.   
image_pathr;   r>   fcontent	generatedr   	job_offers               r'   create_job_offerrP   ,   su     JI%(()3tyy{+;*<Aenn=MNWW\\*h7
*d# 	q!JJL(GGGG	 +8*5	 (::6:NNI Yu-r
2C"%%mmGR(MM-4]]>26"'92>z*]]>2mmO4I FF9IIKJJy3 )	 	 Os=   A0F'2FFF&F'F%CF'FF"F'c                     | j                  t              j                  t        j                  |j                  k(        j                  t        j                  j                               j                         S )u4   Lister toutes les offres de l'entreprise connectée.)	r!   r   r"   r:   r#   order_by
created_atdescall)r   r.   s     r'   list_job_offersrV   W   sP     		##'9'99	:	(%%**,	-		r)   z/{offer_id}offer_idc                     |j                  t              j                  t        j                  | k(  t        j                  |j                  k(        j                         }|st        dd      |S )u3   Récupérer le détail d'une offre de l'entreprise.     Offre non trouvéestatus_codedetail)r!   r   r"   r#   r:   r$   r   rW   r   r.   offers       r'   get_job_offerr`   e   sZ     		x')<)<@R@R)R	S	 

 4HIILr)   update_datac                 j   |j                  t              j                  t        j                  | k(  t        j                  |j                  k(        j                         }|st        dd      |j                  d      }d|v rh|d   j                         j                         }dj                  d |D              }|j                  d	      d
d }t        |||j                        |d<   |j                         D ]  \  }}t        |||        |j                          |j                  |       |S )z&Modifier une offre d'emploi existante.rY   rZ   r[   T)exclude_unsetr   r3   c              3   N   K   | ]  }|j                         s|d k(  r|nd   yw)r   N)isalnum).0cs     r'   	<genexpr>z#update_job_offer.<locals>.<genexpr>   s#     I199;!s(a;Is   #%r   N   )r   )r!   r   r"   r#   r:   r$   r   
model_dumplowerstriprA   r(   itemssetattrrI   rJ   )	rW   ra   r   r.   r_   update_dictr   fieldvalues	            r'   update_job_offerrr   v   s    		x')<)<@R@R)R	S	 

 4HII((t(<K % &&(..0ggISIIiinSb!/REHHME#))+ %uue$% IIKJJuLr)   c           	         |j                  t              j                  t        j                  | k(  t        j                  |j                  k(        j                         }|st        dd      |j                  rt        j                  j                  t        j                  j                  t        j                  j                  t        j                  j                  t                          |j                  j                  d            }t        j                  j                  |      rt        j                  |       |j!                  |       |j#                          ddiS )zSupprimer une offre d'emploi.rY   rZ   r[   r*   r]   u   Offre supprimée avec succès)r!   r   r"   r#   r:   r$   r   r;   r?   r@   rA   dirname__file__lstripexistsremovedeleterI   )rW   r   r.   r_   img_paths        r'   delete_job_offerr{      s     		x')<)<@R@R)R	S	 

 4HII 77<<PX@Y0Z []b]l]l]s]stw]xy77>>(#IIhIIeIIK566r)   z/{offer_id}/applicationsc                     |j                  t              j                  t        j                  | k(  t        j                  |j                  k(        j                         }|st        dd      |j                  S )u/   Lister les candidatures reçues pour une offre.rY   rZ   r[   )r!   r   r"   r#   r:   r$   r   applicationsr^   s       r'   list_applicationsr~      sa     		x')<)<@R@R)R	S	 

 4HIIr)   z/by-tag/{tag}c                 .   |j                  t              j                  t        j                  | k(  t        j                  dk(        j                         }|st        dd      t        j                  |      }|j                  j                  |_
        |S )uA   Récupérer une offre par son tag (utilisé par le bot WhatsApp).TrY      Offre non trouvée ou inactiver[   )r!   r   r"   r   	is_activer$   r   r   model_validatecompanycompany_name)r   r   r_   responses       r'   get_offer_by_tagr      sv     		#X%7%74%?	@	 

 4TUU%44U;H!MM66HOr)   z/by-tag/{tag}/applyapplicationc                 d   |j                  t              j                  t        j                  | k(  t        j                  dk(        j                         }|st        dd      |j                  t              j                  t        j                  |j                  k(  t        j                  |j                  k(        j                         }|rt        dd      t        |j                  |j                        }|j                  |       |j                          |j                  |       |S )uR   Soumettre une candidature à une offre via son tag (utilisé par le bot WhatsApp).TrY   r   r[   i  u(   Vous avez déjà postulé à cette offre)job_offer_iduser_id)r!   r   r"   r   r   r$   r   r   r   r#   r   rH   rI   rJ   )r   r   r   r_   existingjob_applications         r'   apply_to_offerr      s     		#X%7%74%?	@	 

 4TUU 	 	''5883""k&9&99

 
  4^__$XX##O FF?IIKJJr)   )N)3r?   r=   typingr   fastapir   r   r   r   r   r	   sqlalchemy.ormr
   app.core.databaser   app.auth.dependenciesr   
app.modelsr   r   r   app.job_offers.schemasr   r   r   r   r   app.job_offers.servicer   routerr@   rA   rt   ru   rB   makedirsstrr<   r(   postrP   rG   rV   r`   patchrr   ry   r{   r~   r   r    r)   r'   <module>r      s#   	   M M " $ 5 8 8  5	WW\\"''//"''//"''//(:S*TUW`bno
 J &C W #* PS $ S!12s)#Dz&/&':;	''' 	' 	' 3'T C%5 67&/&':;


 8
 M*:; &/&':;  <  m,<= &/&':;	 	 	 >@ } &/&':;777 7 72 &t<R7ST &/&':;  U( O,BC-4V_ # 7  D "3IJ &/!	!%! 	! K!r)   