param(
  [string]$BuildType = "Release", # Release or Debug
  [switch]$BuildFirst
)

$RepoRoot = $PSScriptRoot
$buildPath = Join-Path $RepoRoot "sojorn_app\build\windows\x64\runner\$BuildType"
$exePath = Join-Path $buildPath "sojorn_app.exe"

Write-Host "Sojorn Windows App Runner" -ForegroundColor Cyan
Write-Host "Build Type: $BuildType" -ForegroundColor White
Write-Host "Executable: $exePath" -ForegroundColor White

# Build first if requested
if ($BuildFirst) {
  Write-Host "Building first..." -ForegroundColor Yellow
  & "$PSScriptRoot\run_windows.ps1" -Release:($BuildType -eq "Release")
  if ($LASTEXITCODE -ne 0) {
    Write-Host "Build failed, cannot run app." -ForegroundColor Red
    exit 1
  }
}

# Check if executable exists
if (-not (Test-Path $exePath)) {
  Write-Host "Executable not found: $exePath" -ForegroundColor Red
  Write-Host "Try building first with: .\run_windows.ps1 -Release" -ForegroundColor Yellow
  exit 1
}

# Run the app
Write-Host "Starting Sojorn Windows app..." -ForegroundColor Green
Start-Process -FilePath $exePath -WorkingDirectory $buildPath

Write-Host "App launched! Check your taskbar or desktop for the Sojorn window." -ForegroundColor Cyan
