o
    3Bi                     @   sf  d dl mZmZmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlZd dlZd dl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 d d
lmZ d dlmZ ejejd eeZ edddZ!eddZ"edddZ#eddZ$e!%ededefddZ&e!'ddefddZ(G dd deZ)G dd deZ*G dd  d eZ+G d!d" d"eZ,G d#d$ d$eZ-G d%d& d&eZ.G d'd( d(eZ/G d)d* d*eZ0G d+d, d,eZ1G d-d. d.eZ2G d/d0 d0eZ3G d1d2 d2eZ4d d3l5m6Z6 G d4d5 d5eZ7G d6d7 d7eZ8G d8d9 d9e2Z9d:e:d;e:d<e:d=eee:e
f  d>e;d?ee- d@ee. dAe2fdBdCZ<e!j=dDe2dEde/fdFdGZ>e!j=dHe9dEde8fdIdJZ?e!j=dKe+dEde*fdLdMZ@dNdOdPdQdRdSdQdTdUdQdVdWdQdXdYdQdZd[dQgd\ZAd]d^d[d_d`dadbdcgdddedfdgdhdidgdjdkdgdld`dgdmdndggdodpdpdqg drdsdtdtdudvdwgdrdxdydzd{g d|drgd}ZBdS )~    )FastAPIHTTPExceptionRequest)	BaseModel)ListDictOptionalAnyUnionN)asdict)JSONResponse)RequestValidationError)SkillAnalysisSystem)DomainAwareCourseFilter) OnlineCourseRecommendationSystem)course_cache)levelzTNA System APIz1.0.0)titleversionz
gemma3:12b)
model_name   )r   
batch_sizerequestexcc                    s*   t d|  td| |jddS )NzValidation error: i  )detailbody)status_codecontent)loggererrorr   errorsr   )r   r    r!   6/var/www/eduai.edurigo.com/tna/testing/tna_fast_api.pyvalidation_exception_handler   s   r#   httpc                    sT   | j jdkr!|  I d H  td     fdd}|| _|| I d H }|S )N/analyze-skills-courseszRaw request body: c                      s   d dS )Nzhttp.request)typer   r!   r!   r   r!   r"   receive+   s   
zlog_requests.<locals>.receive)urlpathr   r   infodecode_receive)r   	call_nextr(   responser!   r'   r"   log_requests$   s   r0   c                   @      e Zd ZU eed< eed< dS )
DepartmentidnameN__name__
__module____qualname__int__annotations__strr!   r!   r!   r"   r2   4      
 r2   c                   @   s"   e Zd ZU eed< ee ed< dS )DepartmentRequestuser_promptorganization_departmentsN)r6   r7   r8   r;   r:   r   r2   r!   r!   r!   r"   r=   8   s   
 r=   c                   @   s.   e Zd ZU eed< eed< dZee ed< dS )DepartmentResponseidentified_departmentdepartment_idnone
confidenceN)r6   r7   r8   r;   r:   r9   rD   r   r!   r!   r!   r"   r@   <      
 r@   c                   @   s>   e Zd ZU eed< eed< dZee ed< dZee ed< dS )FilteredSkillskill_id
skill_nameNskill_levelskill_count)	r6   r7   r8   r9   r:   r;   rI   r   rJ   r!   r!   r!   r"   rF   A   s
   
 rF   c                   @   r1   )AllSkillDatarG   rH   Nr5   r!   r!   r!   r"   rK   G   r<   rK   c                   @   sJ   e Zd ZU eed< eed< eed< eed< eeeeeef f  ed< dS )OfflineCourse	course_idcourse_nameshort_descriptiondescriptionskillsN)	r6   r7   r8   r;   r:   r   r
   r   r	   r!   r!   r!   r"   rL   K   s   
  rL   c                   @   sZ   e Zd ZU eed< eed< eed< eeeef  ed< eed< ee	 ed< ee
 ed< dS )	SkillsCourseRequest
user_query
admin_namedepartment_namefiltered_skillstotal_users_in_departmentall_skills_dataoffline_coursesN)r6   r7   r8   r;   r:   r   r   r	   r9   rK   rL   r!   r!   r!   r"   rR   R   s   
 rR   c                   @   s.   e Zd ZU eed< eed< eed< eed< dS )CourseRecommendationrM   r4   reason
