import json
from pathlib import Path

folder = Path(__file__).parent
load = lambda name: json.loads((folder / f"{name}.json").read_text())
b, c, red_b, red_c = (load(x) for x in ("green-B", "green-C", "baseline-B", "red-C"))
money_paths = {"balance_list": ["data", "data", 0, "money"],
               "balance_detail": ["data", "money"],
               "cashout_list": ["data", "data", 0, "money"]}

def at(value, path):
    for key in path:
        value = value[key]
    return value

for route, path in money_paths.items():
    assert at(red_b["routes"][route]["shape"], path) == "string"
    assert at(red_c["routes"][route]["shape"], path) == "number"
    assert at(c["routes"][route]["shape"], path) == "string"

for route, left in b["routes"].items():
    right = c["routes"][route]
    assert left["json_valid"] and right["json_valid"]
    assert left["status"] == right["status"]
    assert left["code"] == right["code"]
    assert left["shape"] == right["shape"], route
    assert left["response_sha256"] == red_b["routes"][route]["response_sha256"], route
    if route != "balance_detail_missing":
        assert left["source_match"] and right["source_match"], route
    if route.endswith("list"):
        assert left["response_rows"] > 0 and right["response_rows"] > 0
        assert left["ordered_desc"] and right["ordered_desc"]
for lane in (b, c):
    assert lane["failure"] == "" and lane["source_unchanged"]
    assert lane["original_debug"] is False
    assert lane["database_writes"] == 0
    for table in ("user_balance_log", "cashout_order"):
        assert lane["preflight"][table]["money_precision_scale"] == [10, 2]
assert b["source_before"] == red_b["source_before"]
assert c["source_before"] == red_c["source_before"]
print(json.dumps({"interfaces": 3, "scenarios": 4,
                  "money_type_differences_red": 3, "money_type_differences_green": 0,
                  "mysql_complete_response_hashes_unchanged": 4,
                  "individual_source_and_order_checks": True,
                  "natural_business_values_cross_database_equal_claimed": False}))
