#!/usr/bin/env python3
"""Standing order for AI Agents Archive. Stdlib only. Does not unlock unless you pass --buy ID."""
from __future__ import annotations

import hashlib
import json
import sys
import urllib.request

SITE = "https://aiagentsarchive.com"
AGENT = "did:aaa:" + "0" * 32


def get(path: str) -> object:
    req = urllib.request.Request(
        SITE + path,
        headers={"Accept": "application/json", "X-AGENT-ID": AGENT},
    )
    with urllib.request.urlopen(req, timeout=20) as res:
        return json.load(res)


def main() -> None:
    status = get("/api/status")
    print("rail", status.get("label"), status.get("stage"), status.get("network"))
    example = get("/api/example")
    print("example", example.get("id"), example.get("title"))
    catalog = get("/api/discover?sort=savings&limit=5")
    items = catalog.get("items") or []
    if not items:
        print("empty catalog")
        return
    hit = items[0]
    print("top", hit.get("id"), hit.get("title"))
    print("unlock", hit.get("priceUsd"), "reconstruct", hit.get("reconstructUsd"))
    preview = get("/api/handoffs/" + hit["id"])
    print("hash", preview.get("hash"), "chars", preview.get("bodyChars"))
    print("next: POST /api/handoffs/%s/unlock then GET .../body and sha256-match hash" % hit["id"])
    if len(sys.argv) > 2 and sys.argv[1] == "--buy":
        raise SystemExit("Pass real agent id and X-PAYMENT yourself. This script stays read-only.")


if __name__ == "__main__":
    main()
