#!/usr/bin/env bash
set -euo pipefail

PACKRIFT_MCP_ENDPOINT='https://mcp.packrift.com/mcp?packrift_mcp_source=browser_agent_bridge&packrift_mcp_target=generic_streamable_http'
PACKRIFT_MCP_USER_AGENT='MCP-Order-Handoff/1.0 (+https://mcp.packrift.com/r/order/browser_agent_bridge?format=html)'
PACKRIFT_MCP_SESSION_ID="${PACKRIFT_MCP_SESSION_ID:-mcp-order-browser_agent_bridge-$(date -u +%Y%m%dT%H%M%SZ)-$RANDOM}"
PACKRIFT_BUYER_CONFIRMED="${PACKRIFT_BUYER_CONFIRMED:-0}"
PACKRIFT_UNCONFIRMED_JSON_RPC='{"jsonrpc":"2.0","id":"prepare-1066-unconfirmed","method":"tools/call","params":{"name":"prepare_purchase_handoff","arguments":{"sku":"1066","quantity":1,"source_context":"browser_agent_bridge_purchase_handoff","mcp_source_context":"browser_agent_bridge","mcp_install_target":"generic_streamable_http","journey_id":"mcp_order_handoff_browser_agent_bridge_1066_20260610","result_set_id":"mcp_order_handoff_20260610","utm_term":"1066","buyer_confirmed":false}}}'
PACKRIFT_CONFIRMED_JSON_RPC='{"jsonrpc":"2.0","id":"prepare-1066-confirmed","method":"tools/call","params":{"name":"prepare_purchase_handoff","arguments":{"sku":"1066","quantity":1,"source_context":"browser_agent_bridge_purchase_handoff","mcp_source_context":"browser_agent_bridge","mcp_install_target":"generic_streamable_http","journey_id":"mcp_order_handoff_browser_agent_bridge_1066_20260610","result_set_id":"mcp_order_handoff_20260610","utm_term":"1066","buyer_confirmed":true}}}'
PACKRIFT_MCP_LAST_RESPONSE=''

rpc() {
  PACKRIFT_MCP_LAST_RESPONSE="$(curl -sS "$PACKRIFT_MCP_ENDPOINT" \
    -H 'content-type: application/json' \
    -H 'accept: application/json, text/event-stream' \
    -H "Mcp-Session-Id: $PACKRIFT_MCP_SESSION_ID" \
    -H "user-agent: $PACKRIFT_MCP_USER_AGENT" \
    -d "$1")"
  normalize_mcp_response
}

normalize_mcp_response() {
  if printf "%s\n" "$PACKRIFT_MCP_LAST_RESPONSE" | grep -q "^data:"; then
    printf "%s\n" "$PACKRIFT_MCP_LAST_RESPONSE" | sed -n "s/^data:[[:space:]]*//p" | tail -n 1
    return
  fi
  printf "%s\n" "$PACKRIFT_MCP_LAST_RESPONSE"
}

extract_measured_cart_url() {
  printf "%s\n" "$PACKRIFT_MCP_LAST_RESPONSE" | grep -Eo 'https://mcp\.packrift\.com/r/cart/[^"[:space:]<>\\]+' | tail -n 1 || true
}

touch_measured_cart_landing() {
  local cart_url="${PACKRIFT_MCP_CART_URL:-$(extract_measured_cart_url)}"
  if [ -z "$cart_url" ]; then
    printf "No measured Packrift MCP /r/cart URL found in the confirmed response.\n" >&2
    return 1
  fi
  printf "Opening measured Packrift MCP cart landing: %s\n" "$cart_url"
  curl -sS -o /dev/null "$cart_url" -H "user-agent: $PACKRIFT_MCP_USER_AGENT"
  printf "Recorded mcp_cart_landing. No order was placed. Shopify checkout still requires buyer action.\n"
}

echo 'Packrift MCP source-preserving order handoff'
echo 'Source: browser_agent_bridge; target: generic_streamable_http; SKU: 1066; quantity: 1'
echo 'Running buyer_confirmed=false live check first. This cannot create a cart URL or order.'
rpc "$PACKRIFT_UNCONFIRMED_JSON_RPC"

if [ "$PACKRIFT_BUYER_CONFIRMED" != "1" ]; then
  echo ''
  echo 'Buyer confirmation is still required before generating the measured MCP cart handoff.'
  echo 'After buyer/reviewer approval, run:'
  echo 'PACKRIFT_BUYER_CONFIRMED=1 curl -sS '\''https://mcp.packrift.com/r/order/browser_agent_bridge?format=sh'\'' | bash'
  echo 'The confirmed call returns a measured /r/cart URL only; it still does not place an order.'
  exit 0
fi

echo ''
echo 'Buyer confirmation flag detected. Generating measured MCP cart handoff only; no order is placed.'
rpc "$PACKRIFT_CONFIRMED_JSON_RPC"
touch_measured_cart_landing

