
    K{j%G                       S r SSKJr  SSKrSSKrSSKrSSKrSSKJrJ	r	  SSK
J
r
Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJrJrJr  SrSrSr\R:                  " S5      r\R:                  " S\R>                  5      r \R:                  " S\R>                  5      r!\R:                  " S\R>                  5      r"\R:                  " S\R>                  5      r#\R:                  " S\R>                  5      r$Sr%Sr&\R:                  " S\R>                  5      r'\R:                  " S5      r(S2S jr)S3S jr*S3S jr+S4S jr,S5S jr-S5S jr. S6     S7S jjr/S8S jr0S6S9S  jjr1S:S! jr2\ " S" S#5      5       r3S;S$ jr4        S<S% jr5S=S& jr6          S>S' jr7S:S( jr8          S?S) jr9S@S* jr:SAS+ jr;SBS, jr<SSS-.                 SCS. jjr=S@S/ jr>          SDS0 jr?SES1 jr@g)Fz4Informes masivos por curso desde el chat de agentes.    )annotationsN)	dataclassfield)datetimetimezone)Path)Any)Session)settings)CourseModelSchoolModelStudentAcademicInfoModelStudentModelStudentPersonalInfoModel-         z\b(20\d{2})\bz>\b(liceo|colegio|escuela|establecimiento|unidad\s+educativa)\bz\b(curso|cursos)\bz+\b(informe|informes|documento|documentos)\bzC\b(\d{1,2}[.\s]?\d{3}[.\s]?\d{3}[-\s]?[\dkK]|\d{7,8}[-\s]?[\dkK])\bz\b(informe|informde|documento)\s+(a\s+la\s+familia\s+|psicopedag\w*\s+)?(de|del|para)\s+(?!liceo|colegio|escuela|establecimiento|curso|todos\b)z)Para continuar con los informes del cursoz,Confirma para generar los informes del cursouz   ^\s*(s[ií]|ok|okay|dale|adelante|confirmo|confirma|continuar|contin[uú]a|genera|generar|procede|de acuerdo)\s*[.!?]?\s*$u   [°º.]c                z   [         R                  " SU =(       d    S5      nSR                  S U 5       5      nUR                  5       R	                  SS5      R	                  SS5      n[
        R                  SU5      n[        R                  " SSU5      n[        R                  " SSU5      R                  5       $ )	NNFKD c              3  `   #    U  H$  n[         R                  " U5      (       a  M   Uv   M&     g 7fN)unicodedata	combining).0cs     WC:\Users\jesus\Desktop\proyecto_pie360\backend\app\backend\utils\agents_bulk_reports.py	<genexpr>_fold.<locals>.<genexpr>=   s     AS(=(=a(@!!Ss   .	.   °    ºz[^a-z0-9\s]\s+)	r   	normalizejoinlowerreplace_ORDINAL_REsubrestrip)valueraws     r   _foldr.   ;   s    



4C
''ASA
AC
))+

dC
(
0
0s
;C
//#s
#C
&&c
*C66&#s#))++    c                    U =(       d    S/nU=(       d    /  H,  nUR                  UR                  S5      =(       d    S5        M.     SR                  U5      $ )Nr   content
)appendgetr%   messagehistorypartsitems       r   _conversation_textr:   D   sF    ]OE2TXXi(.B/ 99Ur/   c                   / nU=(       d    /  HL  nUR                  S5      =(       d    SS:X  d  M#  UR                  UR                  S5      =(       d    S5        MN     UR                  U =(       d    S5        SR                  U5      $ )Nroler   userr1   r2   )r4   r3   r%   r5   s       r   
_user_blobr>   K   sh    E2HHV"v-LL),23  
LLB99Ur/   c                    [        U =(       d    / 5       H=  nUR                  S5      =(       d    SS:X  d  M#  UR                  S5      =(       d    Ss  $    g)Nr<   r   	assistantr1   )reversedr4   )r7   r9   s     r   _last_assistantrB   T   sD    B'HHV"{288I&,", ( r/   c                    U =(       d    SR                  5       nU(       d  g[        R                  U5      (       a  g[        [        R                  U5      5      $ )z:Pedido de un solo estudiante (nombre o RUT): no es masivo.r   FT)r+   _RUT_REsearchbool_SINGLE_STUDENT_RE)r6   texts     r   _looks_like_single_studentrI   [   sC    Mr  "D~~d"))$/00r/   c                l    [        [        R                  U =(       d    SR                  5       5      5      $ )Nr   )rF   _CONFIRM_REmatchr+   )r6   s    r   user_confirmed_bulkrM   e   s%    !!7=b"7"7"9:;;r/   c                   U =(       d    SR                  5       nUR                  5       nU(       d  g[        U5      n[        R                  5       UR                  5       ;   nU(       a  g[	        U5      (       a  g[        [        R                  U5      5      n[        [        R                  U5      5      n[        [        R                  U5      5      nU(       a  U(       d  U(       a  gU(       a  SU;   d  SU;   a  g[        X!5      R                  5       n	[        R                  5       U	;   a;  [        R                  U5      (       d   [        U5      (       d  [        U5      (       a  gg)zETrue si el usuario pide informes de un liceo/curso (no de un alumno).r   FTfamiliapsicoped)r+   r&   rB   _ASK_MARKERrI   rF   _INFORME_RErE   	_PLACE_RE_COURSE_WORD_REr:   _YEAR_RE_looks_like_course_replyrM   )
r6   r7   rH   lowr@   in_bulk_flowhas_informe	has_place	has_cursoblobs
             r   looks_like_bulk_requestr]   i   s    Mr  "D
**,C(I$$&)//*;;L "$''{))$/0KY%%d+,I_++D12I	Yi3&**;d,224Dd"#D))t$$r/   c                    [        U 5      nU(       d  g[        R                  " SU5      (       a  g[        R                  " SU5      (       a  [        U5      S:  a  gg)NFuD   \b\d{1,2}\s*(medio|basico|básico|basica|básica|kinder|prekinder)\bTz*\b(prekinder|kinder|medio|basico|basica)\b(   )r.   r*   rE   len)rH   foldeds     r   rV   rV      sL    4[F	yyXZ`aa	yy>GGCPVKZ\L\r/   c                    [        X5      n[        R                  U5       Vs/ s H  n[        U5      PM     nnU(       d  g US   $ s  snf )N)r>   rU   findallint)r6   r7   r\   yyearss        r   extract_yearrh      sF    g'D%--d343SV3E49 5s   Ac                z   U =(       d    Sn[         R                  " SU[         R                  [         R                  -  5      nU(       ar  UR	                  S5      n[         R
                  " SUS[         R                  S9S   nUR                  S5      n[        [        U5      5      S:  a  UR                  5       $ g )	Nr   zP((?:liceo|colegio|escuela|establecimiento)\s+(?:de\s+|del\s+|la\s+|el\s+)?(.+))$   u6   \b(curso|año|ano|familia|psicoped|informe|document)\b)maxsplitflagsr   z .,;:-   )	r*   rE   
IGNORECASEDOTALLgroupsplitr+   r`   r.   )rH   
folded_srcmtailqs        r   _extract_school_queryrv      s    J
		[
		!	A
 	wwqzxxE--	

  JJx uQx=A779r/   c                      \ rS rSr% S\S'   SrS\S'   SrS\S'   SrS\S	'   SrS\S
'   Sr	S\S'   Sr
S\S'   \" \S9rS\S'   Srg)BulkPlan   rF   activeN
int | None	school_id
str | Noneschool_nameyear	course_idcourse_nameask)default_factorylist[dict[str, Any]]students )__name__
__module____qualname____firstlineno____annotations__r|   r~   r   r   r   r   r   listr   __static_attributes__r   r/   r   rx   rx      sS    L Iz "K"D* Iz "K"C%*4%@H"@r/   rx   c                Z   U R                  [        5      R                  [        R                  [	        U5      :H  5      n UR                  [        R
                  S:H  5      nUR                  [        R                  R                  5       5      R                  5       $ ! [         a     NGf = fNr   )queryr   filtercustomer_idre   deleted_status_id	Exceptionorder_byr~   ascall)dbr   ru   s      r   _schools_for_customerr      s    
$$[%<%<K@P%PQAHH[22a78 ::k--113488::  s    "B 
B*)B*c                  ^ [        X5      nU(       d  S / 4$ [        U=(       d    S5      nU(       d  S U4$ / nU H  n[        UR                  =(       d    S5      mT(       d  M*  SnUT:X  d  UT;   d  TU;   a  UT:X  a  SOSnOcUR                  5        Vs/ s H  n[	        U5      S:  d  M  UPM     n	nU	(       a+  [        U4S jU	 5       5      (       a  SS[	        U	5      -  -   nU(       d  M  UR                  Xv45        M     U(       d  S U4$ UR                  S	 S
9  US   S   n
U VVs/ s H  u  pX:X  d  M  UPM     nnn[	        U5      S:X  a  US   U4$ S U4$ s  snf s  snnf )Nr   r   d   P   rm   c              3  ,   >#    U  H	  oT;   v   M     g 7fr   r   r   tnames     r   r   match_school.<locals>.<genexpr>   s     8A4i   2      c                    U S   * $ r   r   xs    r   <lambda>match_school.<locals>.<lambda>   
    qter/   keyrj   )r   r.   r~   rq   r`   r   r3   sort)r   r   r   schoolsru   scoredschoolscorer   tokensbestscstopr   s                 @r   match_schoolr      sR    $B4GRxekrAW},.FV''-2.9T	TQY9C"E!":Ac!fkaF:#8888QV_,5MM5/*  W}
