# HarmonyOS MCP Server 一键安装脚本 (Windows PowerShell)
# 内置爬虫,按需下载知识库到 ~/.harmonyos-docs/
# 用法: .\install.ps1

$ErrorActionPreference = "Stop"
Write-Host "╔══════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║   HarmonyOS MCP Server 安装脚本               ║" -ForegroundColor Cyan
Write-Host "║   内置爬虫 · 按需下载 · 始终最新              ║" -ForegroundColor Cyan
Write-Host "╚══════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# 1. 检查 Node.js
Write-Host "[1/3] 检查 Node.js..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version
    Write-Host "  Node.js $nodeVersion ✓" -ForegroundColor Green
} catch {
    Write-Host "  错误: 未找到 Node.js,请先安装 https://nodejs.org/" -ForegroundColor Red
    exit 1
}

# 2. 安装依赖
Write-Host "[2/3] 安装依赖..." -ForegroundColor Yellow
Push-Location $ScriptDir
npm install
if ($LASTEXITCODE -ne 0) {
    Write-Host "  错误: npm install 失败" -ForegroundColor Red
    Pop-Location
    exit 1
}
Write-Host "  依赖安装完成 ✓" -ForegroundColor Green

# 3. 构建
Write-Host "[3/3] 构建..." -ForegroundColor Yellow
npm run build
if ($LASTEXITCODE -ne 0) {
    Write-Host "  错误: 构建失败" -ForegroundColor Red
    Pop-Location
    exit 1
}
Write-Host "  构建完成 ✓" -ForegroundColor Green
Pop-Location

Write-Host ""
Write-Host "══════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "  安装完成!" -ForegroundColor Green
Write-Host "══════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
Write-Host "MCP Server: $ScriptDir\dist\index.js"
Write-Host "知识库路径: ~/.harmonyos-docs (首次使用时自动下载)"
Write-Host ""

# 配置输出
$jsonPath = ($ScriptDir + "\dist\index.js").Replace('\', '\\')
Write-Host "──────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host "复制以下配置到 MCP 客户端:" -ForegroundColor Cyan
Write-Host "──────────────────────────────────────────────" -ForegroundColor Cyan
Write-Host ""
Write-Host @"
{
  "mcpServers": {
    "harmonyos": {
      "command": "node",
      "args": ["$jsonPath"]
    }
  }
}
"@

Write-Host ""
Write-Host "📌 首次使用: 在对话中说"下载 HarmonyOS 文档"即可自动下载" -ForegroundColor Yellow
Write-Host "   知识库将保存到: ~/.harmonyos-docs/ (约 100 MB)" -ForegroundColor DarkGray