# =============================================================================
# OpenClaw Beast Setup - Home Desktop (Win11, i9/RTX 4090/128GB)
# Run this in PowerShell (Admin) after cloning ClaudeTools from Gitea
# =============================================================================

Write-Host "[INFO] OpenClaw Beast Setup - Starting" -ForegroundColor Cyan
Write-Host "================================================"

# --- Step 1: Install OpenClaw ---
Write-Host ""
Write-Host "[INFO] Step 1: Installing OpenClaw..." -ForegroundColor Cyan
if (Get-Command openclaw -ErrorAction SilentlyContinue) {
    Write-Host "[OK] OpenClaw already installed: $(openclaw --version)" -ForegroundColor Green
} else {
    Write-Host "[INFO] Installing via install script..."
    iwr -useb https://openclaw.ai/install.ps1 | iex
}

# --- Step 2: Install 1Password CLI (if needed) ---
Write-Host ""
Write-Host "[INFO] Step 2: Checking 1Password CLI..." -ForegroundColor Cyan
if (Get-Command op -ErrorAction SilentlyContinue) {
    Write-Host "[OK] op CLI found: $(op --version)" -ForegroundColor Green
} else {
    Write-Host "[INFO] Installing 1Password CLI via winget..."
    winget install AgileBits.1Password.CLI
}

# --- Step 3: Verify Ollama ---
Write-Host ""
Write-Host "[INFO] Step 3: Checking Ollama..." -ForegroundColor Cyan
if (Get-Command ollama -ErrorAction SilentlyContinue) {
    Write-Host "[OK] Ollama found" -ForegroundColor Green
    Write-Host "[INFO] Available models:"
    ollama list

    # Pull required models if missing
    $models = @("qwen3:14b", "codestral:22b", "nomic-embed-text")
    foreach ($model in $models) {
        $list = ollama list
        if ($list -notmatch [regex]::Escape($model)) {
            Write-Host "[INFO] Pulling $model..."
            ollama pull $model
        } else {
            Write-Host "[OK] $model already available" -ForegroundColor Green
        }
    }

    # Beast-specific: pull larger models that take advantage of 24GB VRAM + 128GB RAM
    Write-Host ""
    Write-Host "[INFO] Beast has RTX 4090 (24GB) + 128GB RAM - consider pulling larger models:" -ForegroundColor Yellow
    Write-Host "  ollama pull llama3.1:70b    # Near-cloud reasoning quality"
    Write-Host "  ollama pull qwen3:32b       # Strong general purpose"
    Write-Host "  ollama pull deepseek-r1:32b  # Step-by-step reasoning"
} else {
    Write-Host "[WARNING] Ollama not found. Install from https://ollama.com/download" -ForegroundColor Yellow
}

# --- Step 4: Summary ---
Write-Host ""
Write-Host "================================================"
Write-Host "[OK] Pre-onboard setup complete!" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps (in order):"
Write-Host "  1. Run: openclaw onboard"
Write-Host "     - Select Anthropic provider"
Write-Host "     - Enter API key"
Write-Host "     - Install daemon: yes"
Write-Host "  2. Configure Discord bot token:"
Write-Host "     openclaw config set channels.discord.enabled true"
Write-Host "     openclaw config set channels.discord.token 'BOT_TOKEN'"
Write-Host "     openclaw config set channels.discord.groupPolicy allowlist"
Write-Host "     openclaw config set 'channels.discord.guilds' '{""*"":{}}'"
Write-Host "  3. Configure model chain:"
Write-Host "     openclaw config set agents.defaults.model '{""primary"":""anthropic/claude-opus-4-6"",""fallbacks"":[""ollama/qwen3:14b""]}'"
Write-Host "  4. Copy fleet\beast-workspace\* into OpenClaw workspace"
Write-Host "  5. openclaw gateway restart"
Write-Host "  6. Start chatting - bot reads BOOTSTRAP.md and sets itself up"
Write-Host "================================================"
