"""Daily digest 빌더 (Markdown / HTML).

규칙: 변화 없는 항목은 표시하지 않는다.
"""

from __future__ import annotations

import html
from typing import Any, Dict, List

from .classifier import classify_bucket
from .summarizer import summarize


DISCLAIMER_KO = (
    "본 도구는 연구·교육 목적의 참고용이며, 임상시험 등록·MASH 처방·임상 의사결정 "
    "근거로 사용할 수 없습니다. 모든 데이터는 합성(synthetic)입니다."
)

SECTIONS = (
    ("ctg",   "ClinicalTrials.gov diff (synthetic)"),
    ("fda",   "FDA news diff (synthetic)"),
    ("ema",   "EMA EPAR (synthetic)"),
    ("pmda",  "PMDA news (synthetic)"),
    ("ir",    "Sponsor IR / SEC 8-K (synthetic)"),
    ("aasld", "AASLD The Liver Meeting 2026 abstracts (synthetic)"),
)


def _enrich(buckets: Dict[str, List[Dict[str, Any]]]) -> Dict[str, List[Dict[str, Any]]]:
    return {key: classify_bucket(events) for key, events in buckets.items()}


def _affected_drugs(event: Dict[str, Any]) -> List[str]:
    if event.get("drug"):
        return [event["drug"]]
    return list(event.get("drugs") or [])


def _has_any_event(buckets: Dict[str, List[Dict[str, Any]]]) -> bool:
    return any(len(v) > 0 for v in buckets.values())


def build_markdown(buckets: Dict[str, List[Dict[str, Any]]], snapshot_date: str) -> str:
    enriched = _enrich(buckets)
    lines: List[str] = []
    lines.append(f"# MASHTrialPulse Daily Digest — {snapshot_date}")
    lines.append("")
    lines.append(f"> Disclaimer: {DISCLAIMER_KO}")
    lines.append("")

    if not _has_any_event(enriched):
        lines.append("_오늘은 변화가 감지되지 않았습니다 (no diff events)._")
        lines.append("")
        return "\n".join(lines)

    for key, title in SECTIONS:
        events = enriched.get(key, [])
        if not events:
            continue
        lines.append(f"## {title}")
        lines.append("")
        for ev in events:
            tags = ", ".join(ev.get("tags", []))
            drugs = ", ".join(_affected_drugs(ev)) or "(약물 미상)"
            ko = summarize(ev, snapshot_date)
            id_str = ev.get("nct_id") or ev.get("id") or ""
            lines.append(f"- **[{tags}]** {ko}")
            lines.append(f"  - 영향 약물: {drugs}")
            if id_str:
                lines.append(f"  - id: {id_str}")
            if ev.get("detail"):
                lines.append(f"  - detail: {ev['detail']}")
        lines.append("")

    lines.append("---")
    lines.append("")
    lines.append("### 출처 (sources, all synthetic)")
    lines.append("")
    lines.append("- ClinicalTrials.gov API v2 (synthetic snapshot)")
    lines.append("- FDA NewsRoom (synthetic)")
    lines.append("- EMA EPAR (synthetic)")
    lines.append("- PMDA news (synthetic)")
    lines.append("- Sponsor IR pages + SEC EDGAR 8-K (synthetic)")
    lines.append("- AASLD The Liver Meeting 2026 abstracts (synthetic)")
    lines.append("")
    return "\n".join(lines)


def build_html(buckets: Dict[str, List[Dict[str, Any]]], snapshot_date: str) -> str:
    enriched = _enrich(buckets)
    parts: List[str] = []
    parts.append("<!doctype html><html lang='ko'><head><meta charset='utf-8'>")
    parts.append(f"<title>MASHTrialPulse {html.escape(snapshot_date)}</title>")
    parts.append("<style>"
                 "body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;"
                 "max-width:920px;margin:2em auto;padding:0 1em;color:#1f2937;}"
                 "h1{border-bottom:2px solid #0f766e;padding-bottom:.3em;}"
                 "h2{margin-top:1.6em;color:#0f766e;}"
                 ".disclaimer{background:#fef3c7;border-left:4px solid #d97706;"
                 "padding:.6em 1em;font-size:.92em;}"
                 ".event{border:1px solid #e5e7eb;border-radius:6px;padding:.6em .9em;"
                 "margin:.5em 0;background:#f9fafb;}"
                 ".tag{display:inline-block;background:#0f766e;color:#fff;border-radius:4px;"
                 "padding:.05em .55em;font-size:.78em;margin-right:.3em;}"
                 ".tag.safety{background:#b91c1c;} .tag.efficacy{background:#0f766e;}"
                 ".tag.regulatory{background:#1d4ed8;} .tag.pipeline{background:#7c3aed;}"
                 ".meta{color:#6b7280;font-size:.85em;margin-top:.3em;}"
                 "</style></head><body>")
    parts.append(f"<h1>MASHTrialPulse Daily Digest — {html.escape(snapshot_date)}</h1>")
    parts.append(f"<p class='disclaimer'>Disclaimer: {html.escape(DISCLAIMER_KO)}</p>")

    if not _has_any_event(enriched):
        parts.append("<p><em>오늘은 변화가 감지되지 않았습니다 (no diff events).</em></p>")
        parts.append("</body></html>")
        return "".join(parts)

    for key, title in SECTIONS:
        events = enriched.get(key, [])
        if not events:
            continue
        parts.append(f"<h2>{html.escape(title)}</h2>")
        for ev in events:
            tags = ev.get("tags", [])
            drugs = ", ".join(_affected_drugs(ev)) or "(약물 미상)"
            ko = summarize(ev, snapshot_date)
            id_str = ev.get("nct_id") or ev.get("id") or ""
            parts.append("<div class='event'>")
            for t in tags:
                parts.append(f"<span class='tag {html.escape(t)}'>{html.escape(t)}</span>")
            parts.append(f" {html.escape(ko)}")
            parts.append(
                f"<div class='meta'>영향 약물: {html.escape(drugs)}"
                + (f" &middot; id: {html.escape(id_str)}" if id_str else "")
                + (f" &middot; detail: {html.escape(ev.get('detail', ''))}" if ev.get("detail") else "")
                + "</div>"
            )
            parts.append("</div>")

    parts.append("<hr>")
    parts.append("<h3>출처 (sources, all synthetic)</h3><ul>")
    for s in (
        "ClinicalTrials.gov API v2 (synthetic snapshot)",
        "FDA NewsRoom (synthetic)",
        "EMA EPAR (synthetic)",
        "PMDA news (synthetic)",
        "Sponsor IR pages + SEC EDGAR 8-K (synthetic)",
        "AASLD The Liver Meeting 2026 abstracts (synthetic)",
    ):
        parts.append(f"<li>{html.escape(s)}</li>")
    parts.append("</ul></body></html>")
    return "".join(parts)
