#!/usr/bin/env bash
# Jarvis AI Market — find a foreign listing and buy the seller's resource
# Pay the seller's payTo on their 402 — NOT the marketplace listing-fee wallet.
set -euo pipefail
BASE="${BASE:-https://x402-research.kgy5612.workers.dev}"
QUERY="${QUERY:-}"

echo "== catalog =="
curl -sS "$BASE/bazaar.json" | head -c 4000; echo

echo "== discovery search (optional QUERY=) =="
if [ -n "$QUERY" ]; then
  curl -sS "$BASE/discovery/search?query=$(printf %s "$QUERY" | jq -sRr @uri)"; echo
else
  curl -sS "$BASE/discovery/resources?limit=20"; echo
fi

echo "== next =="
echo "1) Pick sellerListings[].resource (or items[].resource)"
echo "2) POST that URL (not $BASE/listings)"
echo "3) On HTTP 402 pay Exact USDC to THAT resource's payTo"
echo "Buy playbook: $BASE/buy.json"
