"""B5 wiring — AI-Act Art. 50 Transparenzpflicht-Check + HTML block.

Runs after B4 (vendor-consistency). Deterministic detection of
AI-Provider mentions + disclosure-phrase mentions. When an AI is
present but no Art-50-disclosure → HIGH finding; when both present
the renderer flags MEDIUM/manual-review because the LIVE pre-chat
UI hint cannot be verified without a consent-tester DOM scan.
"""

from __future__ import annotations

import html
import logging

from compliance.services.ai_act_transparency_check import (
    check_ai_act_transparency,
)

logger = logging.getLogger(__name__)


def run_b5(state: dict) -> None:
    findings = check_ai_act_transparency(state)
    state["ai_act_findings"] = findings
    if not findings:
        return
    state["ai_act_html"] = _render(findings)
    logger.info(
        "B5 AI-Act: %d findings (HIGH=%d, MEDIUM=%d)",
        len(findings),
        sum(1 for f in findings if (f.get("severity") or "") == "HIGH"),
        sum(1 for f in findings if (f.get("severity") or "") == "MEDIUM"),
    )


def _render(findings: list[dict]) -> str:
    cards = []
    for f in findings:
        sev = (f.get("severity") or "").upper()
        color = "#dc2626" if sev == "HIGH" else "#f59e0b"
        vendors_html = ""
        if f.get("ai_vendors"):
            chips = "".join(
                f"<span style='display:inline-block;background:#f1f5f9;"
                f"padding:2px 8px;border-radius:999px;margin:2px 4px 2px 0;"
                f"font-size:11px;'>{html.escape(v.get('vendor') or '—')}</span>"
                for v in f["ai_vendors"]
            )
            vendors_html = (
                "<div style='margin-top:6px;font-size:13px;'>"
                f"<strong>Erkannte AI-Vendors:</strong> {chips}</div>"
            )
        signals_html = (
            f"<div style='font-size:12px;color:#475569;margin-top:6px;'>"
            f"<em>{html.escape(f.get('detected_signals') or '')}</em></div>"
        )
        cards.append(
            f"<div style='margin:12px 0;padding:14px;background:#fff;"
            f"border-left:3px solid {color};border-radius:4px;'>"
            f"<div style='font-weight:600;color:{color};font-size:14px;'>"
            f"{sev} · {html.escape(f.get('check_id') or '')}</div>"
            f"<div style='font-size:14px;margin-top:4px;'>"
            f"<strong>{html.escape(f.get('title') or '')}</strong></div>"
            f"<div style='font-size:12px;color:#64748b;margin-top:2px;'>"
            f"{html.escape(f.get('norm') or '')}</div>"
            f"{vendors_html}{signals_html}"
            f"<div style='font-size:13px;margin-top:8px;background:#dcfce7;"
            f"padding:8px 10px;border-radius:4px;'>"
            f"<strong>→ Empfehlung:</strong> "
            f"{html.escape(f.get('action') or '')}</div>"
            "</div>"
        )
    return (
        "<div style='margin:24px 0;padding:16px;border-left:4px solid #dc2626;"
        "background:#fef2f2;border-radius:4px;'>"
        "<h2 style='margin:0 0 8px;color:#991b1b;font-size:16px;'>"
        "🤖 AI-Act Art. 50 — Transparenzpflicht KI-Interaktion"
        "</h2>"
        + "".join(cards) +
        "</div>"
    )