percentageN)r6   r7   r8   r;   r:   floatr!   r!   r!   r"   rZ   [   s
   
 rZ   c                   @   sr   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< eed	< ee ed
< eed< eed< dS )OnlineCourseRecommendationr   platform
instructordurationrI   rP   r)   priceratingskills_coveredrelevance_scorer[   N)r6   r7   r8   r;   r:   r   r9   r!   r!   r!   r"   r^   a   s   
 r^   c                   @   s>   e Zd ZU eeef ed< ee ed< ee	 ed< eed< dS )SkillsCourseResponseskill_analysiscourses_offlineonline_recommended_coursesstrategic_presentationN)
r6   r7   r8   r   r;   r	   r:   r   rZ   r^   r!   r!   r!   r"   rf   o   s
   
 rf   c                   @   s.   e Zd ZU eed< eed< dZee ed< dS )OnlineCourseRequestdomainfield_prompt
   max_coursesN)r6   r7   r8   r;   r:   ro   r   r9   r!   r!   r!   r"   rk   u   rE   rk   c                   @   sB   e Zd ZU eed< eed< eed< eed< ee ed< eed< dS )OnlineCourseResponsesuccessrl   rm   total_recommendationscourse_recommendationssearch_summaryN)	r6   r7   r8   boolr:   r;   r9   r   r^   r!   r!   r!   r"   rp   z   s   
 rp   )analyze_departmentc                   @   s:   e Zd ZU eed< eed< eed< eeeef  ed< dS )DepartmentDatar3   r4   total_usersrV   N)	r6   r7   r8   r9   r:   r;   r   r   r	   r!   r!   r!   r"   rw      s
   
 rw   c                   @   sB   e Zd ZU eed< eed< ee ed< ee ed< ee ed< dS )FullTnaRequestrS   rT   departmentsrX   rY   N)	r6   r7   r8   r;   r:   r   rw   rK   rL   r!   r!   r!   r"   ry      s   
 ry   c                   @   s*   e Zd ZU eed< ee ed< eed< dS )FullTnaResponserA   rB   rD   N)r6   r7   r8   r;   r:   r   r9   r!   r!   r!   r"   r{      s   
 r{   rS   rT   rU   rV   rW   rX   rY   returnc           5         s  zt ddd  d g }|r6|D ]}d|v r5t|d D ]\}	}
|d|	 |
d|d d	 q"qg }|D ]}||j|jd
 q:i }rvg |d< D ]$}|j}t|dkrd|dd d }|d |j|j	|j
||jd qQ|||||d}t   dtjt|I dH t ddd ddd   fdd} fdd}t| | I dH \}}dd dg D }dd dg D }dd dg D }tdg }dd}g }|d | d! |d" |d# |d$| d% |d# |d& d' |d# |d(| d) |d# |r~| d*kr~|d+ |d# |d,| d- |d# t|d.D ]\}	}|d/|	 d0|d1  d2|d3   qW|d# |d4 |d# |r|rdnd.}|d5| d6 |d# |d7 |d# t|d.D ]\}	}|d/|	 d0|d1  d2|d3   q|d# |d8 |d# |r%|r|rd9n	|s|rdnd.}|d:| d; |d# |d< |d# t|d.D ]\}	}|d/|	 d0|d1  d2|d3   q|d# |d= |d# t|d>d? d@dA}|ddB }t|dBk rvrvdBt| }dCdD |D }d} D ]&}t|j}!|!|vrt||!|j	dEdF| dG | d.7 } | |krt nqO|rtdHd |||fD d. }"|dI|" dJ |d# |dK |d# t|d.D ]+\}	}|dLd}#|d/|	 d0|dM  dN|# dO |dP|dQ   |d# qt|dBkr|dRt|dB  dS |d# |dT |dU |dV |dW |dX |d# t|dYd? d@dA}$|$ddB }%|%rtdZd ||||fD d. }"|d[|" d\ |d# |d] |d# t|%d.D ]]\}	}|dLd}#|d^d_}&|d`da}'|dbdc}(|ddde})|d/|	 d0|dMdf dN|# dO |dg|& dh|) di|(  |dj|'  |dk|dldm  |d# qEt|dBkr|dRt|dB  dn |d# |do |dp |dq |dr |ds |dt |d# |du |d# |r |dvdwdxd |dd9 D   |r1|rdnd.}*|dy|* dz|rdnd. d{|rd|nd9 d}dwd~d |dd9 D   |r|r<|r<d9n	|sB|rDdnd.}*|rN|rNd|n	|sT|rVd9nd.}+|r`|r`dn	|sf|rhdBnd9},|dy|* dz|+ d{|, ddwdd |dd9 D   |d# |d |d| d! |dt| d |dt| d |dt| d |r|dt| d |%r|dt|% d |d# |d t|t|% }-|-dkr|d| dt| dt|% d|  d	 n|d|  d |d# |d |d# |d |d d|}.dg |dd#dd#d}/g }0|D ]}1|0|1d |1dM |1dQ t|1dL d qGg }2|%D ]E}3|2|3dMd#|3d^d#|3dd#|3ddd#|3dd#|3dd#|3dd#|3d`d#|3dbd#|3dg |3dLd|3dld#d qct|/|0|2|.dW S  ty }4 zt d|4  tg ddg g dt|4 dW  Y d}4~4S d}4~4ww )zT
    Reusable core logic for skills and course analysis with async optimization
    zEnhanced analysis for query: Nd   ...rQ   i  Intermediate   rG   rH   rI   rJ   rG   rH   coursesi   i  )courseIdr4   rO   rP   rQ   )rS   rT   rU   rV   rX   all_courses_dataz%Enhanced analysis completed - Field: identified_fieldUnknownz
