#!/usr/bin/env python3
"""Build finite original B oracle records. No DB and no scalar numeric JSON values."""
import json
import struct
import subprocess
from pathlib import Path

R = Path(__file__).resolve().parent
B = R.parent
old = json.loads(subprocess.check_output(["python3", str(B / "analyze-evidence.py")]))
half = json.loads(subprocess.check_output(["python3", str(B / "half-tie-execution-20261011/analyze-evidence.py")]))
cases = []
for p in old["lanes"]["B"]["records"]:
    lex = p["original_lexeme"]
    cases.append({"case_id": "old/"+p["case_id"]+"/"+p["column"], "context": "explicit_physical_FLOAT8_2",
                  "stored_double_hex": None if lex is None else struct.pack("<d", float(p["server_decimal30"])).hex(),
                  "stored_oracle_kind": "decimal30_projection_roundtrip_not_physical_bits",
                  "expected_getter_hex": p["native_double_hex"], "original_lexeme": lex})
for p in half["lanes"]["B"]["points"]:
    cases.append({"case_id": "half/"+p["case_id"], "context": "explicit_physical_FLOAT8_2",
                  "stored_double_hex": p["reads"]["computed_point"]["typed_rows"][0]["power"]["native_double_hex"],
                  "stored_oracle_kind": "actual_computed_native_double_bits",
                  "expected_getter_hex": p["reads"]["native_point"]["typed_rows"][0]["power"]["native_double_hex"],
                  "original_lexeme": p["original_lexeme"]})
computed = []
for p in half["lanes"]["B"]["points"]:
    t = p["reads"]["computed_point"]["typed_rows"][0]["power"]
    computed.append({"case_id": "computed/"+p["case_id"], "context": "computed_POWER",
                     "stored_double_hex": t["native_double_hex"], "expected_getter_hex": t["native_double_hex"]})
for p in half["lanes"]["B"]["aggregates"]:
    for column, t in p["reads"]["aggregate_native"]["typed_rows"][0].items():
        if t["php_type"] == "double":
            computed.append({"case_id": "aggregate/"+p["case_id"]+"/"+column, "context": "aggregate_"+column,
                             "stored_double_hex": t["native_double_hex"], "expected_getter_hex": t["native_double_hex"]})
print(json.dumps({"physical": cases, "computed": computed}, indent=2))
