#!/usr/bin/env python3
# reproduce_moltmarket.py — anonymous GET-only reproducer for the
# moltmarket.store inventory (report #42). Prints the settlement
# breaker state, reserve staleness, revenue line, job/offer/agent
# census. Stdlib only.
import json, urllib.request

BASE = "https://moltmarket.store"

def get(path):
    req = urllib.request.Request(BASE + path, headers={"Accept": "application/json",
                                                       "User-Agent": "arion-research/1.0"})
    try:
        with urllib.request.urlopen(req, timeout=15) as r:
            return json.load(r)
    except urllib.error.HTTPError as e:
        # /health answers 503 while degraded but still serves the JSON body
        return json.loads(e.read())

h = get("/health")
s, f = h.get("safety", {}), h.get("financial", {})
print(f"safety.settlement      = {s.get('settlement')}")
print(f"safety.deposits        = {s.get('deposits')}   withdrawals = {s.get('withdrawals')}")
print(f"operating_mode         = {f.get('operating_mode')}")
print(f"circuit_breaker_open   = {f.get('circuit_breaker_open')}")
print(f"breaker_reason         = {f.get('circuit_breaker_reason')}")
rv = f.get("reserve", {})
print(f"reserve.fresh          = {rv.get('fresh')}   staleBySeconds = {rv.get('staleBySeconds')}   observedMicrounits = {rv.get('observedMicrounits')}")
print(f"arbitrators            = {f.get('arbitrator_pool_size')} of {f.get('required_launch_arbitrator_pool_size')} required")

rs = get("/revenue/stats")
print(f"revenue                = ${rs.get('revenue_usdc')} USDC over {rs.get('settlement_count')} settlements, last {rs.get('last_settled_at')} on {rs.get('network')}")

open_jobs = get("/jobs?status=open&limit=50")
all_jobs = get("/jobs?limit=100")
oj = open_jobs if isinstance(open_jobs, list) else open_jobs.get("jobs", [])
aj = all_jobs if isinstance(all_jobs, list) else all_jobs.get("jobs", [])
print(f"jobs                   = {len(oj)} open / {len(aj)} total")

offers = get("/offers?limit=50")
o = offers if isinstance(offers, list) else offers.get("offers", [])
prices = sorted({x.get("price_usdc") or x.get("price_cents") for x in o})
print(f"offers                 = {len(o)}, prices {prices[:3]} .. {prices[-3:] if len(prices)>3 else prices}")

agents = get("/agents?limit=100")
a = agents if isinstance(agents, list) else agents.get("agents", [])
def5 = sum(1 for x in a if x.get("rating") == 5 and x.get("completed_jobs") == 0)
print(f"agents                 = {len(a)}+ listed, {def5} with default rating=5 & completed_jobs=0")
