#!/usr/bin/env python3
"""Wolfram Alpha cross-validation of AVM ISA arithmetic across all ports."""
import json, os, subprocess, sys
from pathlib import Path

WOLFRAM_KEY = os.environ.get("WOLFRAM_APP_ID", "")
SILVER = Path(__file__).resolve().parent.parent

def wolfram(query):
    """Query Wolfram Alpha and return result."""
    if not WOLFRAM_KEY:
        return None
    import urllib.request, urllib.parse
    params = urllib.parse.urlencode({"input": query, "appid": WOLFRAM_KEY, "format": "plaintext"})
    try:
        with urllib.request.urlopen(f"https://api.wolframalpha.com/v2/query?{params}", timeout=15) as r:
            data = r.read().decode()
            import re
            texts = re.findall(r'<plaintext>(.*?)</plaintext>', data, re.DOTALL)
            return ' '.join(t.strip() for t in texts if t.strip()) if texts else None
    except: return None

def verify_arithmetic():
    """Verify AVM arithmetic matches Wolfram Alpha ground truth."""
    cases = [
        ("INT32_MAX", 2147483647, "2147483647"),
        ("Q16 scale (2^16)", 65536, "65536"),
        ("neg(-2147483647)", "-(-2147483647)", "2147483647"),
        ("floor(-1.666)", "floor(-1.666)", "-2"),
    ]
    results = []
    for name, py_val, wa_expected in cases:
        wa_result = wolfram(name) if isinstance(py_val, str) else wolfram(str(py_val))
        match = wa_result and wa_expected in wa_result
        results.append({"property": name, "python": py_val, "wolfram": wa_result, "match": match})
    return results

def run_port_tests():
    """Run ALL AVM port test harnesses."""
    import shutil
    results = {}

    # Each port: (name, [cmd, args...], working_dir_relative, env_add)
    ports = [
        ("Python", [sys.executable, "tests/test_avm_python.py"], ".", {"PYTHONPATH": str(SILVER / "python")}),
        ("Rust", ["cargo", "test"], "rust", {}),
        ("Go", ["go", "test", "-v", "./..."], "go", {}),
        ("C", ["sh", "-c", "gcc -o /tmp/c_avm_test c/test_avm.c -lm && /tmp/c_avm_test"], ".", {}),
        ("C++", ["sh", "-c", "g++ -std=c++20 -o /tmp/cpp_avm_test cpp/test_avm.cpp && /tmp/cpp_avm_test"], ".", {}),
        ("Julia", ["julia", "julia/AVMIsa/test_avm.jl"], ".", {}),
        ("R", ["Rscript", "r/AVMIsa/test_avm.r"], ".", {}),
        ("Octave", ["octave", "--no-gui", "--eval", "addpath('octave'); test_avm"], ".", {}),
    ]

    for name, cmd, wd, env_add in ports:
        exe = cmd[0]
        # Look in PATH that includes common Nix/cargo locations
        search_path = os.environ.get("PATH", "") + ":/home/allaun/.nix-profile/bin:/home/allaun/.cargo/bin:/usr/local/go/bin"
        exe_path = shutil.which(exe.split()[0] if '/' in exe else exe, path=search_path)
        if not exe_path:
            results[name] = {"passed": False, "skipped": True, "reason": f"{exe} not found"}
            continue
        try:
            env = {**os.environ, **env_add,
                   "PATH": os.environ.get("PATH", "") + ":/home/allaun/.nix-profile/bin:/home/allaun/.cargo/bin:/usr/local/go/bin"}
            r = subprocess.run(cmd, capture_output=True, text=True, timeout=120, cwd=SILVER / wd, env=env)
            ok = r.returncode == 0 and "❌" not in r.stdout
            results[name] = {"passed": ok, "output": r.stdout[-300:]}
        except Exception as e:
            results[name] = {"passed": False, "error": str(e)[:200]}
    return results

def main():
    report = {
        "schema": "avm_port_verification_v1",
        "wolfram_arithmetic": verify_arithmetic(),
        "port_tests": run_port_tests(),
    }

    # Write receipt
    out = SILVER / "signatures" / "avm_verification_receipt.json"
    out.write_text(json.dumps(report, indent=2))
    print(f"Receipt written to {out}")

    # Summary
    print("\n=== Wolfram Verification ===")
    for c in report["wolfram_arithmetic"]:
        print(f"  {'✅' if c['match'] else '❌'} {c['property']}: matched={c['match']}")

    print("\n=== Port Tests ===")
    for name, r in report["port_tests"].items():
        status = "✅" if r.get("passed") else "❌"
        print(f"  {status} {name}: {'pass' if r.get('passed') else 'fail'}")

    success = all(c["match"] for c in report["wolfram_arithmetic"] if c["wolfram"])
    return 0 if success else 1

if __name__ == "__main__":
    sys.exit(main())
