"""Join four country risk lists into data/countries.csv. Standard library only.

Inputs in src/.sources/ (git-ignored; see "About the data" in README.md):
  oecd_country_risk.csv  - OECD Country Risk Classification as built by our
                           pipeline (datopian/mizuho, data/country_risk_classification)
  wb_countries.json      - World Bank API, /v2/country (income groups)
The FATF and EU lists are short and typed in below from the official texts,
with the publication each comes from.

    python3 blog/country-risk-lists/src/prepare_data.py
"""

import csv
import json
from collections import Counter
from pathlib import Path

HERE = Path(__file__).resolve().parent
SRC = HERE / ".sources"
DATA = HERE.parent / "data"

# FATF (October 2026), "High-Risk Jurisdictions subject to a Call for Action" and
# "Jurisdictions under Increased Monitoring", Paris, 30 October 2026.
FATF_CALL_FOR_ACTION = {"PRK", "IRN", "MMR"}
FATF_INCREASED_MONITORING = {
    "AGO", "BOL", "BIH", "CMR", "HTI", "IRQ", "KEN", "KWT", "LAO", "LBN",
    "NPL", "PNG", "SSD", "SYR", "VEN", "VNM", "VGB", "YEM",
}
FATF_REMOVED_OCT_2026 = {"BGR", "CIV", "COD", "MCO"}

# Commission Delegated Regulation (EU) 2016/1675, Annex, consolidated version of
# 29 January 2026 (EUR-Lex CELEX 02016R1675-20260129).
EU_HIGH_RISK = {
    # Section I: action plan with the FATF
    "AFG", "DZA", "AGO", "BOL", "VGB", "CMR", "CIV", "COD", "HTI", "KEN", "LAO",
    "LBN", "MCO", "MMR", "NAM", "NPL", "SSD", "SYR", "TTO", "VUT", "VEN", "VNM", "YEM",
    # Section II and III: FATF public statement
    "IRN", "PRK",
    # Section IV: FATF membership suspended
    "RUS",
}

OECD_STATUS_NOT_CLASSIFIED = "not classified (high-income OECD, euro area or euro micro-state)"
OECD_STATUS_NOT_REVIEWED = "not reviewed or classified"
OECD_STATUS_NOT_COVERED = "not covered by the OECD"


def main() -> None:
    DATA.mkdir(exist_ok=True)
    with open(SRC / "oecd_country_risk.csv", encoding="utf-8") as f:
        oecd = list(csv.DictReader(f))
    wb = json.loads((SRC / "wb_countries.json").read_text())[1]
    income = {c["id"]: c["incomeLevel"]["value"] for c in wb if c["region"]["value"] != "Aggregates"}

    rows = []
    for r in oecd:
        cc = r["Alpha_3_Country_code"]
        cat = r["Risk_Classification"]
        notes = r["Notes"]
        if cat != "":
            oecd_status = "classified"
        elif "Not covered" in notes:
            oecd_status = OECD_STATUS_NOT_COVERED
        elif notes.strip() == "Currently not reviewed or classified.":
            oecd_status = OECD_STATUS_NOT_REVIEWED
        else:
            oecd_status = OECD_STATUS_NOT_CLASSIFIED
        if cc in FATF_CALL_FOR_ACTION:
            fatf = "call for action"
        elif cc in FATF_INCREASED_MONITORING:
            fatf = "increased monitoring"
        elif cc in FATF_REMOVED_OCT_2026:
            fatf = "removed October 2026"
        else:
            fatf = ""
        rows.append({
            "iso3": cc,
            "country": r["Country_Name"],
            "oecd_risk_category": cat,
            "oecd_previous_category": r["Risk_Classification_Previous"],
            "oecd_status": oecd_status,
            "oecd_member": r["OECD_Member"],
            "oecd_as_of": r["As_Of_Date"],
            "fatf_list": fatf,
            "eu_high_risk": "yes" if cc in EU_HIGH_RISK else "",
            "world_bank_income": income.get(cc, ""),
        })

    known = {r["iso3"] for r in rows}
    for name, codes in [("FATF call", FATF_CALL_FOR_ACTION), ("FATF grey", FATF_INCREASED_MONITORING),
                        ("FATF removed", FATF_REMOVED_OCT_2026), ("EU", EU_HIGH_RISK)]:
        missing = codes - known
        if missing:
            raise SystemExit(f"{name}: codes not in the OECD country list: {sorted(missing)}")

    with open(DATA / "countries.csv", "w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=list(rows[0]), lineterminator="\n")
        w.writeheader()
        w.writerows(rows)

    flagged = [r for r in rows if r["fatf_list"] in ("call for action", "increased monitoring") or r["eu_high_risk"]]
    print("countries", len(rows), "as of", Counter(r["oecd_as_of"] for r in rows).most_common(2))
    print("oecd status", Counter(r["oecd_status"] for r in rows))
    print("oecd categories", sorted(Counter(r["oecd_risk_category"] for r in rows if r["oecd_risk_category"]).items()))
    print("changed vs previous", [(r["iso3"], r["oecd_previous_category"], r["oecd_risk_category"])
                                  for r in rows if r["oecd_risk_category"] != r["oecd_previous_category"]])
    print("fatf", Counter(r["fatf_list"] for r in rows if r["fatf_list"]), "eu", sum(1 for r in rows if r["eu_high_risk"]))
    print("on FATF list or EU list:", len(flagged))
    both = [r["iso3"] for r in flagged if r["eu_high_risk"] and r["fatf_list"] in ("call for action", "increased monitoring")]
    eu_only = [r["iso3"] for r in flagged if r["eu_high_risk"] and r["fatf_list"] not in ("call for action", "increased monitoring")]
    fatf_only = [r["iso3"] for r in flagged if not r["eu_high_risk"]]
    print("both", len(both), both); print("eu only", eu_only); print("fatf only", fatf_only)
    print("income of flagged", Counter(r["world_bank_income"] or "n/a" for r in flagged))
    print("oecd of flagged", sorted(Counter(r["oecd_risk_category"] or "-" for r in flagged).items()))
    print("wb income all", Counter(r["world_bank_income"] or "not in WB list" for r in rows))


if __name__ == "__main__":
    main()
