param(
    [switch]$Force = $false
)

$ErrorActionPreference = "Stop"

Write-Host "====================================" -ForegroundColor Cyan
Write-Host "  TodoList 服务关闭脚本" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""

$stoppedProcesses = @()

Write-Host "[1/3] 查找 TodoList.Api 服务..." -ForegroundColor Yellow

$apiProcesses = Get-Process -Name "dotnet" -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle -like "*TodoList.Api*" }

if ($apiProcesses) {
    Write-Host "🔍 找到 ($apiProcesses.Count) 个 TodoList.Api 进程" -ForegroundColor Yellow
    
    foreach ($process in $apiProcesses) {
        Write-Host "   正在停止进程 ID: ($process.Id)..." -ForegroundColor Gray
        try {
            Stop-Process -Id $process.Id -Force:$Force -ErrorAction Stop
            Write-Host "   ✅ 进程 ($process.Id) 已停止" -ForegroundColor Green
            $stoppedProcesses += $process
        } catch {
            Write-Host "   ❌ 停止进程 ($process.Id) 失败: $_" -ForegroundColor Red
        }
    }
} else {
    Write-Host "✓ TodoList.Api 服务未运行" -ForegroundColor Green
}

Write-Host ""
Write-Host "[2/3] 查找 TodoList.Web 服务..." -ForegroundColor Yellow

$webProcesses = Get-WmiObject Win32_Process -Filter "Name='node.exe'" | Where-Object { $_.CommandLine -like "*vite*" -or $_.CommandLine -like "*TodoList.Web*" }

if ($webProcesses) {
    $count = @($webProcesses).Count
    Write-Host "🔍 找到 ($count) 个 TodoList.Web 进程" -ForegroundColor Yellow
    
    foreach ($process in $webProcesses) {
        $processId = $process.ProcessId
        Write-Host "   正在停止进程 ID: ($processId)..." -ForegroundColor Gray
        try {
            Stop-Process -Id $processId -Force:$Force -ErrorAction Stop
            Write-Host "   ✅ 进程 ($processId) 已停止" -ForegroundColor Green
            $stoppedProcesses += $process
        } catch {
            Write-Host "   ❌ 停止进程 ($processId) 失败: $_" -ForegroundColor Red
        }
    }
} else {
    Write-Host "✓ TodoList.Web 服务未运行" -ForegroundColor Green
}

Write-Host ""
Write-Host "[3/3] 查找 TodoList.Maui 应用..." -ForegroundColor Yellow

$mauiProcesses = Get-Process -Name "TodoList.Maui" -ErrorAction SilentlyContinue

if ($mauiProcesses) {
    Write-Host "🔍 找到 ($mauiProcesses.Count) 个 TodoList.Maui 进程" -ForegroundColor Yellow
    
    foreach ($process in $mauiProcesses) {
        Write-Host "   正在停止进程 ID: ($process.Id)..." -ForegroundColor Gray
        try {
            Stop-Process -Id $process.Id -Force:$Force -ErrorAction Stop
            Write-Host "   ✅ 进程 ($process.Id) 已停止" -ForegroundColor Green
            $stoppedProcesses += $process
        } catch {
            Write-Host "   ❌ 停止进程 ($process.Id) 失败: $_" -ForegroundColor Red
        }
    }
} else {
    Write-Host "✓ TodoList.Maui 应用未运行" -ForegroundColor Green
}

Write-Host ""
Write-Host "====================================" -ForegroundColor Cyan

$stoppedCount = @($stoppedProcesses).Count
if ($stoppedCount -gt 0) {
    Write-Host "  关闭完成" -ForegroundColor Green
    Write-Host "  已停止 ($stoppedCount) 个进程" -ForegroundColor Green
} else {
    Write-Host "  没有需要停止的进程" -ForegroundColor Yellow
}

Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "💡 提示:" -ForegroundColor Yellow
Write-Host "   - 运行 start-service.bat 可以启动服务" -ForegroundColor Gray
Write-Host "   - 运行 restart-service.bat 可以重启服务" -ForegroundColor Gray
Write-Host ""