# USB: map the phone's localhost:8000 to this PC's port 8000.
# Required for Postman on the phone and for Flutter when BASE_URL uses 127.0.0.1.

$adb = "$env:LOCALAPPDATA\Android\Sdk\platform-tools\adb.exe"
if (-not (Test-Path $adb)) {
    $adb = "adb"
}

Write-Host "Checking device..." -ForegroundColor Cyan
& $adb devices

Write-Host ""
Write-Host "Forwarding phone 127.0.0.1:8000 -> PC :8000 ..." -ForegroundColor Cyan
& $adb reverse tcp:8000 tcp:8000

if ($LASTEXITCODE -ne 0) {
    Write-Host "Failed. Enable USB debugging and accept the RSA prompt on the phone." -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "OK. On the phone (Postman / app), use:" -ForegroundColor Green
Write-Host "  http://127.0.0.1:8000/api/v1"
Write-Host ""
Write-Host "Example: POST http://127.0.0.1:8000/api/v1/auth/login"
Write-Host "Start the API first: .\serve-dev.ps1"
