$ngrok = 'C:\Tools\ngrok\ngrok.exe'
$config = 'C:\Users\quantfortune\SIP\SIP_India\ngrok.yml'

if (-not (Test-Path $ngrok)) {
  Write-Host 'ngrok not found at C:\Tools\ngrok\ngrok.exe'
  exit 1
}

Start-Process -FilePath $ngrok -ArgumentList @('start', 'frontend', '--config', $config) | Out-Null
Start-Sleep -Seconds 3

$info = Invoke-RestMethod -Uri 'http://127.0.0.1:4040/api/tunnels'
$frontend = ($info.tunnels | Where-Object { $_.name -eq 'frontend' }).public_url
if (-not $frontend) {
  $frontend = ($info.tunnels | Select-Object -First 1).public_url
}
$backend = $frontend

if ($frontend) {
  Set-Content -Path 'C:\Users\quantfortune\SIP\SIP_India\ngrok_frontend_url.txt' -Value $frontend -Encoding ascii
}
if ($backend) {
  Set-Content -Path 'C:\Users\quantfortune\SIP\SIP_India\ngrok_backend_url.txt' -Value $backend -Encoding ascii
}

Write-Host "Frontend URL: $frontend"
Write-Host "Backend URL:  $backend"
