#!/bin/bash
# x402-layer Skill Installer
# Usage: curl -fsSL https://api.x402layer.cc/skill/x402-layer/install | bash

set -e

SKILL_NAME="x402-layer"
INSTALL_DIR="${1:-./$SKILL_NAME}"

echo "Installing x402-layer skill..."
echo "Install directory: $INSTALL_DIR"

mkdir -p "$INSTALL_DIR/scripts"
mkdir -p "$INSTALL_DIR/references"

echo "Downloading SKILL.md..."
curl -fsSL "https://api.x402layer.cc/skill/x402-layer/SKILL.md" -o "$INSTALL_DIR/SKILL.md"

echo "Downloading requirements.txt..."
curl -fsSL "https://api.x402layer.cc/skill/x402-layer/requirements.txt" -o "$INSTALL_DIR/requirements.txt"

SCRIPTS=(
    "agentkit_support.py"
    "awal_bridge.py"
    "awal_cli.py"
    "check_credits.py"
    "consume_credits.py"
    "consume_product.py"
    "create_endpoint.py"
    "discover_marketplace.py"
    "erc8004_wallet_client.py"
    "list_agents.py"
    "list_my_endpoints.py"
    "list_on_marketplace.py"
    "manage_campaign.py"
    "manage_endpoint.py"
    "manage_webhook.py"
    "network_selection.py"
    "ows_cli.py"
    "pay_base.py"
    "pay_solana.py"
    "recharge_credits.py"
    "register_agent.py"
    "solana_signing.py"
    "submit_feedback.py"
    "support_auth.py"
    "support_threads.py"
    "topup_endpoint.py"
    "update_agent.py"
    "verify_webhook_payment.py"
    "wallet_signing.py"
    "xmtp_support.mjs"
)

echo "Downloading scripts..."
for script in "${SCRIPTS[@]}"; do
    curl -fsSL "https://api.x402layer.cc/skill/x402-layer/scripts/$script" -o "$INSTALL_DIR/scripts/$script"
    echo "  - $script"
done

REFERENCES=(
    "agent-registry-reputation.md"
    "agentic-endpoints.md"
    "agentkit-benefits.md"
    "credit-based.md"
    "marketplace.md"
    "mcp-control-plane.md"
    "openwallet-ows.md"
    "pay-per-request.md"
    "payment-signing.md"
    "payments-integration.md"
    "webhooks-verification.md"
    "xmtp-support.md"
)

echo "Downloading references..."
for ref in "${REFERENCES[@]}"; do
    curl -fsSL "https://api.x402layer.cc/skill/x402-layer/references/$ref" -o "$INSTALL_DIR/references/$ref"
    echo "  - $ref"
done

echo ""
echo "Installing Python dependencies..."
pip install -r "$INSTALL_DIR/requirements.txt" --quiet

echo ""
echo "Optional for XMTP support scripts:"
echo "  npm install @xmtp/node-sdk viem"

echo ""
echo "x402-layer skill installed successfully."
echo "Location: $INSTALL_DIR"
echo ""
echo "Next steps:"
echo "  1. For read-only discovery, no secrets are required"
echo "     python $INSTALL_DIR/scripts/discover_marketplace.py"
echo ""
echo "  2. For signing/payment flows, set wallet environment variables"
echo "     export X402_AUTH_MODE='private-key'"
echo "     export PRIVATE_KEY='0x...'"
echo "     export WALLET_ADDRESS='0x...'"
echo ""
echo "     # Or AWAL mode"
echo "     export X402_AUTH_MODE='awal'"
echo "     export X402_USE_AWAL=1"
echo ""
echo "  3. Optional OWS mode"
echo "     npm install -g @open-wallet-standard/core"
echo "     export OWS_WALLET='hackathon-wallet'"
echo "     python $INSTALL_DIR/scripts/ows_cli.py pay-url https://api.x402layer.cc/e/weather-data --wallet hackathon-wallet"
echo ""
echo "  4. Try it out"
echo "     python $INSTALL_DIR/scripts/pay_base.py https://api.x402layer.cc/e/weather-data"
echo ""
echo "Docs: https://docs.x402layer.cc/agentic-access/openclaw-skill"
