#!/usr/bin/env bash
# =============================================================================
#  verify-everything.sh — reproduce every live BlazePhoenix claim in ~60s.
#
#  Do not trust the site; run this. Needs: curl, jq. Optional: foundry's
#  `cast` — when present, on-chain reads are ALSO reproduced directly against
#  your RPC, bypassing blazephoenix.xyz entirely. Set BASE_RPC to your own
#  Base node (the quote below is read through it, never through our servers).
#
#    curl -sO https://blazephoenix.xyz/repro/verify-everything.sh && bash verify-everything.sh
# =============================================================================
set -u
BASE="${BLAZEPHOENIX_ORIGIN:-https://blazephoenix.xyz}"
RPC="${BASE_RPC:-https://mainnet.base.org}"
pass=0; fail=0
ok()   { echo "  ✓ $1"; pass=$((pass+1)); }
bad()  { echo "  ✗ $1"; fail=$((fail+1)); }

command -v curl >/dev/null || { echo "curl required"; exit 1; }
command -v jq   >/dev/null || { echo "jq required";   exit 1; }

echo "— Live-verifiable facts (re-executed on-chain via $BASE/api/verify) —"
ids=$(curl -sf "$BASE/api/verify" | jq -r '.verifiable[].id')
for id in $ids; do
  r=$(curl -sf "$BASE/api/verify?fact=$id")
  if [ "$(echo "$r" | jq -r '.ok')" = "true" ]; then
    ok "$id → $(echo "$r" | jq -r '.verified.decoded // .verified.raw' | cut -c1-48) @ block $(echo "$r" | jq -r '.verified.block')"
  else
    bad "$id ($(echo "$r" | jq -r '.code // "unknown"')) — retry: it may be a transient RPC miss"
  fi
done

echo "— Solvency (the invariant, not a dashboard) —"
b=$(curl -sf "$BASE/api/badge" | jq -r '.message')
# The staking address is written here at deployment; before it exists there
# is no verdict to check, and the badge says "coming soon". Only the exact
# solvent message passes; insolvent, unreachable or an empty answer fail.
STAKING="<staking>"
case "$b" in "isSolvent() = true") ok "badge: $b";; "coming soon"|"not deployed yet") echo "  · staking: address published at deployment, skipped";; *) bad "badge: $b";; esac
if ! echo "$STAKING" | grep -Eq '^0x[0-9a-fA-F]{40}$'; then
  echo "  · staking: address published at deployment, cast check skipped"
elif command -v cast >/dev/null; then
  s=$(cast call "$STAKING" "isSolvent()(bool)" --rpc-url "$RPC" 2>/dev/null)
  [ "$s" = "true" ] && ok "cast isSolvent() = true (independent of the site)" || bad "cast isSolvent() = $s"
else
  echo "  · install foundry (cast) to reproduce isSolvent() without trusting this site"
fi

echo "— A real quote, computed on-chain and read on YOUR RPC ($RPC) —"
# The site performs no RPC for integrators: it prepares the eth_call, your node
# runs it, and the pure decoder turns the answer into the quote.
p=$(curl -sf "$BASE/api/quote/prepare?chain=base&in=ETH&out=USDC&amountIn=100000000000000000")
if [ "$(echo "$p" | jq -r '.ok')" = "true" ]; then
  res=$(curl -sf "$RPC" -H 'content-type: application/json' -d "$(echo "$p" | jq -c '.rpcRequest')" | jq -r '.result // empty')
  if [ -n "$res" ]; then
    q=$(jq -n --argjson req "$(echo "$p" | jq -c '.request')" --arg res "$res" '{request: $req, result: $res}' \
      | curl -sf -X POST "$BASE/api/quote/decode" -H 'content-type: application/json' -d @-)
    if [ "$(echo "$q" | jq -r '.ok')" = "true" ]; then
      ok "0.1 ETH → $(echo "$q" | jq -r '.amountOut') USDC base-units · v$(echo "$q" | jq -r '.protocolVersion') · verdict $(echo "$q" | jq -r '.checks.verdict') · floor enforced on-chain"
    else
      bad "decode failed: $(echo "$q" | jq -r '.error // "unknown"')"
    fi
  else
    bad "your RPC ($RPC) returned no result for the prepared eth_call"
  fi
else
  bad "prepare failed: $(echo "$p" | jq -r '.error // "unknown"')"
fi

echo "— Authorship priority (Bitcoin-anchored) —"
n=$(curl -sf "$BASE/provenance/provenance.json" | jq -r '.proofs | length')
[ "${n:-0}" -gt 0 ] && ok "$n OpenTimestamps proofs published — verify any with: ots verify <proof>" || bad "no provenance proofs found"

echo ""
echo "passed $pass · failed $fail — every claim above has a reproduction path;"
echo "the full proof-carrying list is at $BASE/facts.json"
[ "$fail" -eq 0 ]
