
    P1i                     X    d dl Z d dlmZ d dlZd dlmZ  ej                  e      ZddZ	y)    N)Path)settingsc           
         ddl m} |s|  d}	 |j                  j                  | |d      j	                  d      j                         }|r2t        j                  d|j                   d| d	       |j                  S |d
k7  rk|j                  j                  | d
d      j	                  d      j                         }|r.t        j                  d| ||j                         |j                  S t        j                  d| |       t        t        j                        }|r|g}n#|r|j!                  dd      nd}	|  d|	 d|  dg}|D ]o  }
|dz  dz  |
z  }t"        j$                  j'                  |      s.	 t)        |dd      5 }t        j                  d|
        |j+                         cddd       c S  t        j-                  d| |dj/                  |             t1        d|  d| d       # t        $ r,}t        j                  dt        |              Y d}~6d}~ww xY w# 1 sw Y   nxY w# t        $ r*}t        j-                  d|
 dt        |               d}~ww xY w)!a  
    Get the active template for a given divination type from the database.
    Falls back to the file system only for backward compatibility.
    
    This function first checks if there's an active template in the database for the
    specified divination type. If found, it returns the template content.
    If not, it falls back to reading the template file from the file system.
    
    Args:
        divination_type (str): The divination type (bazi, number, liuyao)
        language (str): Language code for the template. Defaults to 'zh-hans'.
        filename (str, optional): Specific filename to look for (for file-based fallback only).
                                 If None, uses default naming based on divination type.
    
    Returns:
        str: The template content as a string
    
    Raises:
        FileNotFoundError: If the template cannot be found in the database or file system
    r   )PromptTemplatez_analysis.txtactive)divination_typelanguagestatusz-updated_atz Using active database template: z []zh-hanszKNo active template found for %s (%s); falling back to zh-hans template '%s'z'Error fetching template from database: NzUNo active template found in database for %s (%s), falling back to file-based template-_default
_analysis_z.txtaiprompt_templatesrzutf-8)encodingzUsing file-based template: zError loading template file z: zITemplate file not found for divination_type=%s, language=%s (checked: %s)z, zNo template found for z (language=z,). Please create one in the admin interface.)	ai.modelsr   objectsfilterorder_byfirstloggerdebugnamecontentwarning	Exceptionstrr   r   BASE_DIRreplaceospathexistsopenreaderrorjoinFileNotFoundError)r   r	   filenamer   templatefallback_templateebase_dirfilename_candidateslanguage_suffix	candidatetemplate_pathfiles                3/home/cursorai/projects/iching/ai/utils/__init__.pyget_active_templater6      s   , )%&m4K!))00+ 1 
 (=
!%%'	 	 LL;HMM?"XJVWXY###y  . 6 6 = = /" !> ! h}%eeg	 
 !a#%**	 )000 NN_
 H%%&H'j8@(**34iz/):$?}-

 )	 4*<<yHww~~m,	mS7;t:9+FGyy{ <; ) LLS		%&	 
  1XJFrs O  K@QIJJK4 <;;  	LL7	{"SVHMN	sO   A.G4 >A/G4 8H9(H,.	H94	H)=!H$$H),H5	1H99	I,%I''I,)r   N)
r#   pathlibr   loggingdjango.confr   	getLogger__name__r   r6        r5   <module>r>      s)    	    			8	$`r=   