o
    <h                     @   sd   d dl mZ d dlmZ d dlZd dlZddlmZ d dlm	Z	 d dl
mZ e ZG dd	 d	ZdS )
    )HTTPException)defaultdictN   )HashGenerator)UserJourney)SummaryGeneratorc                   @   s`   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zde	de
fddZde	de
fddZdS )SummaryCreationServicec                 C   s>   t  | _|| _t|| _d| j d| _d| j d| _d S )Nzoutput/z.mdzsummary/)r   crew_instance
input_datar   generate_input_hash
input_hashoutput_filesummary_file)selfr
    r   /home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/services/summary_creation_service.py__init__   s
   zSummaryCreationService.__init__c                 C   s   t d | | j\}}|  r<| | j\}}|r%| |}dd|dS |   | | j\}}| |}dd|dS | | j\}}|rQ| |}dd|dS |   | | j\}}| |}dd|dS )NzInside def run function...successzFetch existing result.)statusmessageoutputzExecution completed.)print_fetch_existing_resultr   isStructured_restructure_output_structuredcreate_summary_structured_restructure_output_ondemandcreate_summary_ondemand)r   existsresultr   r   r   r   run   s&   



zSummaryCreationService.runc                 C   s&   t d| jj  | jjdkrdS dS )NzInside journey type : zStructured LearningTF)r   r
   journey)r   r   r   r   r   0   s   z#SummaryCreationService.isStructuredc                 C   sj   | j  }t| jddd}| }W d    n1 sw   Y  ||d< | jj| jd}|j|d d S )Nrutf-8encodingcontentr   inputs)	r
   dictopenr   readr	   structured_crewr   kickoffr   r)   fr&   crewr   r   r   r   6   s   

z0SummaryCreationService.create_summary_structuredc                 C   sr   t d | j }t| jddd}| }W d    n1 s w   Y  ||d< | jj| jd}|j	|d d S )Nz#Inside on demand journey function..r"   r#   r$   r&   r'   r(   )
r   r
   r*   r+   r   r,   r	   ondemand_crewr   r.   r/   r   r   r   r   @   s   

z.SummaryCreationService.create_summary_ondemandc                 C   s   t d tj|r>t d t|d}| }t d|  t dt|  W d    d|fS 1 s5w   Y  d|fS dS )NzInside fetch result functionzInside if conditionr"   zThe fetched result is : The type of result is : T)F )r   ospathr   r+   r,   type)r   	file_pathfiler   r   r   r   r   J   s   
z-SummaryCreationService._fetch_existing_resultr   returnc              
   C   s   z(t d t dt|  t dt|  td|}t d dd |D W S  ty@ } zt | i W  Y d }~S d }~ww )Nz"Inside restructure output functionr3   zResult content: z##\s*([\w\s]+?)\s*-\s*(\d+)%zmatch foundc                 S   s   i | ]\}}|  t|qS r   )stripint).0skillpercentr   r   r   
<dictcomp>_   s    zISummaryCreationService._restructure_output_structured.<locals>.<dictcomp>)r   r7   reprrefindall	Exception)r   r   matcheser   r   r   r   V   s   z5SummaryCreationService._restructure_output_structuredmd_textc                 C   sz  |   }tt}d }i }|D ]}|  }|dr$|dd    }q|dkr_d|v r>td|}|r=d|d  i}qd|v r^td	|}|r^|r^|d  |d
< || | i }q|dkrt	d|}	|	r|	
 \}
}|
  |  d}|| | qd|v r|| rtd|}|r|d  || d d< q|dv rt	d|}|r|| |d   qt|S )Nz##    z
Skill Gapsz
**Skill:**z\*\*Skill:\*\*\s*(.+)r>      z	- Reason:z- Reason:\s*(.+)reasonzRecommended Modulesz&\d+\.\s+\*\*(.+?)\*\* \(Stage: (.+?)\))module_titlestagez- Why Relevant:z- Why Relevant:\s*(.+)why_relevant>   Applied SkillsCore foundationBusiness ContextPersonalization Reasonsz-\s*(.+))r;   
splitlinesr   list
startswithrB   searchgroupappendmatchgroupsr*   )r   rG   linesdatacurrent_section
temp_skillliner>   rJ   module_matchrK   rL   modulerN   bullet_matchr   r   r   r   c   sN   
z3SummaryCreationService._restructure_output_ondemandN)__name__
__module____qualname__r   r    r   r   r   r   strr*   r   r   r   r   r   r   r      s    

r   )fastapir   collectionsr   r5   rB   utils.hashingr   user_journey_service.crewr   1user_journey_service.processors.summary_generatorr   summary_generatorr   r   r   r   r   <module>   s    