# Reset password for notifications@dataforth.com and set to never expire
# Using Microsoft Graph PowerShell (modern approach)

Write-Host "[OK] Resetting password for notifications@dataforth.com..." -ForegroundColor Green
Write-Host ""

# Check if Microsoft.Graph module is installed
if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Users)) {
    Write-Host "[WARNING] Microsoft.Graph.Users module not installed" -ForegroundColor Yellow
    Write-Host "          Installing now..." -ForegroundColor Yellow
    Install-Module Microsoft.Graph.Users -Scope CurrentUser -Force
}

# Connect to Microsoft Graph
Write-Host "[OK] Connecting to Microsoft Graph..." -ForegroundColor Green
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All" -TenantId "7dfa3ce8-c496-4b51-ab8d-bd3dcd78b584"

# Generate a strong random password
Add-Type -AssemblyName System.Web
$NewPassword = [System.Web.Security.Membership]::GeneratePassword(16, 4)

Write-Host "[OK] Generated new password: $NewPassword" -ForegroundColor Cyan
Write-Host "    SAVE THIS PASSWORD - you'll need it for the website config" -ForegroundColor Yellow
Write-Host ""

# Reset the password
$PasswordProfile = @{
    Password = $NewPassword
    ForceChangePasswordNextSignIn = $false
}

try {
    Update-MgUser -UserId "notifications@dataforth.com" -PasswordProfile $PasswordProfile
    Write-Host "[SUCCESS] Password reset successfully!" -ForegroundColor Green
} catch {
    Write-Host "[ERROR] Failed to reset password: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# Set password to never expire
Write-Host "[OK] Setting password to never expire..." -ForegroundColor Green

try {
    Update-MgUser -UserId "notifications@dataforth.com" -PasswordPolicies "DisablePasswordExpiration"
    Write-Host "[SUCCESS] Password set to never expire!" -ForegroundColor Green
} catch {
    Write-Host "[ERROR] Failed to set password policy: $($_.Exception.Message)" -ForegroundColor Red
}

# Verify the settings
Write-Host ""
Write-Host "================================================================"
Write-Host "Verifying Configuration"
Write-Host "================================================================"

$User = Get-MgUser -UserId "notifications@dataforth.com" -Property UserPrincipalName,PasswordPolicies,LastPasswordChangeDateTime

Write-Host "[OK] User: $($User.UserPrincipalName)"
Write-Host "    Password Policies: $($User.PasswordPolicies)"
Write-Host "    Last Password Change: $($User.LastPasswordChangeDateTime)"

if ($User.PasswordPolicies -contains "DisablePasswordExpiration") {
    Write-Host "    [OK] Password will never expire" -ForegroundColor Green
} else {
    Write-Host "    [WARNING] Password expiration policy not confirmed" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "================================================================"
Write-Host "NEXT STEPS"
Write-Host "================================================================"
Write-Host "1. Update the website SMTP configuration with:" -ForegroundColor Cyan
Write-Host "   - Username: notifications@dataforth.com"
Write-Host "   - Password: $NewPassword" -ForegroundColor Yellow
Write-Host ""
Write-Host "2. Test SMTP authentication:"
Write-Host "   D:\ClaudeTools\Test-DataforthSMTP.ps1"
Write-Host ""
Write-Host "3. Monitor for successful sends:"
Write-Host "   Get-MessageTrace -SenderAddress notifications@dataforth.com -StartDate (Get-Date).AddHours(-1)"
Write-Host ""

# Save credentials to a secure file for reference
$CredPath = "D:\ClaudeTools\dataforth-notifications-creds.txt"
@"
Dataforth Notifications Account Credentials
Generated: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")

Username: notifications@dataforth.com
Password: $NewPassword

SMTP Configuration for Website:
- Server: smtp.office365.com
- Port: 587
- TLS: Yes
- Username: notifications@dataforth.com
- Password: $NewPassword

DO NOT COMMIT TO GIT OR SHARE PUBLICLY
"@ | Out-File -FilePath $CredPath -Encoding UTF8

Write-Host "[OK] Credentials saved to: $CredPath" -ForegroundColor Green
Write-Host "    (Keep this file secure!)" -ForegroundColor Yellow

Disconnect-MgGraph
