"""Audit Digital execution through Result references and configuration facts.

Counts alone are insufficient evidence. An expert consumer also checks program
hash, bit order, selected method, seed semantics, diagnostics, and whether a
full RunTrace is actually available on this standalone Digital path.
"""

from __future__ import annotations

import json

from cascaqit import Circuit, LocalBackend


def main() -> None:
    """Run one Digital Job and verify its cross-object references."""
    circuit = Circuit(2, program_id="lesson.digital.evidence")
    circuit.h(0).cx(0, 1).measure_all(key="readout")
    job = LocalBackend(seed=205).run(circuit, shots=32)
    result = job.result()
    config = result.metadata["simulation_execution_config"]

    payload = {
        "track": "digital_developer",
        "level": "expert",
        "lesson": "execution_evidence",
        "facts": {
            "job_state": job.status().state,
            "program_hash_matches": result.program_hash
            == circuit.to_ir().stable_hash(),
            "trace_available": "run_trace" in result.metadata,
            "reference_program_hash_matches": (
                result.metadata["references"]["program_hash"] == result.program_hash
            ),
            "method": config["method"],
            "seed": config["seed"],
            "bit_order": result.metadata["bitstring_ordering"]["qubit_order"],
            "diagnostic_codes": result.metadata["diagnostics_summary"]["codes"],
        },
        "boundaries": {
            "hardware_execution": False,
            "cloud_execution": False,
            "network_accessed": False,
            "credentials_loaded": False,
        },
    }
    print(json.dumps(payload, sort_keys=True))


if __name__ == "__main__":
    main()
