"""Tests against the Inland Revenue Department's own published figures.

1. Worked examples A-E from IRD's "Brief Guide to Taxes" (2024/25 examples).
2. Examples 1-2 from IRD's "Joint Assessment & Personal Assessment for Married Couples" (2025/26).
3. GovHK's table of income levels at which taxpayers reach the standard-rate zone
   (2024/25-2025/26 and 2026/27 columns): at each level progressive tax must equal
   standard-rate tax.
Run: python tests.py   (writes results.json used by the web page)
"""
import json
from model import *

results = []


def check(source, name, got, want, tol=0.5):
    ok = abs(got - want) <= tol
    results.append(dict(source=source, name=name, got=round(got, 2), want=want, ok=ok))
    print(("PASS " if ok else "FAIL ") + f"{source} | {name}: got {got:,.2f} want {want:,}")


G = "IRD Brief Guide 2024/25"
# Example A: single-income family, quarters provided by employer.
husband = Person(income=750_000, outgoings=3_000, provided_quarters=True, rent_to_employer=5_000,
                 donations=4_000, mpf=18_000)
cA = Couple(husband, Person(), Dependants(children=2, parents60=1))
check(G, "Example A: value of quarters", husband.rental_value(), 69_700)
check(G, "Example A: tax payable", sum(separate(cA, "a", "a", "a", "2024/25")), 18_019)
# Example B: same family, taxed at standard rate (two tiers).
cB = Couple(Person(income=7_000_000, outgoings=4_000, donations=5_000, mpf=18_000), Person(),
            Dependants(children=2, parents60=1))
check(G, "Example B: tax payable (standard rate, two tiers)", sum(separate(cB, "a", "a", "a", "2024/25")), 1_064_180)
# Example C: both spouses earn; separate taxation.
cC = Couple(Person(income=668_000, mpf=0), Person(income=350_000, mpf=0), Dependants(children=2))
tA, tB = separate(cC, "a", "a", "a", "2024/25")
check(G, "Example C: Mr A", tA, 27_420)
check(G, "Example C: Mrs A", tB, 17_560)
# Example D: one spouse earns less than the basic allowance; joint assessment.
cD = Couple(Person(income=673_000, mpf=0), Person(income=68_000, mpf=0), Dependants(children=2))
check(G, "Example D: separate total", sum(separate(cD, "a", "a", "a", "2024/25")), 28_270)
check(G, "Example D: joint", joint(cD, "2024/25"), 17_390)
# Example E: who should claim the children when one spouse is on the standard rate.
cE = Couple(Person(income=4_508_000, mpf=0), Person(income=448_000, mpf=0), Dependants(children=2))
check(G, "Example E: children to Mr C", sum(separate(cE, "a", "a", "a", "2024/25")), 708_920)
check(G, "Example E: children to Mrs C", sum(separate(cE, "b", "b", "b", "2024/25")), 674_700)

J = "IRD Joint Assessment leaflet 2025/26"
c1 = Couple(Person(income=540_000), Person(income=120_000), Dependants(children=1))
check(J, "Example 1: separate total", sum(separate(c1, "a", "a", "a", "2025/26")), 23_200)
check(J, "Example 1: joint", joint(c1, "2025/26"), 20_140)
c2 = Couple(Person(income=540_000), Person(income=420_000), Dependants(children=1))
check(J, "Example 2: separate total", sum(separate(c2, "a", "a", "a", "2025/26")), 48_100)
check(J, "Example 2: joint", joint(c2, "2025/26"), 69_100)

S = "GovHK standard-rate zone table"
zones = {
    "2025/26": {"Single": (2_022_000, 132_000), "Married": (3_144_000, 264_000),
                "Married + 1 child": (4_249_000, 394_000), "Married + 2 children": (5_708_000, 524_000),
                "Married + 3 children": (7_918_000, 654_000),
                "Single + 2 parents 60+": (2_872_000, 232_000),
                "Married + 3 children + 2 parents living with you": (11_318_000, 854_000),
                "Single + 1 parent living with you + disabled sibling": (3_828_250, 344_500)},
    "2026/27": {"Single": (2_132_500, 145_000), "Married": (3_365_000, 290_000),
                "Married + 1 child": (4_555_000, 430_000), "Married + 2 children": (6_490_000, 570_000),
                "Married + 3 children": (8_870_000, 710_000),
                "Single + 2 parents 60+": (3_067_500, 255_000),
                "Married + 3 children + 2 parents living with you": (12_610_000, 930_000),
                "Single + 1 parent living with you + disabled sibling": (4_023_750, 367_500)},
}
for yr, rows in zones.items():
    for label, (income, allow) in rows.items():
        check(S, f"{yr} {label}: progressive = standard at {income:,}",
              progressive(income - allow), standard(income), tol=1)
# The 2026/27 allowance sums above are rebuilt from PAM 61(e) amounts:
y = YEARS["2026/27"]
check(S, "2026/27 married + 3 children + 2 parents living with you: allowance sum",
      y["married"] + 3 * y["child"] + 4 * y["parent60"], 930_000)

passed = sum(r["ok"] for r in results)
print(f"\n{passed}/{len(results)} passed")
json.dump(results, open("results_tests.json", "w"), indent=1)
