#!/usr/bin/env bash
# Standalone installer for the WhatsApp MCP — an alternative to the Claude Desktop
# plugin. Installs + starts the always-on bridge service (so WhatsApp pairing can
# begin), then registers the MCP server with Claude Code (`claude mcp add`) so you
# can use it from the CLI. Re-runnable; safe to run again to repair an install.
#
#   ./install.sh
#
# After it finishes: scan the QR it points you to (WhatsApp ▸ Linked Devices), then
# in Claude Code ask "what's my WhatsApp status".
set -euo pipefail

SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXT=""; case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*) EXT=".exe";; esac
WA_MCP_BUNDLED="$SELF_DIR/bin/wa-mcp$EXT"
[ -x "$WA_MCP_BUNDLED" ] || { echo "error: $WA_MCP_BUNDLED missing or not executable" >&2; exit 1; }

say() { printf '\033[1;34m[install]\033[0m %s\n' "$*"; }

# 1. Install + start the bridge service (reuses the binary's own bootstrap logic).
say "installing + starting the WhatsApp bridge service…"
INSTALL_OUT="$("$WA_MCP_BUNDLED" --install)"
echo "$INSTALL_OUT" | sed 's/^/    /'

# Parse the machine-readable KEY=VALUE lines the binary printed.
get() { echo "$INSTALL_OUT" | sed -n "s/^$1=//p" | tail -1; }
WA_MCP="$(get WA_MCP)"; PAIR_URL="$(get PAIR_URL)"; LOGGED_IN="$(get LOGGED_IN)"
[ -n "$WA_MCP" ] || WA_MCP="$WA_MCP_BUNDLED"

# 2. Register the MCP with Claude Code (user scope), if the CLI is present.
if command -v claude >/dev/null 2>&1; then
  say "registering MCP with Claude Code (name: whatsapp)…"
  claude mcp remove whatsapp >/dev/null 2>&1 || true
  if claude mcp add --scope user whatsapp -- "$WA_MCP" >/dev/null 2>&1; then
    say "registered. Use it in any Claude Code session."
  else
    say "could not auto-register; add it manually:"
    echo "    claude mcp add --scope user whatsapp -- \"$WA_MCP\""
  fi
else
  say "Claude Code CLI not found. To use this MCP, run:"
  echo "    claude mcp add --scope user whatsapp -- \"$WA_MCP\""
fi

# 2b. Install bundled skills for Claude Code (personal scope), if the CLI is present.
#     Claude Code auto-discovers skills under ~/.claude/skills/<name>/SKILL.md.
SKILLS_SRC="$SELF_DIR/skills"
if command -v claude >/dev/null 2>&1 && [ -d "$SKILLS_SRC" ]; then
  SKILLS_DST="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/skills"
  mkdir -p "$SKILLS_DST"
  for d in "$SKILLS_SRC"/*/; do
    [ -f "${d}SKILL.md" ] || continue
    name="$(basename "$d")"
    rm -rf "$SKILLS_DST/$name"            # idempotent: replace any prior copy
    cp -R "$d" "$SKILLS_DST/$name"
    say "installed skill: $name"
  done
fi

# 3. Pairing.
echo
if [ "$LOGGED_IN" = "1" ]; then
  say "WhatsApp is already linked — nothing to scan. You're done."
else
  say "Link your phone: WhatsApp ▸ Settings ▸ Linked Devices ▸ Link a Device"
  if [ -n "$PAIR_URL" ]; then
    say "Open this page and scan the QR:  $PAIR_URL"
    case "$(uname -s)" in Darwin) open "$PAIR_URL" >/dev/null 2>&1 || true;; Linux) xdg-open "$PAIR_URL" >/dev/null 2>&1 || true;; esac
  else
    say "Bridge still starting — wait a few seconds, then open http://localhost:8080/pair"
  fi
fi
echo
say "Done. To remove everything later, run ./uninstall.sh"
