""" HTML email report builder for document checks. Generates a styled HTML report similar to the frontend ChecklistView, including L1/L2 check hierarchy, progress bars, and actionable hints. """ from __future__ import annotations from typing import TYPE_CHECKING if TYPE_CHECKING: from .agent_doc_check_routes import CheckItem, DocCheckResult def _bar(pct: int, color: str) -> str: bg = {"green": "#22c55e", "yellow": "#eab308", "red": "#ef4444", "blue": "#60a5fa"} c = bg.get(color, "#60a5fa") return ( f'
' f'
' f'
{pct}%' ) def _icon(passed: bool, skipped: bool = False) -> str: if skipped: return '' if passed: return '' return '' def _hint_box(hint: str) -> str: return ( f'
{hint}
' ) def build_html_report( results: list[DocCheckResult], cookie_result: dict | None, ) -> str: """Build HTML email report styled like the frontend.""" ok_count = sum(1 for r in results if r.completeness_pct == 100) html = [ '
', '

Dokumenten-Pruefung

', f'

' f'{len(results)} Dokumente, {ok_count} vollstaendig

', ] for r in results: _render_document(html, r) if cookie_result: _render_cookie_banner(html, cookie_result) html.append('
') return "\n".join(html) def _render_document(html: list[str], r: DocCheckResult) -> None: pct = r.completeness_pct cpct = r.correctness_pct bar_color = "green" if pct >= 80 else "yellow" if pct >= 50 else "red" status_label = "OK" if pct == 100 else "LUECKENHAFT" if pct >= 50 else "MANGELHAFT" if r.error: status_label = "FEHLER" l1_checks = [c for c in r.checks if c.level == 1] l2_by_parent: dict[str, list[CheckItem]] = {} for c in r.checks: if c.level == 2 and c.parent: l2_by_parent.setdefault(c.parent, []).append(c) l1_passed = sum(1 for c in l1_checks if c.passed) l2_active = [c for c in r.checks if c.level == 2 and not c.skipped] l2_passed = sum(1 for c in l2_active if c.passed) # Header html.append( f'
' f'
' f'
' f'{status_label}' f'{r.label}' f'
' f'{l1_passed}/{len(l1_checks)} Pflichtangaben' ) if l2_active: html.append(f', {l2_passed}/{len(l2_active)} Detailpruefungen') html.append(f'
{_bar(pct, bar_color)}') if cpct and l2_active: html.append(f'
{_bar(cpct, "blue")}') html.append('
') # Body if r.error: html.append(f'
{r.error}
') else: html.append('
') for c in l1_checks: _render_l1_check(html, c, l2_by_parent.get(c.id, [])) if r.word_count: html.append( f'
' f'{r.word_count} Woerter analysiert
' ) html.append('
') html.append('
') def _render_l1_check( html: list[str], c: CheckItem, children: list[CheckItem], ) -> None: l2_sub = [ch for ch in children if not ch.skipped] l2_passed = sum(1 for ch in l2_sub if ch.passed) style = "color:#991b1b;font-weight:600" if not c.passed else "color:#374151" html.append( f'
{_icon(c.passed)} ' f'{c.label}' ) if l2_sub: html.append(f' ({l2_passed}/{len(l2_sub)})') if not c.passed and c.hint: html.append(_hint_box(c.hint)) html.append('
') for ch in children: if ch.skipped: continue _render_l2_check(html, ch) def _render_l2_check(html: list[str], ch: CheckItem) -> None: style = "color:#dc2626;font-weight:500" if not ch.passed else "color:#6b7280" html.append( f'
' f'{_icon(ch.passed)} ' f'{ch.label}' ) if ch.passed and ch.matched_text: html.append( f'
"...{ch.matched_text[:80]}..."
' ) if not ch.passed and ch.hint: html.append(_hint_box(ch.hint)) html.append('
') def _render_cookie_banner(html: list[str], cookie_result: dict) -> None: html.append( '
' 'Cookie-Banner Pruefung
' f'Banner erkannt: {cookie_result.get("banner_detected", False)}
' f'Anbieter: {cookie_result.get("banner_provider", "unbekannt")}' ) violations = cookie_result.get("banner_checks", {}).get("violations", []) if violations: for v in violations[:10]: html.append(f'
{_icon(False)} {v.get("text", "")[:80]}') else: html.append('
Keine Verstoesse erkannt.') html.append('
')