"""LT candidacy & KONOS alignment (Korean Network for Organ Sharing).

References (built-in, offline):
- OPTN MELD 3.0 (2023-07 adoption)
- KONOS LT allocation policy
- KLTS LT guidelines
- AASLD/EASL HCC criteria (Milan, UCSF)

For research / QI use only.
"""
from __future__ import annotations
from typing import Dict, Optional


def lt_candidate(meld3_score: float, child_class: str, aclf_grade: str,
                 age: int, hcc: bool = False,
                 hcc_meets_milan: bool = False) -> Dict[str, object]:
    """Return waitlist eligibility, urgency, and reasons."""
    reasons = []
    eligible = False
    urgency = "none"

    if meld3_score >= 15:
        eligible = True
        reasons.append(f"MELD 3.0 = {meld3_score:.1f} >= 15")
    if child_class in ("B", "C"):
        eligible = True
        reasons.append(f"Child-Pugh {child_class}")
    if aclf_grade in ("ACLF-2", "ACLF-3"):
        eligible = True
        urgency = "high"
        reasons.append(f"{aclf_grade} (high-urgency)")
    elif aclf_grade == "ACLF-1":
        urgency = "intermediate"
        eligible = True
        reasons.append("ACLF-1")

    if hcc and hcc_meets_milan:
        eligible = True
        reasons.append("HCC within Milan criteria (MELD exception)")

    if age > 75:
        reasons.append(f"age {age} (relative contraindication)")

    if meld3_score >= 35 or aclf_grade == "ACLF-3":
        urgency = "high"
    elif meld3_score >= 25:
        urgency = "intermediate" if urgency == "none" else urgency

    return {
        "eligible": eligible,
        "urgency": urgency,
        "konos_priority_band": _konos_band(meld3_score, aclf_grade, hcc, hcc_meets_milan),
        "slk_flag": False,  # placeholder, see slk_candidate()
        "reasons": reasons,
    }


def slk_candidate(meld3_score: float, creatinine: float,
                  dialysis_weeks: float, ckd_stage: Optional[int]) -> bool:
    """Simultaneous Liver-Kidney transplant rough proxy.

    OPTN SLK criteria (research proxy):
    - sustained AKI: >=6 weeks dialysis OR creat>=4 for >=6 weeks
    - CKD with GFR<=60 for >=90 days AND (eGFR<=30 at listing or dialysis)
    """
    return dialysis_weeks >= 6 or creatinine >= 4.0 or (ckd_stage is not None and ckd_stage >= 4)


def _konos_band(meld3: float, aclf_grade: str, hcc: bool, milan: bool) -> str:
    """KONOS-style priority band (proxy)."""
    if aclf_grade == "ACLF-3" or meld3 >= 35:
        return "Status 1 / high-MELD"
    if hcc and milan:
        return "HCC exception"
    if meld3 >= 25:
        return "Band A"
    if meld3 >= 15:
        return "Band B"
    return "Below listing threshold"
