
Write-Host "=== BRAIN Project 自动安装脚本 (Steps 0-4) ===" -ForegroundColor Cyan

# Step 0: Prerequisites (Critical)
Write-Host "`n[1/5] 正在运行步骤 0: 检查基础环境 (Node.js/Python)..." -ForegroundColor Yellow
.\Step0_ensure_prerequisites.ps1
if ($LASTEXITCODE -ne 0) { 
    Write-Host "❌ 步骤 0 失败。请检查上方错误信息。" -ForegroundColor Red; 
    Pause; exit 
}

# Force refresh environment variables (to find new Python/Node)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")

# Initialize and activate project-level virtual environment
$venvPath = "$(Get-Location)\.venv"
$pythonExe = "$venvPath\Scripts\python.exe"

if (!(Test-Path $pythonExe)) {
    Write-Host "`n正在创建虚拟环境..." -ForegroundColor Cyan
    python -m venv .venv
}

$env:PATH = "$venvPath\Scripts;" + $env:PATH
$env:VIRTUAL_ENV = $venvPath
Write-Host "已切换到虚拟环境: $venvPath" -ForegroundColor Green

# Step 1: Init Project Files
Write-Host "`n[2/5] 正在运行步骤 1: 初始化项目文件..." -ForegroundColor Yellow
# Try to find python. If not in path yet, try 'py' launcher, or hope it's in path now
try {
    python Step1_init_project_files.py
} catch {
    Write-Host "Python命令未找到，尝试使用 'py' 启动器..."
    py Step1_init_project_files.py
}

if ($LASTEXITCODE -ne 0) { 
    Write-Host "❌ 步骤 1 失败。" -ForegroundColor Red; 
    Pause; exit 
}

# Step 2: Config Brain Agent
Write-Host "`n[3/5] 正在运行步骤 2: 配置 BRAIN 顾问角色..." -ForegroundColor Yellow
.\Step2_config_brainSystemPrompt_Toclaude.ps1
if ($LASTEXITCODE -ne 0) { 
    Write-Host "❌ 步骤 2 失败。" -ForegroundColor Red; 
    Pause; exit 
}

# Step 3: Setup MCP
Write-Host "`n[4/5] 正在运行步骤 3: 注册 MCP 工具服务..." -ForegroundColor Yellow
.\Step3_setup_mcp.ps1
if ($LASTEXITCODE -ne 0) { 
    Write-Host "❌ 步骤 3 失败。" -ForegroundColor Red; 
    Pause; exit 
}

# Step 4: Configure Model (Interactive)
Write-Host "`n[5/5] 正在运行步骤 4: 配置 API 模型..." -ForegroundColor Yellow
try {
    python Step4_SetAPI_And_Check_LLMModel.py
} catch {
    py Step4_SetAPI_And_Check_LLMModel.py
}
if ($LASTEXITCODE -ne 0) { 
    Write-Host "❌ 步骤 4 失败。" -ForegroundColor Red; 
    Pause; exit 
}

# Add Shortcut creation
Write-Host "`n[+] 创建桌面快捷方式..." -ForegroundColor Yellow
$wsh = New-Object -ComObject WScript.Shell
$desktop = [Environment]::GetFolderPath("Desktop")
$shortcut = $wsh.CreateShortcut("$desktop\BRAIN打工人_双击可开多个.lnk")
$shortcut.TargetPath = "cmd.exe"

# Ensure .claude directory exists (user's working context)
$claudeDir = Join-Path $HOME ".claude"
if (-not (Test-Path $claudeDir)) { New-Item -ItemType Directory -Path $claudeDir -Force | Out-Null }

# Arguments: Use cmd to avoid PowerShell execution policy issues. Run in .claude dir.
$shortcut.Arguments = "/k title Brain Consultant && echo Starting Brain Consultant... && claude"
$shortcut.WorkingDirectory = $claudeDir

if (Test-Path "icon.ico") {
    $shortcut.IconLocation = "$PWD\icon.ico"
}

$shortcut.Save()
Write-Host "✅ 桌面快捷方式已创建: BRAIN打工人" -ForegroundColor Green

Write-Host "`n🎉 所有安装步骤已完成! 🚀" -ForegroundColor Green
Write-Host "您现在可以使用以下命令启动助手: claude" -ForegroundColor Cyan
Write-Host "按回车键关闭此窗口..."
Read-Host
