
    P1i               	           d Z ddlZddlZddlmZmZmZmZ ddlm	Z
  ej                  e      Zh dh dh ddZh d	 e       h d
dZdedee   fdZdededeeee   f   fdZdedeeef   fdZddZy)zK
Utility functions for validating prompt templates and their placeholders.
    N)DictListSetTuple)gettext>+   namegenderday_stem	hour_stem	year_stem
birth_date
birth_time
day_branch
month_stemcurrent_agehour_branchlife_numberlife_palaceyear_branchcurrent_yearday_sha_godsmonth_branchfire_strengthhour_sha_godswood_strengthyear_sha_godsearth_strengthmetal_strengthmonth_sha_godswater_strengthday_stem_elementcurrent_year_stemhour_stem_elementyear_stem_elementmonth_stem_elementcurrent_luck_pillarcurrent_year_branchday_hidden_elementshour_hidden_elementsyear_hidden_elementsmonth_hidden_elementshour_stem_relationshipyear_stem_relationshipmonth_stem_relationshipday_hidden_relationshipshour_hidden_relationshipsyear_hidden_relationshipsmonth_hidden_relationships>   r   r	   formular   r   
life_geneshead_numberhidden_giftlife_resultyear_numberzodiac_namecareer_genesr   life_processyear_elementcareer_resultcareer_processelement_countsmissing_numberspersonal_numberpresent_numbersweakest_elementpersonal_elementpersonality_titlestrongest_elementrelationship_genesrelationship_resultrelationship_processzodiac_boost_numbersyear_element_relationship>   qdatequestionhexagram_calculation)bazinumberliuyao>   	next_year
next2_yearnext_year_stemnext2_year_stemnext_year_branchnext2_year_branchday_stem_relationship>   changing_lineschanging_lines_countchanged_hexagram_infochanged_hexagram_numbertemplate_contentreturnc                 x    d}t        j                  ||       }|D ch c]  }|j                          c}S c c}w )z
    Extract all placeholders from a template string.
    
    Args:
        template_content: The template content to analyze
        
    Returns:
        Set of placeholder names found in the template
    z{{([^}]+)}})refindallstrip)r]   patternmatchesmatchs       >/home/cursorai/projects/iching/ai/utils/template_validation.pyextract_placeholdersrg   \   s8     Gjj"23G'./weEKKMw///s   7divination_typec                    g }t         j                  |t                     }t        j                  |t                     }|s*|j	                  t        t        d|                    d|fS t        |       }|dk(  r>d| v r'd| vr#|j	                  t        t        d                   d| v r'd| vr#|j	                  t        t        d                   t        j                  d|       }|r5|j	                  t        t        d	d
j                  |                          ||z
  }|r0t        j                  dd
j                  t        |                    ||z  }||z
  }	|	r0t        j                  dd
j                  t        |	                    t        |      xs d| v xr d| vxs
 d| v xr d| v}
|
 |fS ||z
  }|r>|j	                  t        t        dd
j                  t        |                                ||z  }||z
  }	|	r>|j	                  t        t        dd
j                  t        |	                                d| v r'd| vr#|j	                  t        t        d                   d| v r'd| vr#|j	                  t        t        d                   t        j                  d|       }|r5|j	                  t        t        d	d
j                  |                          t        |      dk(  |fS )a  
    Validate a template against required placeholders.
    
    Args:
        template_content: The template content to validate
        divination_type: The type of divination (bazi, number, liuyao)
        
    Returns:
        Tuple of (is_valid, list_of_warnings)
    z%No required placeholders defined for FrO   z{{z}}zUnclosed placeholder foundzUnopened placeholder foundz{{[^}]*[^a-zA-Z0-9_][^}]*}}zMalformed placeholders found: , z7BaZi template could use these additional placeholders: z,BaZi template contains custom placeholders: zMissing required placeholders: zUnused placeholders found: r   )REQUIRED_PLACEHOLDERSgetsetOPTIONAL_PLACEHOLDERSappendstr_rg   r`   ra   joinloggerinfosortedboollen)r]   rh   warningsrequiredoptionalfound_placeholders	malformedmissingallowedunusedhas_syntax_errorss              rf   validate_templater   k   s
    H %((#%@H$((#%@HA EoEVWXYZh ..>? & ##4D(DOOC"> ?@A##4D(DOOC"> ?@A JJ=?OP	OOC$B499YCWBX"Y Z[\ //KKQRVR[R[\bcj\kRlQmno X%#g-KKFtyyQWX^Q_G`Fabc !O  j8H0H0iTYiMi  jos  xH  pH  pi  MQ  Yi  Mi$$h..
 //OOC$CDIIfU\oD^C_"` abc X%#g-OOC$?		&QW.@Y?Z"[ \]^ ##4D(DOOC"> ?@A##4D(DOOC"> ?@A JJ=?OP	OOC$B499YCWBX"Y Z[\8}!8++    c                 `    t        |       }t        |      t        |       t        |       dz  dS )z
    Get statistics about a template.
    
    Args:
        template_content: The template content to analyze
        
    Returns:
        Dictionary with template statistics
       )total_placeholderstotal_charactersestimated_tokens)rg   rw   )r]   placeholderss     rf   get_template_statisticsr      s:     ((89L ",/ 01 01Q6 r   c                 t    |g }t         j                  d| d|  d| d|rdj                  |      nd        y)a  
    Log a change to a template.
    
    Args:
        template_id: ID of the template being changed
        user_id: ID of the user making the change
        action_type: Type of action (created, updated, archived, activated)
        warnings: List of warnings from validation
    Nz	Template z - ID: z, User: z, Warnings: rj   None)rs   rt   rr   )template_iduser_idaction_typerx   s       rf   log_template_changer      sM     
KK
K=}HWI F,4TYYx(&A	Cr   )N)__doc__r`   loggingtypingr   r   r   r   django.utils.translationr   rq   	getLogger__name__rs   rk   rm   rn   rp   rg   rv   r   intr   r    r   rf   <module>r      s    
  ) ) 1			8	$@0s= B e  03 03s8 0L, L,c L,eDRVWZR[OF\ L,\c d38n $r   