#!/bin/bash

# Installation procedure for mcp-adp-bigmind
# No external API token required — all data is stored locally.
# This script is called by the main install.sh script.
# Parameter: $1 = comma-separated list of IDEs (e.g., "VS Code,IntelliJ")

set -e

SELECTED_IDES="$1"
BASEDIR=$(cd "$(dirname "$0")" && pwd)
MCP_NAME="ADP BigMind"

# ── Validate Python and uv ────────────────────────────────────────────────────
source "$(dirname "$BASEDIR")/helper_scripts/shared_uv_setup.sh"

# ── Install Python dependencies ───────────────────────────────────────────────
if [ ! -d "$BASEDIR/.venv" ]; then
    echo "Installing dependencies for $MCP_NAME..." >&2
    cd "$BASEDIR"
    uv sync >&2
fi

# ── Create the memory DB directory ───────────────────────────────────────────
BIGMIND_DIR="$HOME/.mcp/bigmind"
mkdir -p "$BIGMIND_DIR"
echo "INFO: BigMind DB directory: $BIGMIND_DIR/memory.db" >&2

# ── Write GitHub Copilot instructions (Layer 4) ───────────────────────────────
DEFAULT_WORKSPACE="$(dirname "$BASEDIR")"

echo "" >&2
echo "────────────────────────────────────────────────────────────" >&2
echo "BigMind — GitHub Copilot Instructions Setup" >&2
echo "────────────────────────────────────────────────────────────" >&2
echo "BigMind works best when Copilot is told to use memory tools" >&2
echo "at the start and end of every conversation." >&2
echo "" >&2
echo "Enter your workspace root path where .github/copilot-instructions.md" >&2
echo "should be written (press Enter to use: $DEFAULT_WORKSPACE):" >&2
read -r WORKSPACE_PATH
WORKSPACE_PATH="${WORKSPACE_PATH:-$DEFAULT_WORKSPACE}"

COPILOT_DIR="$WORKSPACE_PATH/.github"
COPILOT_FILE="$COPILOT_DIR/copilot-instructions.md"

BIGMIND_BLOCK="
## BigMind Persistent Memory

You have access to a BigMind persistent memory MCP server.

Rules (mandatory):
- ALWAYS call memory_start_session() as the very first action of every conversation.
- ALWAYS call memory_end_session() as the very last action before closing a conversation.
- Call memory_flag_important() whenever a significant decision, code change, or new
  preference is mentioned — do not wait to be asked.
- If asked \"do you remember…\", call memory_search_chunks() or
  memory_get_session_detail() before answering from your own knowledge.
"

mkdir -p "$COPILOT_DIR"

if [ -f "$COPILOT_FILE" ]; then
    if grep -q "BigMind Persistent Memory" "$COPILOT_FILE" 2>/dev/null; then
        echo "INFO: BigMind instructions already present in $COPILOT_FILE — skipping." >&2
    else
        printf '%s' "$BIGMIND_BLOCK" >> "$COPILOT_FILE"
        echo "INFO: BigMind instructions appended to $COPILOT_FILE" >&2
    fi
else
    printf '# Copilot Instructions\n%s' "$BIGMIND_BLOCK" > "$COPILOT_FILE"
    echo "INFO: Created $COPILOT_FILE with BigMind instructions" >&2
fi

echo "" >&2
echo "✅  Copilot instructions written to: $COPILOT_FILE" >&2
echo "────────────────────────────────────────────────────────────" >&2

# ── Output MCP config snippet ─────────────────────────────────────────────────
echo "MCP_NAME=$MCP_NAME"
echo "MCP_SNIPPET="
cat <<EOF
{
  "command": "uv",
  "args": [
    "--directory",
    "$BASEDIR",
    "run",
    "src/server.py"
  ],
  "env": {
    "BIGMIND_USER": "$USER"
  }
}
EOF

exit 0

