#!/bin/bash
# Auto-unregister this Claude Code session from the ouija daemon.
# Runs as a SessionEnd hook when a session terminates.

PANE="${TMUX_PANE:-}"
[ -z "$PANE" ] && exit 0

PORT="${OUIJA_PORT:-7880}"
BASE="http://localhost:${PORT}"

# Find session ID registered to this pane
SID=$(curl -sf "${BASE}/api/status" 2>/dev/null \
  | jq -r --arg pane "$PANE" '.sessions[] | select(.pane == $pane and .origin == "local") | .id' 2>/dev/null)

[ -z "$SID" ] && exit 0

curl -sf -X POST "${BASE}/api/remove" \
  -H "Content-Type: application/json" \
  -d "{\"id\":\"${SID}\"}" >/dev/null 2>&1
