
    ,h5i%                        d 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	m
Z
mZmZ ddlmZ ddlmZmZ dd	lmZ  G d
 d      ZddZy)zHTML report generator.    )annotations)datetime)Path)Any)EnvironmentFileSystemLoaderPackageLoaderselect_autoescape)KPITree)KPIAnalyzerBottleneckInfo)AnalysisResultc                      e Zd ZdZd
ddZ	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 ddZeddd       Zeddd       Z	edd       Z
edd	       Zy)HTMLReportGeneratorz(Generate HTML reports from KPI analysis.Nc                (   |r0t        t        t        |            t        ddg            | _        nTt        t        t        t              j                  j                  j                  dz        t        ddg            | _        | j                  | j                  j                  d<   | j                  | j                  j                  d<   | j                  | j                  j                  d<   | j                  | j                  j                  d<   y	)
zkInitialize generator.

        Args:
            template_dir: Optional custom template directory.
        htmlxml)loader
autoescape	templatesformat_numberformat_percentstatus_class
trend_iconN)r   r   strr
   envr   __file__parent_format_numberfilters_format_percent_status_class_trend_icon)selftemplate_dirs     d/var/www/tkim.planitai.co.jp/gemegg/20251207-make-pdf-report/project/planitai-kpi/src/output/html.py__init__zHTMLReportGenerator.__init__   s     "'L(9:,fe_=DH #'X(=(=(D(D(K(Kk(YZ,fe_=DH -1,?,?)-1-A-A)*+/+=+=()-)9)9&    c                   t        |      }|j                         r|d|j                  dd       dz  }n|}|j                  }|j	                  dd       t        |      }| j                  |||||      }| j                  j                  d      }	 |	j                  di |}
t        |dd	
      5 }|j                  |
       ddd       |S # 1 sw Y   |S xY w)aM  Generate HTML report.

        Args:
            tree: KPI tree with calculated values.
            analysis_results: AI analysis results.
            period: Reporting period.
            output_path: Output directory or file path.
            title: Report title.

        Returns:
            Path to generated HTML file.
        report_ _z.htmlT)parentsexist_okzreport.htmlwzutf-8)encodingN )r   is_dirreplacer   mkdirr   _prepare_contextr   get_templaterenderopenwrite)r$   treeanalysis_resultsperiodoutput_pathtitleoutput_fileanalyzercontexttemplater   fs               r&   generatezHTMLReportGenerator.generate+   s    ( ;' %'&..c2J1K5(QQK%K%,,K$6 t$''h8H&RWX 88((7x)) +sW5 	GGDM	 	 s   2CCc                   |j                         }|j                         }|j                         }|j                         }	|j	                  d      }
|j	                  d      }|j	                  d      }||t        j                         j                  d      ||j                         |j                         |D cg c]  }|j                          c}|dd D cg c]  }|j                          c}t        |      |	|
r|
j                  ni |r|j                  ni |r|j                  ni |
r|
j                  dS ddS c c}w c c}w )	a&  Prepare template context.

        Args:
            tree: KPI tree.
            analyzer: KPI analyzer.
            analysis_results: AI analysis results.
            period: Reporting period.
            title: Report title.

        Returns:
            Template context dictionary.
        executive_summarytrendsbottlenecksz%Y-%m-%d %H:%MN    )r>   r<   generated_atkgir:   	tree_textperformancerH   bottleneck_count
categoriesrF   trend_analysisbottleneck_insights
ai_summary)get_kgi_summaryget_performance_summaryfind_bottlenecksget_category_summarygetr   nowstrftimeto_dictto_textlendatasummary)r$   r:   r@   r;   r<   r>   kgi_summaryrN   rH   category_summary	executiverG   bottleneck_analysispbs                  r&   r5   z$HTMLReportGenerator._prepare_contextX   s4   * ..0 668 //1 $88: %(()<=	!%%h/.22=A $LLN334DELLN1<=AAIIK=1<RaAAAIIKA #K 0*3<"-3fkk?R#6#;#;XZ/8)+++
 	
* ?A+
 	
 >As   ;E Ec                *    | y|dk(  r| dS | d| dS )z'Format number with thousands separator.-r   z,.0fz,.rC   r1   valuedecimalss     r&   r   z"HTMLReportGenerator._format_number   s0     =q=D\"8*A~&'r(   c                    | y| d| ddS )zFormat as percentage.rg   .rC   %r1   rh   s     r&   r!   z#HTMLReportGenerator._format_percent   s$     =(1}%Q''r(   c                "    | y| dk\  ry| dk\  ryy)z(Get CSS class based on achievement rate.neutrald   successP   warningdangerr1   )achievement_rates    r&   r"   z!HTMLReportGenerator._status_class   s&     #s"r!r(   c                2    dddd}|j                  | d      S )zGet icon for trend.u   ↑u   →u   ↓)	improvingstable	declining)rX   )trendiconss     r&   r#   zHTMLReportGenerator._trend_icon   s'     

 yy&&r(   )N)r%   zstr | Path | None)zPlanitAI KPI Report)r:   r   r;   dict[str, AnalysisResult]r<   r   r=   z
