# Update MFA phone number for Lesley Roth @ BG Builders
$ErrorActionPreference = "Stop"
$lesleyUPN = "lesley@bgbuildersllc.com"
$newPhone = "+1 4804954511"
$tenantId = "ededa4fb-f6eb-4398-851d-5eb3e11fab27"

Import-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Users

Connect-MgGraph -TenantId $tenantId -Scopes 'UserAuthenticationMethod.ReadWrite.All','User.ReadWrite.All' -NoWelcome

Write-Output "=== Current Auth Methods for Lesley ==="
$methods = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$lesleyUPN/authentication/phoneMethods"
if ($methods.value.Count -gt 0) {
    foreach ($m in $methods.value) {
        Write-Output "  ID: $($m.id) | Type: $($m.phoneType) | Number: $($m.phoneNumber)"
    }
} else {
    Write-Output "  No phone methods registered"
}

Write-Output "`n=== Updating MFA Phone ==="
# Phone method ID for mobile is always "3179e48a-750b-4051-897c-87b9720928f7"
$mobileMethodId = "3179e48a-750b-4051-897c-87b9720928f7"

try {
    # Try to update existing mobile phone method
    Invoke-MgGraphRequest -Method PUT -Uri "https://graph.microsoft.com/v1.0/users/$lesleyUPN/authentication/phoneMethods/$mobileMethodId" -Body @{
        phoneNumber = $newPhone
        phoneType = "mobile"
    }
    Write-Output "[OK] Mobile phone updated to $newPhone"
} catch {
    Write-Output "[INFO] PUT failed, trying POST to create new method..."
    try {
        Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users/$lesleyUPN/authentication/phoneMethods" -Body @{
            phoneNumber = $newPhone
            phoneType = "mobile"
        }
        Write-Output "[OK] Mobile phone created: $newPhone"
    } catch {
        Write-Output "[ERROR] Failed: $_"
    }
}

Write-Output "`n=== Verify Updated Methods ==="
$methods = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/users/$lesleyUPN/authentication/phoneMethods"
foreach ($m in $methods.value) {
    Write-Output "  ID: $($m.id) | Type: $($m.phoneType) | Number: $($m.phoneNumber)"
}

Disconnect-MgGraph
