param(
    [switch]$Force = $false,
    [switch]$StartMaui = $false
)

$ErrorActionPreference = "Stop"

Write-Host "====================================" -ForegroundColor Cyan
Write-Host "  Hua.Todo 三件套启动" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""

$pwsh = (Get-Command "pwsh" -ErrorAction SilentlyContinue)?.Source
if (-not $pwsh) {
    $pwsh = (Get-Command "powershell" -ErrorAction SilentlyContinue)?.Source
}

if (-not $pwsh) {
    Write-Host "❌ 未找到 PowerShell 可执行文件（pwsh/powershell）" -ForegroundColor Red
    exit 1
}

$forceArgs = @()
if ($Force) { $forceArgs += "-Force" }

Write-Host "[1/3] 启动后端 Host (5173)..." -ForegroundColor Yellow
Start-Process -FilePath $pwsh -ArgumentList @("-NoExit", "-File", (Join-Path $PSScriptRoot "start-host.ps1")) + $forceArgs | Out-Null

Write-Host "[2/3] 启动前端 Vite (5174)..." -ForegroundColor Yellow
Start-Process -FilePath $pwsh -ArgumentList @("-NoExit", "-File", (Join-Path $PSScriptRoot "start-forend.ps1")) + $forceArgs | Out-Null

Write-Host "[3/3] 启动 MAUI..." -ForegroundColor Yellow
if ($StartMaui) {
    Start-Process -FilePath $pwsh -ArgumentList @("-NoExit", "-File", (Join-Path $PSScriptRoot "start-maui.ps1"), "-SkipWebBuild") + $forceArgs | Out-Null
    Write-Host "✓ 已通过脚本启动 MAUI（跳过内置 Web 构建）" -ForegroundColor Green
} else {
    Write-Host "✓ 已启动 Host + Vite。请在 Visual Studio 中启动 Hua.Todo.Maui（F5）进行调试。" -ForegroundColor Green
}

Write-Host ""
Write-Host "💡 约定端口:" -ForegroundColor Yellow
Write-Host "   - Host API: http://localhost:5173" -ForegroundColor Cyan
Write-Host "   - Vite Dev: http://localhost:5174" -ForegroundColor Cyan
Write-Host ""
Write-Host "📝 提示:" -ForegroundColor Yellow
Write-Host "   - 使用 -Force 可强制重启已运行的进程" -ForegroundColor Gray
Write-Host "   - 使用 -StartMaui 也可用脚本拉起 MAUI（不等同于 VS 调试启动）" -ForegroundColor Gray
Write-Host ""

