#!/usr/bin/env bash
# Coordination Protocol — Quickstart Walkthrough (Reference Server: Sundry)
#
# By default this script runs ONLY safe, read-only requests against the live
# Sundry reference server (https://sundry.to) and prints copy-pasteable
# create/stage examples without mutating production demos.
#
# Usage:
#   bash examples/quickstart.sh
#   bash examples/quickstart.sh http://127.0.0.1:3000

set -euo pipefail

BASE="${1:-https://sundry.to}"
BASE="${BASE%/}"

echo "=== Coordination Protocol Quickstart ==="
echo "Target: ${BASE} (read-only live calls + copy-paste write examples)"
echo

echo "1) Fetch site-wide agent guide (/llms.txt)"
echo "   GET ${BASE}/llms.txt"
curl -fsS "${BASE}/llms.txt" | head -n 12
echo "   ..."
echo

echo "2) MCP JSON-RPC 2.0 initialize (returns fixed assistant instructions)"
echo "   POST ${BASE}/api/mcp"
curl -fsS -X POST "${BASE}/api/mcp" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
echo
echo

echo "3) MCP JSON-RPC 2.0 tools/list (discover available coordination tools)"
echo "   POST ${BASE}/api/mcp"
curl -fsS -X POST "${BASE}/api/mcp" \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'
echo
echo

echo "4) Dual-surface inspect via Accept: application/json"
echo "   GET ${BASE}/p/friday-ramen-club"
curl -fsS -H "Accept: application/json" "${BASE}/p/friday-ramen-club"
echo
echo

echo "5) Dual-surface inspect via .json suffix (two-channel envelope)"
echo "   GET ${BASE}/s/nopa-dinner.json"
curl -fsS "${BASE}/s/nopa-dinner.json"
echo
echo

echo "6) Read redacted Activity Tape"
echo "   GET ${BASE}/api/boards/friday-ramen-club/events"
curl -fsS "${BASE}/api/boards/friday-ramen-club/events"
echo
echo

cat <<EOF
=== Copy-Paste Write Examples (Not Executed Automatically) ===

A) Create a custom multi-block board with Idempotency-Key:
curl -s -X POST ${BASE}/api/mcp/create_board \\
  -H "Content-Type: application/json" \\
  -H "Idempotency-Key: my-trip-board-001" \\
  -d @profiles/multi-block-trip.json

B) Stage an answer on a board you created (always staged; returns confirm_url):
curl -s -X POST ${BASE}/api/boards/YOUR_BOARD_ID/stage \\
  -H "Content-Type: application/json" \\
  -H "Idempotency-Key: dave-stage-001" \\
  -d '{
    "actor_name": "Dave",
    "via_agent": "Claude",
    "actions": [
      { "block_id": "dates", "type": "vote", "values": { "opt-fri": "in" } }
    ]
  }'

   # Hand confirm_url to your person. To attach a private note that stays
   # strictly in their browser (never sent to the server), append:
   #   #why=Free%20Friday%20evening

C) Stage a conditional commitment + counter-proposal via MCP tools/call:
curl -s -X POST ${BASE}/api/mcp \\
  -H "Content-Type: application/json" \\
  -H "Idempotency-Key: sarah-rally-001" \\
  -d '{
    "jsonrpc": "2.0",
    "id": 3,
    "method": "tools/call",
    "params": {
      "name": "sundry.stage_actions",
      "arguments": {
        "board_id": "YOUR_BOARD_ID",
        "actor_name": "Sarah",
        "via_agent": "Claude",
        "actions": [
          {
            "block_id": "rally",
            "type": "vote",
            "values": { "sat-afternoon": "in" },
            "condition": { "min_participants": 6, "max_cost_cents": 10000 }
          }
        ]
      }
    }
  }'

D) Verify protocol conformance (read-only):
node conformance/check.mjs ${BASE}
EOF
