o
    ji@)                     @   sL  d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlZd dlZd dlZd dl	mZ d dlZejejd	 eeZed
dgdZeddgdZeddefddZeddefddZeddefddZeddefddZeddefddZeddefd d!Z ed"d#edefd$d%Z!ed&defd'd(Z"ed)defd*d+Z#e$d,defd-d.Z%e$d/defd0d1Z&e$d2defd3d4Z'e$d5defd6d7Z(e$d8defd9d:Z)ed;defd<d=Z*ed>defd?d@Z+edAdefdBdCZ,dS )D    )	APIRouterHTTPException)progress_module)StartCurriculumRequest)get_gemini_model)HTMLResponse)typesN)FileResponse)levelz/goalskill-sales/api/progressProgress)prefixtagsz/goalskill-sales/api/fileFilesz/chart-view/{session_id}
session_idc                    x   t jt jt}t j|}t j|}t j|}t j|ddd}t j|r0t|S t	d|  ddiS )Nfront
componentszdashboard.htmlzDashboard file not found at: errorzDashboard file not found
ospathdirnameabspath__file__joinexistsr	   loggerr   r   current_file_dirapp_dirback_dirroot_dir	file_path r#   =/home/air/goalskill_sales/back/app/routers/progress_router.pyget_chart_page   s   r%   z/chart-data/{session_id}c              
      H   zt | }|W S  ty# } ztd|  tdddd}~ww )uE   
    DB에서 실제 데이터를 조회하여 JSON으로 반환
    zError fetching chart data:   Internal Server Errorstatus_codedetailN)r   get_real_chart_data	Exceptionr   r   r   r   dataer#   r#   r$   get_chart_data_api/   s   
r1   z/chart-view-am/{session_id}c                    s   t jt jt}t jt jt j|}t jt jt}t j|}t j|}t j|}t j|ddd}t j|rHt|S t	d|  ddiS )Nr   r   zdashboard_AM.htmlzAM Dashboard not found: r   File not foundr   )r   r   r!   r   r    r"   r#   r#   r$   get_chart_page_am<   s   r3   z/chart-data-am/{session_id}c              
      D   zt | W S  ty! } ztd|  tdddd }~ww )NzAM API Error: r'   Server Errorr)   )r   get_am_chart_datar-   r   r   r   r   r0   r#   r#   r$   get_chart_data_amO      r8   z/chart-view-pm/{session_id}c                    r   )Nr   r   zdashboard_PM.htmlzPM Dashboard not found: r   r2   r   r   r#   r#   r$   get_chart_page_pmY      r:   z/chart-data-pm/{session_id}c              
      r4   )NzPM API Error: r'   r5   r)   )r   get_pm_chart_datar-   r   r   r   r7   r#   r#   r$   get_chart_data_pmh   r9   r=   z(/download-excel/{time_type}/{session_id}	time_typec           
   
      s  zpt jt jt}t jt jt j|}|  dkr#d}n|  dkr,d}n|  dkr5d}nd|   }t j|dd	|}t j|d
| d}t|}|s_dd| iW S t	|t jj
d}t|t j|dW S  ty }	 zdt|	iW  Y d }	~	S d }	~	ww )NtodaySales_todayGOAL
Sales_GOALRESULTSales_resultSales_r   progress_xls*z.xlsxr   zExcel file not found in )key)r   filename)r   r   r   r   r   lowerupperr   globmaxgetmtimer	   basenamer-   str)
r>   r   r   r!   folder_name
target_dirsearch_patternfileslatest_filer0   r#   r#   r$   download_excelq   s,   
rV   z/chart-view-result/{session_id}c                    s`   t jt jt}t jt jt j|}t j|ddd}t j|r,t|S ddiS )Nr   r   zdashboard_result.htmlr   zDashboard Result file not found)r   r   r   r   r   r   r   r	   )r   r   r!   r"   r#   r#   r$   get_chart_page_result   s   rW   z/chart-data-result/{session_id}c              
      r4   )NzResult API Error: r'   r5   r)   )r   get_result_chart_datar-   r   r   r   r7   r#   r#   r$   get_chart_data_result   r9   rY   z%/generate-afternoon-plan/{session_id}c                    s,   t | }|d dkrtd|d d|S )u<   
    오후 계획 생성 및 DB 업데이트 트리거
    statusr   r'   messager)   )r   execute_afternoon_adjustmentr   r   resultr#   r#   r$   generate_afternoon_plan   s
   
r_   z/update-report-am/{session_id}c                    .   t | d}|d dkrtd|d d|S )u=   
    AM 리포트 엑셀 업데이트 (DB 실적 반영)
    AMrZ   r   r'   r[   r)   r   execute_report_updater   r]   r#   r#   r$   update_report_am   
   rd   z/update-report-pm/{session_id}c                    r`   )u=   
    PM 리포트 엑셀 업데이트 (DB 실적 반영)
    PMrZ   r   r'   r[   r)   rb   r]   r#   r#   r$   update_report_pm   re   rg   z/calc-rate-am/{session_id}c                    r`   )u]   
    Sales_C_DB > today_sales_am_daily 테이블의 achievement_rate 계산 업데이트
    amrZ   r   r'   r[   r)   r   update_achievement_rate_dbr   r]   r#   r#   r$   calc_rate_am   re   rk   z/calc-rate-pm/{session_id}c                    r`   )u]   
    Sales_C_DB > today_sales_pm_daily 테이블의 achievement_rate 계산 업데이트
    pmrZ   r   r'   r[   r)   ri   r]   r#   r#   r$   calc_rate_pm   re   rm   z/ai-dashboard/{session_id}c                    s&   d}t j|rt|S tddd)u,   
    AI 분석 대시보드 HTML 반환
    z</home/air/goalskill_sales/front/components/dashboard_ai.htmli  z%dashboard_ai.html not found on serverr)   )r   r   r   r	   r   )r   r"   r#   r#   r$   ai_dashboard_view   s
   rn   z/chart-view-debug/{session_id}c                    r   )u+   デバッグ日誌 대시보드 HTML 반환r   r   zdashboard_debug.htmlzDebug Dashboard not found: r   zdashboard_debug.html not foundr   r   r#   r#   r$   get_chart_page_debug   r;   ro   z/chart-data-debug/{session_id}c              
      r&   )u0   MBTI scores + SPI 데이터를 JSON으로 반환zDebug Chart Data API Error: r'   r(   r)   N)r   get_debug_chart_datar-   r   r   r   r.   r#   r#   r$   get_chart_data_debug  s   
rq   )-fastapir   r   
app.modelsr   app.schemas.progress_schemar   app.core.configr   fastapi.responsesr   google.genair   jsonr   loggingr	   rL   basicConfigINFO	getLogger__name__r   routerfile_routergetrP   r%   r1   r3   r8   r:   r=   rV   rW   rY   postr_   rd   rg   rk   rm   rn   ro   rq   r#   r#   r#   r$   <module>   sn    
	
