#!/usr/bin/env python3
"""
Quick smoke test for HelixBackplane.

Requires:
  - Helix64 backend running on :5065
  - helix_cdc on PYTHONPATH (same as your Makefile targets)

Usage:
  cd /home/voidstr3m33/helix-cdc
  PYTHONPATH=. python3 tests/test_helix_backplane_smoke.py
"""

import json
import os
from pathlib import Path

from helix_cdc.bloomos.backplane import HelixBackplane


def pretty(result):
    print(json.dumps(result, indent=2, sort_keys=True))


def run():
    bp = HelixBackplane()

    cases = [
        ("math", "Solve 2x + 5 = 13"),
        ("code", "Write a Python function that reverses a list"),
        ("json", "Format this data as JSON"),
    ]

    print("\n🧬 HelixBackplane Smoke Test\n")

    for task_type, desc in cases:
        print("=" * 70)
        print(f"TASK TYPE: {task_type}")
        print(f"DESC     : {desc}")
        print("-" * 70)

        res = bp.run_task(task_type=task_type, description=desc)
        gate = res["gate"]
        meta = res["meta"]

        print(f"Gate policy   : {gate['policy']}")
        print(f"Codon         : {gate['codon']}")
        print(f"Program before: {gate['program_before']}")
        print(f"Program after : {gate['program_after']}")
        print(f"Expected Δ    : {gate['expected_effects']}")
        print(f"Backend       : {meta['backend']}")
        print(f"Elapsed (ms)  : {meta['elapsed_ms']}")
        print(f"Used fallback : {meta['used_fallback']}")
        if meta.get("backend_error"):
            print(f"Backend error : {meta['backend_error']}")
        print(f"Anchors       : {res['anchors_resolved']}")
        print(f"Mean logits   : {res['logits_mean']:.4f}")
        print(f"Std logits    : {res['logits_std']:.4f}")
        print()

    # Optional: write a small receipt-like summary
    out = {
        "backend": os.getenv("BLOOMOS_BACKEND", "helix64"),
        "cases": [
            {
                "task_type": t,
                "description": d,
            }
            for (t, d) in cases
        ],
    }
    Path("helix_backplane_smoke.json").write_text(json.dumps(out, indent=2))
    print("=" * 70)
    print("✅ Smoke test complete!")
    print(f"Wrote helix_backplane_smoke.json")
    print("=" * 70)


if __name__ == "__main__":
    run()
