param(
  [string]$HostBaseUrl = "http://localhost",
  [string]$BenchBaseUrl = "http://traefik",
  [string]$Network = "tapper-web",
  [int]$Requests = 1000,
  [int]$Concurrency = 50
)

$ErrorActionPreference = "Stop"

$suffix = [guid]::NewGuid().ToString("N").Substring(0, 12)
$body = @{
  username = "ab_$suffix"
  email = "ab_$suffix@loadtest.local"
  password = "LoadTest1234!"
} | ConvertTo-Json

Write-Host "[ab] Registering load-test user at $HostBaseUrl"
$register = Invoke-RestMethod `
  -Method Post `
  -Uri "$HostBaseUrl/api/auth/register" `
  -ContentType "application/json" `
  -Body $body

$token = $register.access_token
if (-not $token) {
  $token = $register.token
}
if (-not $token) {
  throw "Register response did not contain access_token"
}

Write-Host "[ab] Running ApacheBench: $Requests requests, concurrency $Concurrency"
Write-Host "[ab] Target: GET $BenchBaseUrl/api/profile"

docker run --rm --network $Network httpd:2.4-alpine `
  ab `
  -n $Requests `
  -c $Concurrency `
  -H "Authorization: Bearer $token" `
  "$BenchBaseUrl/api/profile"
