# Geutebruck API - Start All Services
# This script starts Redis, SDK Bridge, and FastAPI in separate windows

param(
    [switch]$SkipRedis,
    [switch]$SkipSdkBridge,
    [switch]$SkipApi
)

$ErrorActionPreference = "Stop"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Geutebruck API - Starting Services" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

$RepoRoot = Split-Path -Parent $PSScriptRoot

# Check if .env exists
if (-not (Test-Path "$RepoRoot\.env")) {
    Write-Host "ERROR: .env file not found!" -ForegroundColor Red
    Write-Host "Run: .\scripts\setup_dev_environment.ps1 first" -ForegroundColor Red
    exit 1
}

# Function to check if port is in use
function Test-Port {
    param([int]$Port)
    $tcpConnection = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
    return $null -ne $tcpConnection
}

# Start Redis
if (-not $SkipRedis) {
    Write-Host "[1/3] Starting Redis..." -ForegroundColor Yellow

    if (Test-Port 6379) {
        Write-Host "  ✓ Redis already running on port 6379" -ForegroundColor Green
    } else {
        $redisCmd = Get-Command redis-server -ErrorAction SilentlyContinue
        if ($redisCmd) {
            Start-Process -FilePath "redis-server" -WindowStyle Normal
            Start-Sleep -Seconds 2
            Write-Host "  ✓ Redis started" -ForegroundColor Green
        } else {
            Write-Host "  ✗ Redis not found. Install with: choco install redis-64" -ForegroundColor Red
        }
    }
} else {
    Write-Host "[1/3] Skipping Redis" -ForegroundColor Gray
}

# Start SDK Bridge
if (-not $SkipSdkBridge) {
    Write-Host "[2/3] Starting SDK Bridge (gRPC Service)..." -ForegroundColor Yellow

    $sdkBridgePath = "$RepoRoot\src\sdk-bridge\GeViScopeBridge"
    $sdkBridgeExe = "$sdkBridgePath\bin\Debug\net8.0\GeViScopeBridge.exe"

    if (Test-Path $sdkBridgeExe) {
        if (Test-Port 50051) {
            Write-Host "  ✓ SDK Bridge already running on port 50051" -ForegroundColor Green
        } else {
            $sdkBridgeTitle = "Geutebruck SDK Bridge"
            Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$sdkBridgePath'; dotnet run --configuration Debug" -WindowStyle Normal
            Start-Sleep -Seconds 3
            Write-Host "  ✓ SDK Bridge started on port 50051" -ForegroundColor Green
        }
    } else {
        Write-Host "  ⚠ SDK Bridge not built yet" -ForegroundColor Yellow
        Write-Host "  Run: cd $sdkBridgePath; dotnet build" -ForegroundColor Yellow
    }
} else {
    Write-Host "[2/3] Skipping SDK Bridge" -ForegroundColor Gray
}

# Start FastAPI
if (-not $SkipApi) {
    Write-Host "[3/3] Starting FastAPI Application..." -ForegroundColor Yellow

    $apiPath = "$RepoRoot\src\api"

    if (Test-Port 8000) {
        Write-Host "  ✓ API already running on port 8000" -ForegroundColor Green
    } else {
        # Check if virtual environment exists
        if (Test-Path "$RepoRoot\.venv\Scripts\Activate.ps1") {
            $apiTitle = "Geutebruck API"
            $startCommand = "cd '$apiPath'; & '$RepoRoot\.venv\Scripts\Activate.ps1'; uvicorn main:app --reload --host 0.0.0.0 --port 8000"
            Start-Process powershell -ArgumentList "-NoExit", "-Command", $startCommand -WindowStyle Normal
            Start-Sleep -Seconds 3
            Write-Host "  ✓ FastAPI started on http://localhost:8000" -ForegroundColor Green
        } else {
            Write-Host "  ✗ Python virtual environment not found" -ForegroundColor Red
            Write-Host "  Run: .\scripts\setup_dev_environment.ps1 first" -ForegroundColor Red
        }
    }
} else {
    Write-Host "[3/3] Skipping FastAPI" -ForegroundColor Gray
}

# Summary
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Services Status:" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Redis:      http://localhost:6379" -ForegroundColor White
Write-Host "SDK Bridge: http://localhost:50051 (gRPC)" -ForegroundColor White
Write-Host "API:        http://localhost:8000" -ForegroundColor White
Write-Host "API Docs:   http://localhost:8000/docs" -ForegroundColor White
Write-Host ""
Write-Host "All Services Started! 🚀" -ForegroundColor Green
Write-Host ""
Write-Host "Press Ctrl+C in each window to stop services" -ForegroundColor Yellow
