#!/usr/bin/env bash
# =============================================================================
# OpenClaw Mac Setup - Mikes-MacBook-Air
# Run this on the Mac after cloning the ClaudeTools repo from Gitea
# =============================================================================
set -euo pipefail

echo "[INFO] OpenClaw Mac Setup - Starting"
echo "================================================"

# --- Step 1: Install OpenClaw ---
echo ""
echo "[INFO] Step 1: Installing OpenClaw..."
if command -v openclaw &>/dev/null; then
    echo "[OK] OpenClaw already installed: $(openclaw --version)"
else
    echo "[INFO] Installing via install script..."
    curl -fsSL https://openclaw.ai/install.sh | bash -s -- --no-onboard
fi

# --- Step 2: Install 1Password CLI (if needed) ---
echo ""
echo "[INFO] Step 2: Checking 1Password CLI..."
if command -v op &>/dev/null; then
    echo "[OK] op CLI found: $(op --version)"
else
    echo "[INFO] Installing 1Password CLI via brew..."
    brew install --cask 1password-cli
fi

# --- Step 3: Verify Ollama ---
echo ""
echo "[INFO] Step 3: Checking Ollama..."
if command -v ollama &>/dev/null; then
    echo "[OK] Ollama found"
    echo "[INFO] Available models:"
    ollama list
    
    # Pull required models if missing
    for model in "qwen3:14b" "codestral:22b" "nomic-embed-text"; do
        if ! ollama list | grep -q "$model"; then
            echo "[INFO] Pulling $model..."
            ollama pull "$model"
        else
            echo "[OK] $model already available"
        fi
    done
else
    echo "[WARNING] Ollama not found. Install from https://ollama.com/download"
    echo "          Then run: ollama pull qwen3:14b codestral:22b nomic-embed-text"
fi

# --- Step 4: Install tmux (needed for 1Password) ---
echo ""
echo "[INFO] Step 4: Checking tmux..."
if command -v tmux &>/dev/null; then
    echo "[OK] tmux found: $(tmux -V)"
else
    echo "[INFO] Installing tmux..."
    brew install tmux
fi

# --- Step 5: Onboard OpenClaw ---
echo ""
echo "[INFO] Step 5: OpenClaw onboarding..."
echo "[INFO] You'll need your Anthropic API key."
echo "[INFO] If stored in 1Password, retrieve it first:"
echo "       op item get 'Anthropic' --fields label=credential --reveal"
echo ""
echo "[INFO] Run: openclaw onboard"
echo "[INFO] Select Anthropic, enter API key, install daemon."
echo ""
echo "[WARNING] DO NOT run onboard automatically - it needs interactive input."
echo "          Run it manually after this script completes."

# --- Step 6: Post-onboard configuration ---
# This generates a script to run AFTER onboarding completes
cat > /tmp/oc-mac-post-onboard.sh << 'POSTEOF'
#!/usr/bin/env bash
set -euo pipefail

echo "[INFO] Post-onboard configuration..."

# Set model chain: Claude primary, Ollama fallback
openclaw config set agents.defaults.model '{"primary":"anthropic/claude-opus-4-6","fallbacks":["ollama/qwen3:14b"]}'
echo "[OK] Model chain configured: Claude Opus -> ollama/qwen3:14b"

# Enable Ollama auto-discovery
# The systemd/launchd service needs OLLAMA_API_KEY in env
# On macOS with launchd, we set it in the config instead
openclaw config set models.providers.ollama.apiKey "ollama-local" 2>/dev/null || true

# Enable web search
openclaw config set tools.web.search.enabled true
openclaw config set tools.web.search.provider duckduckgo

# Enable plugins
openclaw config set plugins.entries.duckduckgo.enabled true

echo "[OK] Post-onboard config applied"
echo "[INFO] Restart gateway: openclaw gateway restart"
echo ""
echo "[INFO] Next steps:"
echo "  1. Set up Discord bot (get token from Mike/OC-5070)"
echo "  2. Configure workspace files (AGENTS.md, SOUL.md, etc.)"
echo "  3. Connect to Tailscale mesh"
echo ""
echo "[OK] Mac setup complete!"
POSTEOF
chmod +x /tmp/oc-mac-post-onboard.sh

echo ""
echo "================================================"
echo "[OK] Pre-onboard setup complete!"
echo ""
echo "Next steps (in order):"
echo "  1. Run: openclaw onboard"
echo "     - Select Anthropic provider"
echo "     - Enter API key"
echo "     - Install daemon: yes"
echo "  2. Run: bash /tmp/oc-mac-post-onboard.sh"
echo "  3. Set up Discord bot (see fleet/DISCORD_BOT_SETUP.md)"
echo "  4. Copy fleet/mac-workspace/* into OpenClaw workspace:"
echo "     cp fleet/mac-workspace/* \$(openclaw config get agents.defaults.workspace)/"
echo "  5. Start chatting - bot will read BOOTSTRAP.md and set itself up"
echo "================================================"