, Skills: total_skills_recommendedr   c               
      sF  sg S zg } D ]=}d}|j r(t|j tr#ddd |j D }nt|j }|j}t|dkr9|d d d }| |j|j	|j
||d q
d	d
| d} d tjt|I d H }|dry|dg }tdt| d |W S td|d  g W S  ty } ztd|  g W  Y d }~S d }~ww )N , c                 S   s   g | ]}t |qS r!   )r;   .0sr!   r!   r"   
<listcomp>       zJperform_enhanced_analysis.<locals>.get_offline_matches.<locals>.<listcomp>i  i  r~   )rM   rN   course_short_descriptioncourse_descriptionskillr   
Technology)rl   rm   r   rq   course_matcheszCourse filtering completed - z matches foundzCourse filtering failed: r   zCourse filtering error: )rQ   
isinstancelistjoinr;   rP   lenappendrM   rN   rO   getrun_in_executorcourse_filter_systemprocess_json_requestjsondumpsr   r+   warning	Exceptionr   )courses_for_filteringcourse
skills_strdesccourse_filter_requestcourse_filter_resultmatcheselooprY   resultrS   r!   r"   get_offline_matches   sR   





z6perform_enhanced_analysis.<locals>.get_offline_matchesc               
      s   zb dd} tj| dd}|r tdt| d |W S | dd} d tjt	
|I d H }| d	rV| d
g }tj| |dd tdt| d |W S td| d  g W S  ty } ztd|  g W  Y d }~S d }~ww )Nr   r   online)rl   rm   course_typezUsing cached online courses: z courses   )rl   rm   ro   rq   rs   )rl   rm   r   r   zGenerated and cached z online coursesz!Online course generation failed: r   z$Online course recommendation error: )r   r   get_cached_coursesr   r+   r   r   online_course_systemr   r   r   cache_coursesr   r   r   )r   cached_online_coursesonline_course_requestonline_course_resultrecsr   )r   r   rS   r!   r"   get_online_matches  sJ   

