"""Local answer check for an original synthetic case. Python 3.10+, standard library.
This checks the supplied case, not a changed dataset, employer assessment or hiring readiness.
No network calls. Edit submission.json, then run: python check.py [submission.json]
"""
from pathlib import Path
import json, math, sys
EXPECTED = {'billed_total': 73800.0, 'cash_total': 69000.0, 'expected_revenue': 18200.0, 'booked_revenue': 21700.0, 'booked_overstatement': 3500.0, 'ending_contract_liability': 55600.0, 'ending_receivable': 4800.0}
def compare(actual, expected, path):
    if isinstance(expected,dict):
        if not isinstance(actual,dict):return [path+": enter an object with the named fields"]
        issues=[]
        for k,v in expected.items():issues+=compare(actual.get(k),v,path+"."+k)
        return issues
    if expected is None:
        return [] if actual == "unknown" else [path+": enter the explicit word unknown when the complete measure cannot be calculated"]
    if isinstance(expected,list):
        if not isinstance(actual,list) or len(actual)!=len(expected):return [path+": check the array length and order"]
        return sum((compare(a,e,path+f"[{i}]") for i,(a,e) in enumerate(zip(actual,expected))),[])
    if isinstance(expected,(int,float)):
        if isinstance(actual,bool) or not isinstance(actual,(int,float)) or not math.isfinite(actual):return [path+": enter a finite number, using decimals for rates"]
        tolerance=max(0.02,abs(expected)*0.00001) if abs(expected)>1 else 0.0001
        return [] if abs(actual-expected)<=tolerance else [path+": differs from the reference; check units, population and rounding"]
    return [] if actual==expected else [path+": check the record identity or label"]
def main():
    target=Path(sys.argv[1]) if len(sys.argv)>1 else Path(__file__).with_name("submission.json")
    try:
        result=json.loads(target.read_text(encoding="utf-8"))
        issues=compare(result,EXPECTED,"answer")
    except (OSError,ValueError,TypeError) as exc:
        print(f"Cannot check the file: {exc}",file=sys.stderr);return 2
    if issues:
        print("Some answers need another look. This is a learning check, not a grade.")
        print("\n".join(issues));return 1
    print("All supplied-case checkpoints match within the stated rounding tolerance.")
    print("Now check your memo, definitions and limits; matching numbers alone is not a complete project.")
    return 0
if __name__=="__main__":raise SystemExit(main())
