# Start Laravel so phones on your LAN (and adb reverse) can reach the API.
# Postman / Flutter base URL examples are printed when the server starts.

$ip = (
    Get-NetIPAddress -AddressFamily IPv4 |
    Where-Object {
        $_.InterfaceAlias -notmatch 'Loopback|VMware|VirtualBox|vEthernet|WSL' -and
        $_.IPAddress -notmatch '^169\.'
    } |
    Select-Object -First 1
).IPAddress

if (-not $ip) {
    Write-Host "Could not detect a LAN IP. Using 127.0.0.1 only." -ForegroundColor Yellow
    $ip = "127.0.0.1"
}

$publicIp = $null
try {
    $publicIp = (Invoke-RestMethod -Uri "https://api.ipify.org?format=json" -TimeoutSec 5).ip
} catch { }

Write-Host ""
Write-Host "Farm Manager API (dev)" -ForegroundColor Cyan
Write-Host "  PC / emulator:     http://127.0.0.1:8000/api/v1"
Write-Host "  Phone (same WiFi): http://${ip}:8000/api/v1"
Write-Host "    -> Flutter debug:  .\run_lan.ps1"
Write-Host "  Phone (USB):       http://127.0.0.1:8000/api/v1  (run connect-device.ps1 + run_dev.ps1)"
if ($publicIp) {
    Write-Host "  Remote client APK: http://${publicIp}:8000/api/v1"
    Write-Host "    -> Needs router port 8000 -> ${ip}:8000 + firewall rule" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Postman login: POST .../auth/login" -ForegroundColor Green
Write-Host "  Body: { `"email`": `"admin@farmmanager.com`", `"password`": `"Admin@123`" }"
Write-Host ""

Set-Location $PSScriptRoot
php artisan serve --host=0.0.0.0 --port=8000
