param(
    [string]$ServicePath = "src\TodoList.Api",
    [string]$WebPath = "src\TodoList.Web",
    [string]$MauiPath = "src\TodoList.Maui",
    [switch]$StartMaui = $true
)

$ErrorActionPreference = "Stop"

Write-Host "====================================" -ForegroundColor Cyan
Write-Host "  TodoList 服务启动脚本" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""

$apiProjectPath = Join-Path $PSScriptRoot $ServicePath
$webProjectPath = Join-Path $PSScriptRoot $WebPath
$mauiProjectPath = Join-Path $PSScriptRoot $MauiPath

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

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

if ($apiProcess) {
    Write-Host "⚠️  TodoList.Api 服务已经在运行中" -ForegroundColor Yellow
    Write-Host "   进程 ID: ($apiProcess.Id)" -ForegroundColor Gray
    Write-Host "   如果需要重新启动，请先运行 stop-service.bat" -ForegroundColor Gray
    Write-Host ""
    
    if ($StartMaui) {
        Write-Host "[2/3] 启动 TodoList.Maui 应用..." -ForegroundColor Yellow
        
        $mauiPath = Join-Path $mauiProjectPath "bin\Debug\net10.0-windows10.0.19041.0\win-x64\TodoList.Maui.exe"
        
        if (Test-Path $mauiPath) {
            Write-Host "🚀 启动 TodoList.Maui..." -ForegroundColor Green
            Start-Process -FilePath $mauiPath -WorkingDirectory $mauiProjectPath
            Write-Host "✅ TodoList.Maui 已启动" -ForegroundColor Green
        } else {
            Write-Host "❌ TodoList.Maui 可执行文件不存在" -ForegroundColor Red
            Write-Host "   路径: $mauiPath" -ForegroundColor Red
            Write-Host "   请先构建项目: dotnet build src\TodoList.Maui\TodoList.Maui.csproj" -ForegroundColor Red
        }
    }
    
    Write-Host ""
    Write-Host "====================================" -ForegroundColor Cyan
    Write-Host "  启动完成" -ForegroundColor Green
    Write-Host "====================================" -ForegroundColor Cyan
    exit 0
}

Write-Host "✓ TodoList.Api 服务未运行" -ForegroundColor Green
Write-Host ""

Write-Host "[2/3] 启动 TodoList.Api 服务..." -ForegroundColor Yellow

if (!(Test-Path $apiProjectPath)) {
    Write-Host "❌ API 项目路径不存在: $apiProjectPath" -ForegroundColor Red
    exit 1
}

Write-Host "📂 工作目录: $apiProjectPath" -ForegroundColor Gray
Write-Host "🚀 启动服务..." -ForegroundColor Green

$apiProcess = Start-Process -FilePath "dotnet" -ArgumentList "run" -WorkingDirectory $apiProjectPath -PassThru

Write-Host "✅ TodoList.Api 服务已启动" -ForegroundColor Green
Write-Host "   进程 ID: ($apiProcess.Id)" -ForegroundColor Gray
Write-Host "   访问地址: http://localhost:5057" -ForegroundColor Cyan
Write-Host "   Swagger 文档: http://localhost:5057/swagger" -ForegroundColor Cyan
Write-Host ""

Write-Host "[3/3] 启动 TodoList.Web 服务..." -ForegroundColor Yellow

$webProcess = Get-Process -Name "node" -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle -like "*vite*" -or $_.Path -like "*TodoList.Web*" }

if ($webProcess) {
    Write-Host "⚠️  TodoList.Web 服务已经在运行中" -ForegroundColor Yellow
    Write-Host "   进程 ID: ($webProcess.Id)" -ForegroundColor Gray
    Write-Host "   如果需要重新启动，请先运行 stop-service.bat" -ForegroundColor Gray
    Write-Host ""
} else {
    Write-Host "📂 工作目录: $webProjectPath" -ForegroundColor Gray
    
    if (!(Test-Path $webProjectPath)) {
        Write-Host "❌ Web 项目路径不存在: $webProjectPath" -ForegroundColor Red
        exit 1
    }
    
    Write-Host "🚀 启动 Web 服务..." -ForegroundColor Green
    
    $webProcess = Start-Process -FilePath "npm.cmd" -ArgumentList "run", "dev" -WorkingDirectory $webProjectPath -PassThru
    
    Write-Host "✅ TodoList.Web 服务已启动" -ForegroundColor Green
    Write-Host "   进程 ID: ($webProcess.Id)" -ForegroundColor Gray
    Write-Host "   访问地址: http://localhost:5173" -ForegroundColor Cyan
    Write-Host ""
}

if ($StartMaui) {
    Write-Host ""
    Write-Host "[4/4] 启动 TodoList.Maui 应用..." -ForegroundColor Yellow
    
    Start-Sleep -Seconds 2
    
    $mauiPath = Join-Path $mauiProjectPath "bin\Debug\net10.0-windows10.0.19041.0\win-x64\TodoList.Maui.exe"
    
    if (Test-Path $mauiPath) {
        Write-Host "🚀 启动 TodoList.Maui..." -ForegroundColor Green
        Start-Process -FilePath $mauiPath -WorkingDirectory $mauiProjectPath
        Write-Host "✅ TodoList.Maui 已启动" -ForegroundColor Green
    } else {
        Write-Host "❌ TodoList.Maui 可执行文件不存在" -ForegroundColor Red
        Write-Host "   路径: $mauiPath" -ForegroundColor Red
        Write-Host "   请先构建项目: dotnet build src\TodoList.Maui\TodoList.Maui.csproj" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "====================================" -ForegroundColor Cyan
Write-Host "  启动完成" -ForegroundColor Green
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "💡 提示:" -ForegroundColor Yellow
Write-Host "   - 按 Ctrl+C 可以停止服务" -ForegroundColor Gray
Write-Host "   - 运行 stop-service.bat 可以关闭服务" -ForegroundColor Gray
Write-Host "   - 运行 restart-service.bat 可以重启服务" -ForegroundColor Gray
Write-Host ""