"""B20 wiring — Legacy-URL-Discovery + Mail-Block."""

from __future__ import annotations

import html
import logging
import os

from compliance.services.legacy_url_discovery import discover_legacy_urls
from compliance.services.multi_version_dse import (
    analyze_multiple_dse_versions, render_multi_version_block,
)

logger = logging.getLogger(__name__)


_DISABLED = os.environ.get("LEGACY_URL_DISABLED", "").lower() in (
    "1", "true", "yes",
)


async def run_b20(state: dict) -> None:
    if _DISABLED:
        return
    try:
        result = await discover_legacy_urls(state)
    except Exception as e:
        logger.warning("legacy-url-discovery failed: %s", e)
        return
    candidates = result.get("candidates") or []
    state["legacy_url_inventory"] = result
    if candidates:
        state["legacy_url_html"] = _render(result)
        logger.info(
            "B20 legacy-url: %d candidates of %d probed",
            len(candidates), result.get("probed", 0),
        )

    # Plan C — Multi-Version-DSE-Analyse: falls Legacy-Discovery zusätz-
    # liche DSE-URLs liefert UND ≥2 reachable sind, parallele Analyse +
    # Vergleichsblock.
    try:
        mv_info = await analyze_multiple_dse_versions(state)
        if mv_info.get("versions") and len(mv_info["versions"]) >= 2:
            state["multi_version_dse_info"] = mv_info
            state["multi_version_dse_html"] = render_multi_version_block(
                mv_info,
            )
            logger.info(
                "B20-C multi-version-dse: %d versions, date_div=%s dsb_div=%s",
                len(mv_info["versions"]),
                mv_info.get("date_divergent"),
                mv_info.get("dsb_divergent"),
            )
    except Exception as e:
        logger.warning("multi-version-dse analysis failed: %s", e)


def _render(result: dict) -> str:
    candidates = result.get("candidates") or []
    if not candidates:
        return ""
    rows = []
    for c in candidates[:25]:
        st = c["status"]
        sev_color = (
            "#dc2626" if "Legacy-Verdacht" in (c.get("recommendation") or "")
            else "#f59e0b" if st in (404, 410) else "#64748b"
        )
        age = c.get("age_months")
        age_disp = f"{age} Mo." if age is not None else "—"
        rec = c.get("recommendation") or "—"
        rows.append(
            f"<tr>"
            f"<td style='padding:5px 8px;font-family:monospace;color:#475569;"
            f"font-size:11px;max-width:380px;word-break:break-all;'>"
            f"<a href='{html.escape(c['url'])}' "
            f"style='color:{sev_color};'>{html.escape(c['url'][:120])}</a>"
            f"</td>"
            f"<td style='padding:5px 8px;font-size:11px;text-align:center;'>"
            f"<strong style='color:{sev_color};'>{st or '?'}</strong></td>"
            f"<td style='padding:5px 8px;font-size:11px;text-align:center;'>"
            f"{age_disp}</td>"
            f"<td style='padding:5px 8px;font-size:11px;text-align:center;'>"
            f"{'✓' if c.get('in_footer') else '—'}</td>"
            f"<td style='padding:5px 8px;font-size:11px;color:#475569;'>"
            f"{html.escape(rec)}</td>"
            f"</tr>"
        )
    rest = ""
    if len(candidates) > 25:
        rest = (
            f"<p style='font-size:12px;color:#64748b;margin-top:6px;'>"
            f"<em>… und {len(candidates)-25} weitere — vollständig in "
            f"<code>legacy-urls.csv</code> im ZIP-Anhang.</em></p>"
        )
    return (
        "<div style='margin:24px 0;padding:16px;border-left:4px solid #0f766e;"
        "background:#f0fdfa;border-radius:4px;'>"
        "<h2 style='margin:0 0 8px;color:#134e4a;font-size:16px;'>"
        f"🗂️ Legacy-URL-Inventar ({len(candidates)} Kandidaten von "
        f"{result.get('probed', '?')} geprüft)"
        "</h2>"
        "<p style='margin:0 0 8px;font-size:12px;color:#475569;'>"
        "Quellen: /sitemap.xml + Wayback-Machine + Slug-Permutations. "
        "Wir <strong>entscheiden nicht</strong> ob eine URL Legacy ist — "
        "wir präsentieren das Inventar mit Status und Empfehlung. Der "
        "Kunde entscheidet."
        "</p>"
        "<table style='font-size:11px;width:100%;border-collapse:collapse;"
        "background:#fff;border-radius:4px;'>"
        "<thead><tr style='background:#ccfbf1;'>"
        "<th style='padding:6px 8px;text-align:left;'>URL</th>"
        "<th style='padding:6px 8px;'>HTTP</th>"
        "<th style='padding:6px 8px;'>Wayback-Alter</th>"
        "<th style='padding:6px 8px;'>Footer</th>"
        "<th style='padding:6px 8px;text-align:left;'>Empfehlung</th>"
        "</tr></thead><tbody>" + "".join(rows) + "</tbody></table>"
        + rest +
        "</div>"
    )
