"""01 — connect to the broker, list resources, print them.

The smallest useful program: a 5-line consumer of the Observer role.
"""

from __future__ import annotations

from mgf.hrb import Client, MgfHrbError


SOCKET = "/tmp/mgf-hrb.sock"


def main() -> int:
    try:
        with Client.connect(SOCKET) as client:
            urns = client.list("hw:/**")
    except FileNotFoundError:
        print(f"No broker is listening at {SOCKET}.")
        print("Start one: cd hrb-code && zig build && ./zig-out/bin/mgf-hrb-server")
        return 2
    except MgfHrbError as exc:
        print(f"Broker error: {exc}")
        return 1

    print(f"Broker reported {len(urns)} resource(s):")
    for urn in sorted(urns):
        print(f"  {urn}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
