"""Daily digest builder — HTML and Markdown.

Groups ranked records by primary drug class section.
Each entry shows tags, novelty score, source link (id only — no live URL fetch),
and the safety disclaimer is included once at the top.
"""

import html
from datetime import datetime


DISCLAIMER = (
    "본 도구는 연구·교육 목적의 참고용이며, 임상 의사결정 근거로 사용할 수 없습니다. "
    "(This tool is for research and educational reference only and must not be used as a basis for clinical decisions.)"
)


def _primary_drug_class(record):
    classes = (record.get("tags") or {}).get("drug_classes") or []
    if classes:
        return classes[0]
    return "Unclassified"


def _group_by_drug(records):
    groups = {}
    for r in records:
        key = _primary_drug_class(r)
        groups.setdefault(key, []).append(r)
    # Stable order: known classes first, Unclassified last.
    order = ["SGLT2i", "MRA", "GLP-1RA", "dual-GIP-GLP1", "Unclassified"]
    return [(k, groups[k]) for k in order if k in groups] + [
        (k, v) for k, v in groups.items() if k not in order
    ]


# ----- Markdown -----

def _md_record(r):
    tags = r.get("tags") or {}
    drugs = ", ".join(tags.get("drug_classes") or []) or "—"
    outs = ", ".join(tags.get("outcomes") or []) or "—"
    phenos = ", ".join(tags.get("phenotypes") or []) or "—"
    ident = r.get("pmid") or r.get("nct_id") or r.get("doi") or "—"
    title = r.get("title") or "(no title)"
    src = r.get("source") or "?"
    novelty = r.get("novelty", 0.0)
    abstract = (r.get("abstract") or "").strip()
    if len(abstract) > 400:
        abstract = abstract[:400].rstrip() + " …"
    return (
        f"### {title}\n\n"
        f"- **Source / ID**: {src} · `{ident}`\n"
        f"- **Drug class**: {drugs}\n"
        f"- **Outcomes**: {outs}\n"
        f"- **Phenotype**: {phenos}\n"
        f"- **Novelty score**: `{novelty}`\n\n"
        f"> {abstract}\n"
    )


def render_markdown(records, run_date=None, top_n=None):
    run_date = run_date or datetime.utcnow().strftime("%Y-%m-%d")
    lines = []
    lines.append(f"# DKDPulse Daily Digest — {run_date}")
    lines.append("")
    lines.append(f"_{DISCLAIMER}_")
    lines.append("")
    lines.append(f"Total ranked: **{len(records)}**" + (f" (top {top_n})" if top_n else ""))
    lines.append("")
    if not records:
        lines.append("_(No records to display.)_")
        return "\n".join(lines)

    for cls, items in _group_by_drug(records):
        lines.append(f"## {cls}  ({len(items)})")
        lines.append("")
        for r in items:
            lines.append(_md_record(r))
            lines.append("")
    lines.append("---")
    lines.append("")
    lines.append(f"_{DISCLAIMER}_")
    return "\n".join(lines)


# ----- HTML -----

def _html_escape(s):
    return html.escape(str(s) if s is not None else "")


def _html_record_row(r):
    tags = r.get("tags") or {}
    drugs = ", ".join(tags.get("drug_classes") or []) or "—"
    outs = ", ".join(tags.get("outcomes") or []) or "—"
    phenos = ", ".join(tags.get("phenotypes") or []) or "—"
    ident = r.get("pmid") or r.get("nct_id") or r.get("doi") or "—"
    title = r.get("title") or "(no title)"
    src = r.get("source") or "?"
    novelty = r.get("novelty", 0.0)
    abstract = (r.get("abstract") or "").strip()
    if len(abstract) > 500:
        abstract = abstract[:500].rstrip() + " …"
    return (
        "<tr>"
        f"<td class='novelty'>{_html_escape(novelty)}</td>"
        f"<td class='title'><strong>{_html_escape(title)}</strong>"
        f"<div class='abstract'>{_html_escape(abstract)}</div></td>"
        f"<td>{_html_escape(src)}<br><code>{_html_escape(ident)}</code></td>"
        f"<td>{_html_escape(drugs)}</td>"
        f"<td>{_html_escape(outs)}</td>"
        f"<td>{_html_escape(phenos)}</td>"
        "</tr>"
    )


def render_html(records, run_date=None, top_n=None):
    run_date = run_date or datetime.utcnow().strftime("%Y-%m-%d")
    style = (
        "body{font-family:-apple-system,Segoe UI,Roboto,sans-serif;max-width:1200px;margin:24px auto;padding:0 16px;color:#1a1a1a;}"
        "h1{border-bottom:2px solid #0a4d8c;padding-bottom:8px;}"
        "h2{margin-top:32px;color:#0a4d8c;}"
        "table{border-collapse:collapse;width:100%;margin-top:8px;font-size:14px;}"
        "th,td{border:1px solid #ccc;padding:8px;vertical-align:top;text-align:left;}"
        "th{background:#f0f4f8;}"
        "td.novelty{font-family:monospace;text-align:right;width:64px;}"
        "td.title{max-width:420px;}"
        ".abstract{font-size:12px;color:#444;margin-top:6px;}"
        ".disclaimer{background:#fff8e1;border-left:4px solid #f0a818;padding:10px 14px;margin:16px 0;font-size:13px;}"
        "code{background:#f6f8fa;padding:1px 4px;border-radius:3px;font-size:12px;}"
    )

    parts = []
    parts.append("<!doctype html>")
    parts.append("<html lang='en'><head><meta charset='utf-8'>")
    parts.append(f"<title>DKDPulse Digest — {_html_escape(run_date)}</title>")
    parts.append(f"<style>{style}</style></head><body>")
    parts.append(f"<h1>DKDPulse Daily Digest — {_html_escape(run_date)}</h1>")
    parts.append(f"<div class='disclaimer'>{_html_escape(DISCLAIMER)}</div>")
    parts.append(f"<p>Total ranked: <strong>{len(records)}</strong>"
                 + (f" (top {top_n})" if top_n else "") + "</p>")

    if not records:
        parts.append("<p><em>(No records to display.)</em></p>")
    else:
        for cls, items in _group_by_drug(records):
            parts.append(f"<h2>{_html_escape(cls)} <small>({len(items)})</small></h2>")
            parts.append("<table>")
            parts.append("<thead><tr>"
                         "<th>Novelty</th><th>Title / Abstract</th><th>Source / ID</th>"
                         "<th>Drug class</th><th>Outcomes</th><th>Phenotype</th>"
                         "</tr></thead><tbody>")
            for r in items:
                parts.append(_html_record_row(r))
            parts.append("</tbody></table>")

    parts.append(f"<div class='disclaimer'>{_html_escape(DISCLAIMER)}</div>")
    parts.append("</body></html>")
    return "\n".join(parts)
