#!/bin/sh
# Soul, running locally in one command.
#
#   curl -fsSL https://www.soulverified.com/sandbox | sh
#
# Writes a small app wired to the real SDK, serves it, and opens it. No account,
# no key, no signup: it runs against the public sandbox, which is the same code
# a paying partner runs, against real rows, under a slug that can never be a
# real community. Delete the folder when you are done.

set -e

BASE="https://www.soulverified.com"
DIR="${SOUL_DIR:-soul-demo}"
BOLD="$(printf '\033[1m')"; DIM="$(printf '\033[2m')"; GRN="$(printf '\033[32m')"; OFF="$(printf '\033[0m')"
step() { printf "  ${GRN}·${OFF} %s\n" "$1"; }
dim()  { printf "  ${DIM}%s${OFF}\n" "$1"; }

printf "\n  ${BOLD}Soul${OFF} ${DIM}running locally, in one command${OFF}\n\n"

# ---- a port nothing else is on ----------------------------------------------
PORT=5173
while [ "$PORT" -lt 5260 ]; do
  if command -v lsof >/dev/null 2>&1; then
    lsof -i :"$PORT" >/dev/null 2>&1 || break
  else
    break
  fi
  PORT=$((PORT + 1))
done

# ---- something to serve ------------------------------------------------------
mkdir -p "$DIR"
cat > "$DIR/index.html" <<HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Ridgeline · a fitness app that installed Soul</title>
<style>
  :root { color-scheme: dark }
  body { margin:0; min-height:100vh; background:#0d1012; color:#e9eef0;
         font:15px/1.6 -apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,sans-serif;
         display:flex; align-items:center; justify-content:center; padding:40px 22px }
  .app { width:100%; max-width:420px }
  header { display:flex; align-items:baseline; justify-content:space-between; margin-bottom:26px }
  h1 { margin:0; font-size:17px; letter-spacing:.14em; text-transform:uppercase; font-weight:600 }
  .tag { font-size:11px; letter-spacing:.14em; text-transform:uppercase; color:#7f8d94 }
  .tile { border:1px solid #1c2225; border-radius:14px; padding:16px 18px; margin-bottom:10px; background:#12181a }
  .tile b { display:block; font-size:11px; letter-spacing:.14em; text-transform:uppercase; color:#7f8d94; font-weight:500; margin-bottom:6px }
  .tile span { font-size:27px; font-weight:600; letter-spacing:-.01em }
  .row { display:flex; gap:9px; flex-wrap:wrap; margin-top:24px }
  button { font:inherit; font-size:13.5px; padding:11px 16px; border-radius:999px; cursor:pointer;
           border:1px solid #262f33; background:#161d20; color:#e9eef0 }
  button.p { background:#7fd0c0; color:#07120f; border-color:transparent; font-weight:600 }
  button:hover { filter:brightness(1.08) }
  .note { margin-top:20px; font-size:12px; color:#66737a; line-height:1.6 }
  code { background:#12181a; border:1px solid #1c2225; padding:1px 5px; border-radius:5px; font-size:11.5px }
</style>
</head>
<body>
  <div class="app">
    <header><h1>Ridgeline</h1><span class="tag">Soul installed</span></header>

    <div class="tile"><b>Readiness</b><span>82</span></div>
    <div class="tile"><b>Sleep</b><span>7h 41m</span></div>
    <div class="tile"><b>Weekly load</b><span>4 sessions</span></div>

    <div class="row">
      <button class="p" onclick="Soul.present({force:true})">Soul.present()</button>
      <button onclick="Soul.open({view:'cards'})">the deck</button>
      <button onclick="Soul.open()">the tab</button>
    </div>

    <p class="note">
      Everything above the buttons is a stand-in for your app. The only Soul code is the
      three lines at the bottom of this file. <code>force:true</code> skips the once-a-day
      guard so you can reopen it; without it a second call inside 24h does nothing.
    </p>
  </div>

<script src="$BASE/sdk.js"></script>
<script>
  Soul.init({ partner: 'try-oura', skin: 'oura', city: 'New York', theme: 'dark', name: 'Ridgeline' });
  // One sandbox user, kept across reloads. Your answers are written to the real
  // API against this id, so a card you accept is still accepted after a refresh.
  var me = localStorage.getItem('soul:sandbox:user');
  if (!me) { me = 'sandbox-' + Math.random().toString(36).slice(2, 10); localStorage.setItem('soul:sandbox:user', me); }
  Soul.identify(me);
  addEventListener('load', function () { setTimeout(function () { Soul.present({ force: true }); }, 600); });
</script>
</body>
</html>
HTML

step "wrote $DIR/index.html"

# ---- serve it ----------------------------------------------------------------
if   command -v python3 >/dev/null 2>&1; then SERVE="python3 -m http.server $PORT --directory $DIR"
elif command -v python  >/dev/null 2>&1; then SERVE="cd $DIR && python -m SimpleHTTPServer $PORT"
elif command -v npx     >/dev/null 2>&1; then SERVE="npx --yes serve -l $PORT $DIR"
else
  step "no python or npx here, so open the file yourself:"
  dim "$DIR/index.html"
  exit 0
fi

URL="http://localhost:$PORT"
step "serving on $URL"

if   command -v open     >/dev/null 2>&1; then (sleep 1; open "$URL") >/dev/null 2>&1 &
elif command -v xdg-open >/dev/null 2>&1; then (sleep 1; xdg-open "$URL") >/dev/null 2>&1 &
fi

printf "\n  ${BOLD}What you are looking at${OFF}\n"
dim "The deck opens over the app on load, unannounced. Escape closes it."
dim "Three cards, one per state of an introduction:"
dim "  proposed  a reason and a time, never a name. Yes or no."
dim "  waiting   you said yes, they have not answered yet."
dim "  matched   both said yes. Now there is a name, a handle and a calendar file."
dim "Every answer is a real write to /api/embed/rsvp. Reload and it is still there."
printf "\n  ${DIM}ctrl-c to stop. rm -rf $DIR to remove it.${OFF}\n\n"

exec $SERVE