str | Pathr>   r   returnr   )r:   r   r@   r   r;   r|   r<   r   r>   r   r}   zdict[str, Any])r   )ri   float | Nonerj   intr}   r   )   )ru   r~   r}   r   )rz   r   r}   r   )__name__
__module____qualname____doc__r'   rD   r5   staticmethodr   r!   r"   r#   r1   r(   r&   r   r      s    2:< +++ 4+ 	+
  + + 
+Z;
;
 ;
 4	;

 ;
 ;
 
;
z ( ( ( (   ' 'r(   r   c                      y)z"Generate embedded CSS for reports.a  
    :root {
        --primary: #2563eb;
        --success: #22c55e;
        --warning: #f59e0b;
        --danger: #ef4444;
        --neutral: #6b7280;
        --bg: #f8fafc;
        --card-bg: #ffffff;
        --text: #1e293b;
        --text-secondary: #64748b;
        --border: #e2e8f0;
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }

    body {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        background: var(--bg);
        color: var(--text);
        line-height: 1.6;
    }

    .container { max-width: 1200px; margin: 0 auto; padding: 2rem; }

    .header {
        background: var(--primary);
        color: white;
        padding: 2rem;
        margin-bottom: 2rem;
        border-radius: 0.5rem;
    }

    .header h1 { font-size: 1.5rem; margin-bottom: 0.5rem; }
    .header .period { opacity: 0.9; }

    .grid { display: grid; gap: 1.5rem; }
    .grid-2 { grid-template-columns: repeat(2, 1fr); }
    .grid-3 { grid-template-columns: repeat(3, 1fr); }
    .grid-4 { grid-template-columns: repeat(4, 1fr); }

    @media (max-width: 768px) {
        .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    }

    .card {
        background: var(--card-bg);
        border-radius: 0.5rem;
        padding: 1.5rem;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        border: 1px solid var(--border);
    }

    .card-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
        border-bottom: 1px solid var(--border);
    }

    .card-title { font-size: 1rem; font-weight: 600; }

    .metric { text-align: center; padding: 1rem; }
    .metric-value { font-size: 2rem; font-weight: 700; }
    .metric-label { color: var(--text-secondary); font-size: 0.875rem; }

    .success { color: var(--success); }
    .warning { color: var(--warning); }
    .danger { color: var(--danger); }
    .neutral { color: var(--neutral); }

    .badge {
        display: inline-block;
        padding: 0.25rem 0.5rem;
        border-radius: 0.25rem;
        font-size: 0.75rem;
        font-weight: 500;
    }

    .badge-success { background: #dcfce7; color: #166534; }
    .badge-warning { background: #fef3c7; color: #92400e; }
    .badge-danger { background: #fee2e2; color: #991b1b; }

    table { width: 100%; border-collapse: collapse; }
    th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid var(--border); }
    th { font-weight: 600; color: var(--text-secondary); font-size: 0.875rem; }

    .progress-bar {
        height: 8px;
        background: var(--border);
        border-radius: 4px;
        overflow: hidden;
    }

    .progress-fill {
        height: 100%;
        border-radius: 4px;
        transition: width 0.3s;
    }

    .highlight-box {
        padding: 1rem;
        border-radius: 0.5rem;
        margin-bottom: 1rem;
    }

    .highlight-positive { background: #dcfce7; border-left: 4px solid var(--success); }
    .highlight-negative { background: #fee2e2; border-left: 4px solid var(--danger); }
    .highlight-neutral { background: #f1f5f9; border-left: 4px solid var(--neutral); }

    .tree-node { margin-left: 1.5rem; padding: 0.5rem 0; }
    .tree-node-kgi { font-weight: 700; font-size: 1.1rem; }
    .tree-node-kpi { font-weight: 500; }
    .tree-node-input { color: var(--text-secondary); }

    .footer {
        text-align: center;
        padding: 2rem;
        color: var(--text-secondary);
        font-size: 0.875rem;
    }
    r1   r1   r(   r&   generate_embedded_cssr      s    {r(   N)r}   r   )r   
__future__r   r   pathlibr   typingr   jinja2r   r   r	   r
   src.kpi.treer   src.kpi.analyzerr   r   src.ai.analyzerr   r   r   r1   r(   r&   <module>r      s6     "    R R   8 *h' h'V}r(   