#!/bin/bash
# Thin wrapper that delegates to @quantiya/codevibe-claude-plugin

SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  DIR="$(cd "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$(cd "$(dirname "$SOURCE")" && pwd)"

DELEGATE="$SCRIPT_DIR/../node_modules/@quantiya/codevibe-claude-plugin/bin/codevibe-claude"

if [ ! -f "$DELEGATE" ]; then
  echo "Error: @quantiya/codevibe-claude-plugin not found. Try reinstalling: npm install -g @quantiya/codevibe" >&2
  exit 1
fi

case "${1:-}" in
  login|logout|help|--help|-h|version|--version|-v)
    ;;
  *)
    if ! command -v claude >/dev/null 2>&1; then
      cat >&2 <<'EOF'

Claude Code is not installed on this system.

CodeVibe attaches to the Claude Code CLI — you need it installed first.

  Install Claude Code:
    npm install -g @anthropic-ai/claude-code

Then run codevibe-claude again.

Learn more: https://quantiya.ai/codevibe
EOF
      exit 1
    fi
    ;;
esac

# Activation telemetry (background, non-blocking, no PII)
_CID="$(echo "$(uname -n)-$(id -u)" | (sha256sum 2>/dev/null || shasum -a 256 2>/dev/null || echo "anonymous-fallback ") | cut -c1-36)"
curl -s -X POST "https://www.google-analytics.com/mp/collect?measurement_id=G-GS74YEQTB8&api_secret=lAfOF6OxRzSQ-NsLBRjhAg" \
  -H "Content-Type: application/json" \
  -d "{\"client_id\":\"${_CID}\",\"events\":[{\"name\":\"wrapper_launched\",\"params\":{\"agent\":\"claude\",\"platform\":\"$(uname -s)\"}}]}" \
  >/dev/null 2>&1 &

exec "$DELEGATE" "$@"