KKOK$!9Q<D 
/&BJ1&C
/
3x1}1vw9 ; 0s   E -E .E%=E%c                   U R                  [        5      R                  [        R                  [	        U5      :H  5      n UR                  [        R
                  S:H  5      nU(       a+  UR                  [        R                  [	        U5      :H  5      nUR                  [        R                  R                  5       5      R                  5       $ ! [         a     Nyf = fr   )r   r   r   r|   re   r   r   period_yearr   r   r   r   )r   r|   r   ru   s       r   _courses_for_schoolr      s    
$$[%:%:c)n%LMAHH[22a78 HH[,,D	9:::k--113488::	  s    "C 
CCc                  ^ [        XU5      nU(       d  S / 4$ [        U=(       d    S5      nU(       d  S U4$ [        R                  " SSU5      n[        R                  " SSU5      R	                  5       n/ nU GH  n[        UR
                  =(       d    S5      mT(       d  M+  SnUT:X  d  UT;   d  TU;   a  UT:X  a  SOSnOUR                  5        V	s/ s H  o(       d  M  U	PM     n
n	TR                  5        V	s/ s H  o(       d  M  U	PM     nn	U
(       a,  [        U4S jU
 5       5      (       a  S	S
[        U
5      -  -   nOU
(       a  U(       a  U
S   US   :X  a  SnU(       d  M  UR                  X45        GM     U(       d  S U4$ UR                  S S9  US   S   nU VVs/ s H  u  pX:X  d  M  UPM     nnn[        U5      S:X  a  US   U4$ S U4$ s  sn	f s  sn	f s  snnf )Nr   u1   \b(curso|del|de|el|la|los|las|año|ano|20\d{2})\br!   r#   r   r   U   c              3  ,   >#    U  H	  oT;   v   M     g 7fr   r   r   s     r   r   match_course.<locals>.<genexpr>  s     0Rt)Rr   <      r_   c                    U S   * $ r   r   r   s    r   r   match_course.<locals>.<lambda>  r   r/   r   rj   )r   r.   r*   r)   r+   r   rq   r   r`   r3   r   )r   r|   r   r   coursesru   r   courser   r   qtntr   r   r   r   r   s                   @r   match_courser      s    ""6GRxekrAW}
CS!LA
vsA$$&A,.FV''-2.9T	TQY9C"EWWY,Y!!YB,!ZZ\/\Q!\B/c0R000QR[(r!u1~5MM5/*   W}
KKOK$!9Q<D 
/&BJ1&C
/
3x1}1vw9 -/ 0s$   
G$G>
G"G"+G':G'c                `   U =(       d    SR                  5       nU(       d  g [        R                  " SU[        R                  5      nU(       a  UR	                  S5      R                  5       $ [        U5      (       a1  [        R                  " SSU5      R                  5       nU=(       d    S $ g )Nr   uJ   (?:curso\s+)?(\d{1,2}\s*°?\s*(?:medio|b[aá]sico|b[aá]sica)[^\n,]{0,40})rj   z\b20\d{2}\br!   )r+   r*   rE   rn   rp   rV   r)   )rH   r-   rs   cleaneds       r   _course_query_from_textr     s    :2


C
		U
	A
 	wwqz!!$$&&c288:$r/   c                  U R                  [        [        5      R                  [        [        R                  [        R
                  :H  5      R                  [        [        R                  [        R
                  :H  5      R                  [        R                  S:H  [        R                  [        U5      :H  [        R                  [        U5      :H  [        R                  [        [        U5      5      :H  5      R                  [        R                   R#                  5       [        R$                  R#                  5       5      R'                  5       n[)        5       n/ nU GHZ  u  px[        UR
                  5      n	X;   a  M"  UR+                  U	5        U(       a$  [-        USS 5      =(       d    SR/                  5       OSn
U(       a$  [-        USS 5      =(       d    SR/                  5       OSnU(       a$  [-        USS 5      =(       d    SR/                  5       OSnSR                  S XU4 5       5      R/                  5       =(       d    SU	 3nU(       a$  [-        US	S 5      =(       d    SR/                  5       OS=(       d$    [-        US	S 5      =(       d    SR/                  5       nUR1                  XUS
.5        GM]     U$ )Nr   namesr   father_lastnamemother_lastnamer!   c              3  6   #    U  H  o(       d  M  Uv   M     g 7fr   r   )r   ps     r   r   'list_course_students.<locals>.<genexpr>P  s     @#:aa#:s   
	zEstudiante identification_number)idr   rut)r   r   r   	outerjoin
student_idr   r%   r   r   r   r|   re   r   r   strr   r   r   r   r   setaddgetattrr+   r3   )r   r|   r   r   rowsseenoutstudentpersonalsidr   fathermotherfullr   s                  r   list_course_studentsr   -  s    	78	$$//<??B

 
$$//<??B

 
**a/""c)n4$..#i.@$$CI6	

 
$4488:$**..0

 
) 	, UD "C!'**o;DL7D17R>>@RTOW'($5t<BIIK]_OW'($5t<BIIK]_xx@E6#:@@FFH_kZ]Y^L_  X6=CJJLM g6=CJJL	 	
 	

#C89 " Jr/   c                    U [         :X  d$  U(       a  SU=(       d    SR                  5       ;   a  gU [        :X  d$  U(       a  SU=(       d    SR                  5       ;   a  gg)NrP   r   u'   Informe de Evaluación PsicopedagógicarO   zInforme a la Familiainforme)PSYCHOPED_DOCUMENT_IDr&   FAMILIA_DOCUMENT_IDdocument_id
agent_names     r   
_doc_labelr   Z  sM    ++zj&6B%=%=%??8))yZ%52$<$<$>>%r/   c                    U S S  Vs/ s H;  oR                   =(       d    SR                  5       =(       d    SUR                   3PM=     nnU(       d  gSR                  U5      $ s  snf )Nr_   r   zCurso z(sin cursos), )r   r+   r   r%   )r   r   r   s      r   _format_course_listr   f  sZ    GNsPR|T|!mm!r((*=qttfo=|ET99U Us   AA%c                F  ^
 U =(       d    Sn[        U5      m
T
(       a  ST
;   a  gUR                  5       nS[        U5      ;   d  SU;   a  SU;   a  gg[        R                  " SSU=(       d    S5      R                  5       n[        R                  " S	ST
5      n[	        U5      S
:  a  XV;   a  g[        U5      R                  5        Vs/ s H  n[	        U5      S:  d  M  UPM     nnU(       d  g[        U
4S jU 5       5      n	U	[        S[	        U5      5      :  $ s  snf )zBTrue si el contexto de Files parece corresponder a ese estudiante.r   zsin trozos relevantesFzdatos extraidos de excelu   datos extraídos de excelzsin fila coincidenteTz[^0-9kK]z[^0-9k]r   rm   c              3  6   >#    U  H  oT;   d  M
  S v   M     g7f)rj   Nr   )r   r   r\   s     r   r   (files_mention_student.<locals>.<genexpr>  s     .&QIqq&s   		   )r.   r&   r*   r)   r`   rq   summin)files_blockr   r   r-   rW   
rut_digitsblob_digitsr   r   hitsr\   s             @r   files_mention_studentr   m  s    

C:D*d2
))+C!U3Z/3NRU3U!S(R399;J&&R.K
:!
 9t**,<,AA!a,F<.&..D3q#f+&&&	 =s   
D!D)default_yearsession_school_idc               N   [        X#5      (       d  g [        XE5      n[        X#5      n	[        X#5      =(       d-    U(       a$  S[	        U5      s=::  a  S::  a  O  O[	        U5      OS n
[        U	5      =(       d    [        U5      n[        XU5      u  pUcO  U(       aH  U(       dA  U=(       d    [        X5       H)  n[	        UR                  5      [	        U5      :X  d  M'  Un  O   Uc  [        U5      S:X  a  US   nOzU(       a7  U(       a0  SR                  S US S  5       5      n[        SU
[         S	U S
3S9$ SR                  S [        X5      S S  5       5      n[        SU
[         SU SU S
3S9$ U
c  [        U [	        UR                  5      S 5      n[        U Vs1 s H+  nUR                  (       d  M  [	        UR                  5      iM-     snSS9nU(       a  SR                  S US S  5       5      OSn[        S[	        UR                  5      UR                   [         SUR                   =(       d    SR#                  5        SU S3S9$ [%        U5      nU(       d  [%        U	5      n['        U [	        UR                  5      U
U5      u  nnUci  [)        U5      n[        S[	        UR                  5      UR                   U
[         SUR                   =(       d    SR#                  5        SU
 SU
 SU S
3
S9$ [+        U [	        UR                  5      [	        UR                  5      [	        U
5      S9nU(       d  [        S[	        UR                  5      UR                   U
[	        UR                  5      UR,                  SUR,                  =(       d    SR#                  5        SUR                   =(       d    SR#                  5        SU
 S 3S!9$ [        S[	        UR                  5      UR                   U
[	        UR                  5      UR,                  US"9$ s  snf )#Ni  i4  rj   r   r   c              3  f   #    U  H'  oR                   =(       d    S R                  5       v   M)     g7fr   Nr~   r+   r   r   s     r   r   $resolve_bulk_plan.<locals>.<genexpr>  s%     VEU}}299;;EU   /1   TzF. Hay varios establecimientos que coinciden. Indica el nombre exacto: .)rz   r   r   c              3  f   #    U  H'  oR                   =(       d    S R                  5       v   M)     g7fr   r   r  s     r   r   r    s'      7b!$"++--7br     z6. Indica el liceo o colegio (nombre) para elaborar el z. Establecimientos: )reversec              3  8   #    U  H  n[        U5      v   M     g 7fr   )r   )r   rf   s     r   r   r    s     8ic!ffis      2026z de **r   u   **. Indica el **año** (ej. u#   ) y el **curso** (ej. 1° Medio A).)rz   r|   r~   r   u   **, año **z**. Indica el curso. Cursos z: )rz   r|   r~   r   r   )r|   r   r   z No hay estudiantes activos en **** (u   ). Verifica el curso y el año.)rz   r|   r~   r   r   r   r   )rz   r|   r~   r   r   r   r   )r]   r   r>   rh   re   rv   r   r   r   r`   r%   rx   rQ   r   sortedr   r~   r+   r   r   r   r   r   )r   r   r6   r7   r   r   r   r   label	user_textr   school_qr   school_optsoptr   courses_previewr   rg   	year_hintcourse_qr   course_optslistedr   s                            r   resolve_bulk_planr    s    #744{/E7,I) )dc,6G.O4.OLUY 	 %Y/Q3H3QH&rAF~+HH"7"HHC366{c"344 I
 ~{q  ^FXIIV[QTRTEUVVE"m $005wa9	  II 7LR7]^a_a7b E "m $ 4UG1>	  |-b#fii.$G )(A== #AMM"(
 
 =BDII8eBQi88v	&))n**-vv'9'9'?R&F&F&H%I J++4+ 6%&	
 		
 'w/H*95&r3vyy>4JFK~$[1&))n**-vv'9'9'?R&F&F&H%IUYTZ [++/&6(!=	
 		
 $
c&))nFIISYH &))n**&))n**2F4F4F4L"3S3S3U2V W''-24467r$ @//
 	
 fii.&&fii.&& ms   "P";P"c                    [        X5      $ r   )r   r   s     r   bulk_document_labelr    s    k..r/   c                V    [          S[         SU  SU SU SU S[         S[         S3$ )	Nz. z. El curso **r  r   z
) tiene **z%** estudiantes. El tope por tanda es u-   . Responde **sí** para generar los primeros r  )rQ   _CONFIRM_MARKERMAX_BULK_STUDENTS)r   r~   r   totals       r   bulk_confirm_askr    sP     -r/* +!]${m2dV:eW M  12 366G5H	Kr/   c                   U  Vs/ s H(  o(       d  M  [        U5      R                  U:X  d  M&  UPM*     nnU(       d  g[        [        R                  5      S-  S-  nUR	                  SSS9  [
        R                  " [        R                  5      R                  S5      nSU S[        R                  " 5       R                  SS	  S
3nX5-  n[        R                  " US[        R                  S9 nU H-  nX8-  n	U	R!                  5       (       d  M  UR#                  XS9  M/     SSS5        UR!                  5       (       a  UR%                  5       R&                  S:  a  gUUSSU 3S.$ s  snf ! , (       d  f       NQ= f)zSEmpaqueta Word/PDF generados en files/system/students. Devuelve name + downloadUrl.Nsystemr   T)parentsexist_okz%Y%m%d_%H%M%Sinformes_curso__r
  z.zipw)compression)arcname   zInformes del curso (ZIP)z/files/system/students/)r   r   documentNamedownloadUrl)r   r   r   	files_dirmkdirr   nowr   utcstrftimeuuiduuid4hexzipfileZipFileZIP_DEFLATEDis_filewritestatst_size)
	filenamesnr   rootstampzip_namezip_pathzfr   srcs
             r   zip_generated_filesrC    s@   !=	1QQ47<<1+<Q	E=""#h.;DJJtdJ+LL&//@E q)9)9"1)=(>dCHH	3G4H4H	IRD+C{{}}+  
J
 !8!82!=20
;	  > 
J	Is!   
E+E+E+1E0E00
E>)r,   r   returnr   )r6   r   r7   list[dict[str, str]] | NonerD  r   )r7   rE  rD  r   )r6   r   rD  rF   r   )r6   r   r7   rE  rD  rF   )rH   r   rD  rF   )r6   r   r7   rE  rD  r{   )rH   r   rD  r}   )r   r
   r   re   rD  zlist[SchoolModel])r   r
   r   re   r   r}   rD  z,tuple[SchoolModel | None, list[SchoolModel]])r   r
   r|   re   r   r{   rD  list[CourseModel])
r   r
   r|   re   r   r{   r   r}   rD  z,tuple[CourseModel | None, list[CourseModel]])
r   r
   r|   re   r   re   r   re   rD  r   )r   r{   r   r}   rD  r   )r   rF  rD  r   )r   r   r   r   r   r}   rD  rF   )r   r
   r   re   r6   r   r7   rE  r   r{   r   r}   r   r{   r   r{   rD  zBulkPlan | None)
r   r   r~   r   r   re   r  re   rD  r   )r;  z	list[str]rD  zdict[str, str] | None)A__doc__
__future__r   r*   r   r1  r4  dataclassesr   r   r   r   pathlibr   typingr	   sqlalchemy.ormr
   app.backend.core.configr   app.backend.db.models.pie_corer   r   r   r   r   r  r   r   compilerU   rn   rS   rT   rR   rD   rG   rQ   r  rK   r(   r.   r:   r>   rB   rI   rM   r]   rV   rh   rv   rx   r   r   r   r   r   r   r   r   r   r  r  r  rC  r   r/   r   <module>rP     s   : " 	    ( '   " ,     ::&'JJEMM	 **2BMMBjjGW
**JMM
 ZZ? MM  :@jj7MM jj$,1<
 :>  6 	 F* A A A;!*41B;$$$'1$:D$1$N"**"*/2*:=**Z	'<  $$(ww w 	w
 )w w w w "w wt/  	
  	r/   