#!/bin/bash
# Ouija mesh status line for Claude Code.

PORT="${OUIJA_PORT:-7880}"
STATUS=$(curl -sf "http://localhost:${PORT}/api/status" 2>/dev/null) || { echo "ouija | offline"; exit 0; }

PANE="${TMUX_PANE:-$(tmux display-message -p '#{pane_id}' 2>/dev/null)}"

# My session ID
MY_ID=""
if [ -n "$PANE" ]; then
  MY_ID=$(echo "$STATUS" | jq -r --arg pane "$PANE" '.sessions[] | select(.pane == $pane) | .id' 2>/dev/null)
fi

# Peer count (all other sessions: local + remote, excluding self)
if [ -n "$MY_ID" ]; then
  PEERS=$(echo "$STATUS" | jq --arg me "$MY_ID" '[.sessions[] | select(.id != $me)] | length' 2>/dev/null)
else
  PEERS=$(echo "$STATUS" | jq '.sessions | length' 2>/dev/null)
fi

# Version
DAEMON_V=$(echo "$STATUS" | jq -r '.version // ""')
PLUGIN_V=""
for d in "$HOME"/.claude/plugins/cache/ouija/ouija/*/; do
  [ -f "${d}.version" ] && PLUGIN_V=$(cat "${d}.version" 2>/dev/null) && break
done

# Build parts
PARTS=()

if [ -n "$MY_ID" ]; then
  PARTS+=("ouija id: $MY_ID")
elif [ -n "$PANE" ] && echo "$STATUS" | jq -e --arg pane "$PANE" '.claude_panes[] | select(.pane_id == $pane)' >/dev/null 2>&1; then
  PARTS+=("ouija id: \033[33mregistering…\033[0m")
else
  PARTS+=("ouija id: \033[33munregistered\033[0m")
fi

PARTS+=("peers: ${PEERS:-0}")

if [ -n "$DAEMON_V" ] && [ -n "$PLUGIN_V" ] && [ "$DAEMON_V" != "$PLUGIN_V" ]; then
  PARTS+=("\033[33m⚠ daemon=${DAEMON_V} plugin=${PLUGIN_V}\033[0m")
else
  PARTS+=("v${DAEMON_V}")
fi

echo -e "$(IFS='|'; echo "${PARTS[*]}" | sed 's/|/ | /g')"