z5perform_enhanced_analysis.<locals>.get_online_matchesc                 S      g | ]
}|d  dkr|qS )sourcedepartment_existingr!   r   r!   r!   r"   r   Q      z-perform_enhanced_analysis.<locals>.<listcomp>recommended_skillsc                 S   r   )r   organizational_relevantr!   r   r!   r!   r"   r   R  r   c                 S   r   )r   externalr!   r   r!   r!   r"   r   S  r   zyour chosen fieldu   📈 **Your Journey to Master z**z2==================================================r   zHello u   ! 👋zWe heard you say: *"ze"* - and that's exactly the kind of growth mindset that separates good professionals from great ones!z*After analyzing your current situation in zY, we've crafted a personalized skill development roadmap just for you. Here's your story:nullu:   🏢 **Chapter 1: Building on Your Department Foundation**zGood news! Your z department already has some valuable skills that align perfectly with your goals. Think of these as your existing superpowers that just need some sharpening:   z   z. **rH   z** - justificationu   💡 *Why start here?* You already have colleagues using these skills, which means mentorship opportunities and immediate practical application!u   🌟 **Chapter z): Expanding Your Organizational Arsenal**zYour organization is a goldmine of additional skills that can accelerate your growth. These are the hidden gems available right within your company:u   🎯 *The advantage?* These skills are already valued by your organization, so developing them directly boosts your internal career prospects!   u   🚀 **Chapter z*: The Competitive Edge - External Skills**zzTo truly excel and stand out in the market, here are the cutting-edge skills that will give you the competitive advantage:u   ⚡ *The impact?* These skills are already valued by your organization, so developing them directly boosts your internal career prospects!c                 S      |  ddS Nmatch_percentager   r   xr!   r!   r"   <lambda>      z+perform_enhanced_analysis.<locals>.<lambda>T)keyreverse   c                 S   s   h | ]	}t |d qS )rM   )r;   r   )r   cr!   r!   r"   	<setcomp>      z,perform_enhanced_analysis.<locals>.<setcomp>g      N@z/Recommended to build foundational knowledge in )rM   rN   r   r[   c                 S      g | ]}|r|qS r!   r!   r   r   r!   r!   r"   r     r   u   📚 **Chapter z/: Your Learning Pathway - Recommended Courses**zxGreat news! We found some fantastic courses in your organization that perfectly align with your skill development goals:r   rN   z** (z% match)z      Why it's perfect: r[   z
   ...and z more courses available!uV   🎓 *The advantage?* These courses are available right in your organization, meaning:u!      • No external training costsu&      • Immediate access and enrollmentu'      • Tailored to your company's needsu*      • Support from internal training teamc                 S   r   r   r   r   r!   r!   r"   r     r   c                 S   r   r!   r!   r   r!   r!   r"   r     r   u   🌐 **Chapter z(: Global Learning - Top Online Courses**zhTo complement your organizational learning, here are some outstanding online courses from top platforms:r_   zOnline Platformrb   zCheck Platformrc   zN/Ara   z
Self-pacedzCourse Titlez      Platform: z | Duration: z | Rating: z      Price: z      Why it's excellent: recommendation_reasonzHigh-quality contentz more online courses available!u   🎓 *The online advantage:*u)      • Learn from world-class instructorsu$      • Flexible, self-paced learningu)      • Industry-recognized certificationsu+      • Latest industry trends and practicesu#      • Global community of learnersu   🎯 **Your Success Formula**z:**Phase 1 (Months 1-2):** Master your department skills - r   c                 S      g | ]}|d  qS rH   r!   r   r!   r!   r"   r     r   z**Phase z	 (Months -   z&):** Leverage organizational skills - c                 S   r   r   r!   r   r!   r!   r"   r     r      z):** Gain competitive edge - c                 S   r   r   r!   r   r!   r!   r"   r     r   u+   📊 **Your Complete Development Package:**u   • Total Skills Identified: **u   • Department Advantage: **z	 skills**u    • Organizational Resources: **u%   • External Growth Opportunities: **u"   • Internal Courses Available: **z
 courses**u   • Top Online Courses: **u   🌟 **The Bottom Line:**zZYou're not just getting a skill list - you're getting a complete learning ecosystem! With z targeted skills, z internal courses, and zD world-class online courses, you have everything needed to become a z powerhouse!zlYou're not just improving random skills - you're building a strategic capability stack that will make you a u@   Ready to begin this exciting journey? Let's make it happen! 🚀z---z>*Generated by TNA v2.0 - Your Personalized Learning Companion*
case_applied)r   total_skillsr   r   rM   )rM   r4   r[   r\   r`   rI   rP   r)   rd   )r   r_   r`   ra   rI   rP   r)   rb   rc   rd   re   r[   )rg   rh   ri   rj   z!Enhanced skills analysis failed: r   r   z:Unable to complete enhanced analysis at this time. Error: )r   r+   	enumerater   rG   rH   rP   r   rM   rN   rO   rQ   asyncioget_running_loopr   skill_analysis_systemr   r   r   r   gatherlowersortedr;   r   r]   rf   r   r   )5rS   rT   rU   rV   rW   rX   rY   filtered_skills_formattedskill_groupirH   all_skills_formattedr   all_courses_formattedr   r   analysis_requestr   r   r   online_coursesdept_skills
org_skills
ext_skillsr   r   strategic_partschapter_numsorted_coursestop_offline_coursesneededexisting_idsadditional_countcourse_id_strfinal_chapter_numr\   sorted_online_coursestop_online_coursesr_   rb   rc   ra   	phase_nummonths_start
months_endtotal_courses_countrj   skill_analysis_dictoffline_courses_formattedcourse_matchonline_courses_formattedonline_courser   r!   r   r"   perform_enhanced_analysis   s  




