#!/usr/bin/env python3
"""Verify displayed aggregate arithmetic for PPA-20260831."""
import csv
from decimal import Decimal, ROUND_HALF_UP
from pathlib import Path


def rounded(value: Decimal, places: str) -> Decimal:
    return value.quantize(Decimal(places), rounding=ROUND_HALF_UP)


def main() -> int:
    path = Path(__file__).with_name("desk-sample-PPA-20260831.csv")
    with path.open(newline="", encoding="utf-8") as handle:
        rows = {row["metric"]: row for row in csv.DictReader(handle)}

    cac = Decimal(rows["covered_cac_per_joiner"]["value"])
    contribution = Decimal(rows["monthly_contribution_per_joiner"]["value"])
    covered = Decimal(rows["covered_joiners"]["value"])
    eligible = Decimal(rows["eligible_joiners"]["value"])

    payback = cac / contribution
    coverage = covered / eligible * Decimal("100")

    assert rounded(payback, "0.1") == Decimal("13.7")
    assert rows["stable_monthly_payback"]["value"] == "13.7"
    assert rounded(coverage, "0.1") == Decimal("84.7")
    assert rounded(coverage, "1") == Decimal("85")
    assert rows["account_coverage_exact"]["value"] == "84.7"
    assert rows["account_coverage_display"]["value"] == "85"
    assert rows["unmatched_spend"]["denominator_or_formula"].startswith(
        "unmatched paid-media dollars"
    )

    print("OK: 520/38 -> 13.7; 210/248 -> 84.7% -> 85%; dollar denominator distinct.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
