o
    ó·…iç  ã                   @   s¾   d dl Zd dlmZmZ d dlZd dlmZmZm	Z	 de
dede
de
fdd	„Zde
d
e
de
dedede
fdd„Zde
dee fdd„Zde
de
de
dedede
fdd„Zde
defdd„ZdS )é    N)ÚA_DB_CONFIGÚlogger)ÚListÚDictÚOptionalÚ
session_idÚquestion_numberÚquestion_textÚanswer_textc              
   C   s¦   d }zDz-t jjdi t¤Ž}| ¡ }d}| || |||f¡ | ¡  |jW W |r0| ¡  | ¡  S S  t	yF } z
t
 d|› ¡ |‚d }~ww |rR| ¡  | ¡  w w )Nz™
            INSERT INTO studyplan_log
            (session_id, question_number, question_text, answer_text)
            VALUES (%s, %s, %s, %s)
        zStudyPlan Question Save Error: © ©ÚmysqlÚ	connectorÚconnectr   ÚcursorÚexecuteÚcommitÚ	lastrowidÚcloseÚ	Exceptionr   Úerror)r   r   r	   r
   Úconnr   ÚsqlÚer   r   ú=/home/air/goalskill_sales/back/app/models/studyplan_module.pyÚsave_studyplan_question   s*   
þü€þ
þr   Útotal_durationÚcurrent_statusÚweekday_hoursÚweekend_hoursÚpreferred_time_slotc           
   
   C   sª   d }zFz/t jjdi t¤Ž}| ¡ }d}| || |||||f¡ | ¡  |jW W |r2| ¡  | ¡  S S  t	yH }	 z
t
 d|	› ¡ |	‚d }	~	ww |rT| ¡  | ¡  w w )Na4  
            INSERT INTO studyplan_Output
            (session_id, total_duration, current_status, weekday_hours, weekend_hours, preferred_time_slot)
            VALUES (%s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE
                total_duration = VALUES(total_duration),
                current_status = VALUES(current_status),
                weekday_hours = VALUES(weekday_hours),
                weekend_hours = VALUES(weekend_hours),
                preferred_time_slot = VALUES(preferred_time_slot),
                updated_at = NOW()
        zStudyPlan Output Save Error: r   r   )
r   r   r   r   r   r    r   r   r   r   r   r   r   Úsave_studyplan_output    s*   
þü€þ
þr!   Úreturnc           	   
      s  d}z~zUt jjdi t¤Ž}|jdd}d}| || f¡ | ¡ }i ‰ |D ]}|d }|ˆ vr<|d |d |d dœˆ |< q$‡ fd	d
„tˆ  ¡ ƒD ƒ}|W W |rX| 	¡  | 	¡  S S  t
y€ } zt d|› ¡ g W  Y d}~W |r{| 	¡  | 	¡  S S d}~ww |rŒ| 	¡  | 	¡  w w )uˆ   
    ì„¸ì…˜ì˜ ëª¨ë“  ë‹µë³€ ì¡°íšŒ
    
    Returns:
        ë‹µë³€ ë¦¬ìŠ¤íŠ¸ (question_number, question_text, answer_text í¬í•¨)
    NT©Ú
dictionaryz¿
            SELECT question_number, question_text, answer_text, id
            FROM studyplan_log
            WHERE session_id = %s
            ORDER BY question_number ASC, id DESC
        r   r	   r
   )r   r	   r
   c                    s   g | ]}ˆ | ‘qS r   r   )Ú.0Úq_num©Úunique_answersr   r   Ú
<listcomp>d   s    z*get_answers_by_session.<locals>.<listcomp>zStudyPlan Get Answers Error: r   )r   r   r   r   r   r   ÚfetchallÚsortedÚkeysr   r   r   r   )	r   r   r   r   Úall_resultsÚrowr&   Úresultsr   r   r'   r   Úget_answers_by_sessionA   sF   
ý€
þü
þ€ü
þr0   Ústudyplan_durationÚstudyplan_statusÚstudyplan_weekday_hoursÚstudyplan_weekend_hoursÚstudyplan_time_slotc           
   
   C   s¼   d}zOz)t jjdi t¤Ž}| ¡ }d}| || |||||f¡ | ¡  t d| › ¡ W n t	yB }	 z
t 
d|	› ¡ |	‚d}	~	ww W |rP| ¡  | ¡  dS dS |r]| ¡  | ¡  w w )uC   StudyPlan ì™„ë£Œ ì‹œ user_profile_summary í…Œì´ë¸”ì—ë„ ë™ê¸°í™”Na†  
            INSERT INTO user_profile_summary
            (session_id, studyplan_duration, studyplan_status, studyplan_weekday_hours, studyplan_weekend_hours, studyplan_time_slot)
            VALUES (%s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE
                studyplan_duration = VALUES(studyplan_duration),
                studyplan_status = VALUES(studyplan_status),
                studyplan_weekday_hours = VALUES(studyplan_weekday_hours),
                studyplan_weekend_hours = VALUES(studyplan_weekend_hours),
                studyplan_time_slot = VALUES(studyplan_time_slot),
                updated_at = NOW()
        z6StudyPlan synced to user_profile_summary for session: zStudyPlan Summary Sync Error: r   )r   r   r   r   r   r   r   r   Úinfor   r   r   )
r   r1   r2   r3   r4   r5   r   r   r   r   r   r   r   Úsync_to_user_profile_summaryq   s*   €þþþ
þr7   c              
   C   sÆ   d}zTz+t jjdi t¤Ž}|jdd}d}| || f¡ | ¡ }|W W |r.| ¡  | ¡  S S  tyV } zt	 
d|› ¡ W Y d}~W |rP| ¡  | ¡  dS dS d}~ww |rb| ¡  | ¡  w w )up  
    user_profile_summary í…Œì´ë¸”ì—ì„œ StudyPlan ë°ì´í„° ê°€ì ¸ì˜¤ê¸°
    
    Returns:
        {
            'studyplan_duration': str,
            'studyplan_status': str,
            'studyplan_weekday_hours': int,
            'studyplan_weekend_hours': int,
            'studyplan_time_slot': str  # ì›ë³¸ í…ìŠ¤íŠ¸ (ì˜ˆ: "å¤•æ–¹")
        } ë˜ëŠ” None
    NTr#   a!  
            SELECT 
                studyplan_duration,
                studyplan_status,
                studyplan_weekday_hours,
                studyplan_weekend_hours,
                studyplan_time_slot
            FROM user_profile_summary
            WHERE session_id = %s
        z"StudyPlan Get From Summary Error: r   )r   r   r   r   r   r   Úfetchoner   r   r   r   )r   r   r   r   Úresultr   r   r   r   Úget_studyplan_from_summary“   s2   
þüþ€ü
þr:   )Úmysql.connectorr   Úapp.core.configr   r   ÚjsonÚtypingr   r   r   ÚstrÚintr   r!   r0   r7   Údictr:   r   r   r   r   Ú<module>   s    "!"0"