&73







(





(


$


(








"









&









(N$$$:




2





















r  r%   )response_modelc              	      s,   t | j| j| j| j| j| j| jdI dH S )u   
    Enhanced skill analysis using tna_skill_second.py
    
    Flow:
    1. Field identification with specificity analysis
    2. Smart skill filtering by domain relevance  
    3. Priority-based recommendations (dept → org → external)
    4. Professional strategic presentation
    rS   rT   rU   rV   rW   rX   rY   N)r  rS   rT   rU   rV   rW   rX   rY   )r   r!   r!   r"   analyze_skills_courses_endpointA  s   r  z/analyze-full-tnac                    sx  zt d| jdd  d dd | jD }t }|dt| j|I dH }|dd}|d	}|d
d}t d| d| d d}|rX| jD ]}|j	|krW|} nqL|r\|nd}	g }
d}|rm|j
}
|j}|j}	t| j| j|	|
|| j| jdI dH }t|j|j|j|j|||dW S  ty } z!t d|  tg ddg g dt| ddddW  Y d}~S d}~ww )z[
    Combined endpoint performing Department Identification + Skills & Course Analysis
    zFull TNA analysis for query: Nr}   r~   c                 S      g | ]	}|j |jd qS r3   r4   r	  )r   dr!   r!   r"   r   `  r   z-analyze_full_tna_endpoint.<locals>.<listcomp>rA   r   rB   rD   rC   zIdentified Department: z (ID: )Generalr   r  )rg   rh   ri   rj   rA   rB   rD   zFull TNA analysis failed: r   z6Unable to complete full analysis at this time. Error: high)r   r+   rS   rz   r   r   r   rv   r   r3   rV   rx   r4   r  rT   rX   rY   r{   rg   rh   ri   rj   r   r   r;   )r   simple_departmentsr   dept_resultidentified_dept_nameidentified_dept_idrD   target_department_datar
  department_name_for_analysisrV   rx   skills_responser   r!   r!   r"   analyze_full_tna_endpointV  sx   




r  z/analyze-departmentc              
      s   zBt d| jdd  d dd | jD }t }|dt| j|I dH }t d|dd	 d
|dd d t	di |W S  t
yd } zt d|  t	ddddW  Y d}~S d}~ww )z<
    Analyze user goal and identify relevant department
    zAnalyzing: Nr}   r~   c                 S   r  r  r	  )r   deptr!   r!   r"   r     s    z/analyze_department_endpoint.<locals>.<listcomp>zResult: rA   Nonez (confidence: rD   rC   r  zAnalysis failed: r   )rA   rB   rD   r!   )r   r+   r>   r?   r   r   r   rv   r   r@   r   r   )r   rz   r   r   r   r!   r!   r"   analyze_department_endpoint  s4   


r  z&Want to improve my teams sales ability   
Operationsr	     HR   Finance   zProduct Management   sales   	Developer)r>   r?   z$Want to improve my technical abilityAscentx   Pythonr   r   r   r   i_  zcommunication skillr   i`  ztechnical skillsy   
JavaScriptz   {   Angular32zGeneral Knowledger   )rM   rN   rO   rP   rQ   43z)Python Programming for Complete BeginnerszLearn Python from scratchpythonprogramming63zAngular BasiczAngular framework coursezComplete Angular course)angular
typescript
javascriptr  )Cfastapir   r   r   pydanticr   typingr   r   r   r	   r
   r   loggingr   dataclassesr   fastapi.responsesr   fastapi.exceptionsr   tna_skill_secondr   testerr   r  r   r   basicConfigINFO	getLoggerr6   r   appr   r   r   exception_handlerr#   
middlewarer0   r2   r=   r@   rF   rK   rL   rR   rZ   r^   rf   rk   rp   
departmentrv   rw   ry   r{   r;   r9   r  postr  r  r  example_department_requestexample_skills_courses_requestr!   r!   r!   r"   <module>   s    


	
   -O(	
