#!/usr/bin/env python3
"""NONCE admission probe.

Prints a DETERMINISTIC state fingerprint for cron change-detection:
  admission=<open|closed>
  mintPaused=<true|false>
  benchmark=<version>

Deliberately excludes block numbers, timestamps, remainingSupply and any other
per-tick-varying field -- the cron `monitor` gate compares this output verbatim
between ticks, so a moving value would wake the agent every single run.

Exit code is always 0; failures print a distinct line so 'unreachable' is not
mistaken for 'closed'.
"""
import json
import sys
import urllib.request

MCP = "https://mcp.nonceprotocol.xyz/mcp"
RPC = "https://bsc-dataseed1.defibit.io"
FREE_MINT = "0x7538ba8C1A82E9706dB6d2463C15f69ADc01a17e"
PAUSED_SELECTOR = "0x5c975abb"  # keccak256("paused()")[:4]


def mcp_call(name, args):
    body = json.dumps({
        "jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {"name": name, "arguments": args},
    }).encode()
    req = urllib.request.Request(MCP, data=body, headers={
        "Content-Type": "application/json",
        "Accept": "application/json, text/event-stream",
        # Cloudflare in front of mcp.nonceprotocol.xyz resets the connection for
        # Python-urllib's default UA; a curl-like UA is required to get a response.
        "User-Agent": "curl/8.7.1",
    })
    with urllib.request.urlopen(req, timeout=25) as r:
        payload = json.loads(r.read().decode())
    if "error" in payload:
        raise RuntimeError(f"jsonrpc error: {payload['error']}")
    return json.loads(payload["result"]["content"][0]["text"])


def eth_call(to, data):
    body = json.dumps({
        "jsonrpc": "2.0", "id": 1, "method": "eth_call",
        "params": [{"to": to, "data": data}, "latest"],
    }).encode()
    req = urllib.request.Request(RPC, data=body,
                                 headers={"Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=25) as r:
        return json.loads(r.read().decode())["result"]


def main():
    try:
        info = mcp_call("protocol_info", {})
        print(f"admission={info.get('admission')}")
        print(f"benchmark={info.get('benchmark', {}).get('version')}")
    except Exception as e:
        print(f"admission=UNREACHABLE({type(e).__name__})")
        print("benchmark=UNREACHABLE")
    try:
        raw = eth_call(FREE_MINT, PAUSED_SELECTOR)
        print(f"mintPaused={'true' if int(raw, 16) == 1 else 'false'}")
    except Exception as e:
        print(f"mintPaused=UNREACHABLE({type(e).__name__})")
    return 0


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