# 更新脚本 - 问题排查助手 Web 服务
# 用于将本地更新同步到 192.168.5.60 服务器

param(
    [string]$Server = "192.168.5.60",
    [string]$RemotePath = "C:\troubleshoot",
    [string]$LocalPath = "E:\github\ubains-module-test\develop\.claude\skills\Troubleshoot"
)

Write-Host "=================================================="
Write-Host "更新问题排查助手 Web 服务"
Write-Host "目标服务器: $Server"
Write-Host "目标路径: $RemotePath"
Write-Host "=================================================="

# 检查网络连通性
Write-Host "`n[1/5] 检查网络连通性..."
if (-not (Test-Connection -ComputerName $Server -Count 1 -Quiet)) {
    Write-Host "错误: 无法连接到服务器 $Server"
    exit 1
}
Write-Host "OK: 服务器可达"

# 检查远程服务是否运行
Write-Host "`n[2/5] 检查服务状态..."
try {
    $response = Invoke-WebRequest -Uri "http://$Server`:8088/" -UseBasicParsing -TimeoutSec 5
    if ($response.StatusCode -eq 200) {
        Write-Host "OK: Web 服务正在运行"
    }
} catch {
    Write-Host "警告: 无法访问 Web 服务"
}

# 使用 net use 连接（如果需要）
Write-Host "`n[3/5] 准备文件传输..."
Write-Host "请确保你有服务器 $Server 的管理员权限"

# 需要更新的文件列表
$filesToUpdate = @(
    "code\web\server.py",
    "code\web\search_engine.py",
    "code\web\templates\index.html",
    "code\web\cache_manager.py",
    "code\requirements.txt",
    "SKILL.md"
)

Write-Host "`n[4/5] 需要更新的文件:"
foreach ($file in $filesToUpdate) {
    $localFile = Join-Path $LocalPath $file
    if (Test-Path $localFile) {
        Write-Host "  - $file (存在)"
    } else {
        Write-Host "  - $file (不存在!)"
    }
}

Write-Host "`n[5/5] 请手动执行以下步骤:"
Write-Host ""
Write-Host "方法一: 使用远程桌面"
Write-Host "  1. 远程桌面连接到 $Server"
Write-Host "  2. 停止 Web 服务（关闭 Python 进程或 Ctrl+C）"
Write-Host "  3. 复制以下文件到 $RemotePath:"
foreach ($file in $filesToUpdate) {
    Write-Host "     - $file"
}
Write-Host "  4. 重启服务: cd $RemotePath\code\web && python server.py"
Write-Host ""
Write-Host "方法二: 使用 SMB 共享"
Write-Host "  1. 打开 \\$Server\c$\troubleshoot\"
Write-Host "  2. 复制文件"
Write-Host "  3. 通过远程桌面或 taskkill 重启服务"
Write-Host ""
Write-Host "方法三: 使用 pscp/scp（如果已安装）"
Write-Host "  pscp -r code\web\server.py admin@${Server}:$RemotePath/code/web/"
Write-Host "  pscp -r code\web\search_engine.py admin@${Server}:$RemotePath/code/web/"
Write-Host "  pscp SKILL.md admin@${Server}:$RemotePath/"
Write-Host ""
Write-Host "=================================================="
Write-Host "文件准备完毕，请选择一种方法进行更新"
Write-Host "=================================================="