<#
.SYNOPSIS
    Phase 3.1 Step 5: Post-domain-join verification.
.DESCRIPTION
    Verifies GPO application, drive mappings, printer deployment, and network
    connectivity after a workstation has been joined to cascades.local.
    Run on the joined machine after reboot, logged in with a domain account.
#>

Write-Host "=== Phase 3: Post-Join Verification - $env:COMPUTERNAME ===" -ForegroundColor Cyan
Write-Host "Logged in as: $env:USERDOMAIN\$env:USERNAME"
Write-Host ""

$issues = @()

# --- Domain membership ---
Write-Host "--- Domain Membership ---" -ForegroundColor Yellow

$cs = Get-WmiObject Win32_ComputerSystem
if ($cs.PartOfDomain) {
    Write-Host "  [OK] Domain: $($cs.Domain)" -ForegroundColor Green
} else {
    Write-Host "  [FAIL] Not joined to domain!" -ForegroundColor Red
    $issues += "Not domain-joined"
}

# --- DC locator ---
Write-Host "`n--- Domain Controller ---" -ForegroundColor Yellow

$nltest = nltest /dsgetdc:cascades.local 2>&1
if ($LASTEXITCODE -eq 0) {
    $dcLine = $nltest | Select-String "DC:"
    Write-Host "  [OK] DC found: $dcLine" -ForegroundColor Green
} else {
    Write-Host "  [FAIL] Cannot locate domain controller" -ForegroundColor Red
    $issues += "Cannot locate DC"
}

# --- GPO ---
Write-Host "`n--- Group Policy ---" -ForegroundColor Yellow

$gpresult = gpresult /r 2>&1
$appliedGPOs = $gpresult | Select-String "CSC -"
if ($appliedGPOs) {
    foreach ($gpo in $appliedGPOs) {
        Write-Host "  [OK] Applied: $($gpo.Line.Trim())" -ForegroundColor Green
    }
} else {
    Write-Host "  [WARN] No CSC GPOs detected - may need gpupdate /force" -ForegroundColor Yellow
    $issues += "No CSC GPOs applied"
}

# --- Drive Mappings ---
Write-Host "`n--- Drive Mappings ---" -ForegroundColor Yellow

$expectedDrives = @("S:")
$mappedDrives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.DisplayRoot -like "\\*" }

foreach ($d in $mappedDrives) {
    Write-Host "  [OK] $($d.Name): -> $($d.DisplayRoot)" -ForegroundColor Green
}

if (-not $mappedDrives) {
    Write-Host "  [WARN] No mapped drives found - check GPO and logoff/logon" -ForegroundColor Yellow
    $issues += "No mapped drives"
}

# SMB access test
try {
    $testPath = Test-Path "\\192.168.2.254\Shares" -ErrorAction Stop
    if ($testPath) {
        Write-Host "  [OK] \\CS-SERVER\Shares accessible" -ForegroundColor Green
    } else {
        Write-Host "  [FAIL] \\CS-SERVER\Shares not accessible" -ForegroundColor Red
        $issues += "Cannot access \\CS-SERVER\Shares"
    }
}
catch {
    Write-Host "  [FAIL] SMB access error: $_" -ForegroundColor Red
    $issues += "SMB access error"
}

# --- Printers ---
Write-Host "`n--- Printers ---" -ForegroundColor Yellow

$printers = Get-Printer -ErrorAction SilentlyContinue
$networkPrinters = $printers | Where-Object { $_.Type -eq "Connection" }

if ($networkPrinters) {
    foreach ($p in $networkPrinters) {
        Write-Host "  [OK] $($p.Name) ($($p.PortName))" -ForegroundColor Green
    }
} else {
    Write-Host "  [WARN] No network printers deployed - check GPO" -ForegroundColor Yellow
    $issues += "No network printers"
}

# --- Network ---
Write-Host "`n--- Network Connectivity ---" -ForegroundColor Yellow

# Internet
$internet = Test-Connection -ComputerName "8.8.8.8" -Count 1 -Quiet -ErrorAction SilentlyContinue
if ($internet) {
    Write-Host "  [OK] Internet: working" -ForegroundColor Green
} else {
    Write-Host "  [FAIL] Internet: NOT working" -ForegroundColor Red
    $issues += "No internet"
}

# DNS
try {
    $dns = Resolve-DnsName "cs-server.cascades.local" -ErrorAction Stop
    Write-Host "  [OK] DNS: cs-server.cascades.local -> $($dns.IPAddress -join ', ')" -ForegroundColor Green
}
catch {
    Write-Host "  [FAIL] DNS: cannot resolve cs-server.cascades.local" -ForegroundColor Red
    $issues += "DNS resolution failed"
}

# Ping DC
$ping = Test-Connection -ComputerName "192.168.2.254" -Count 1 -Quiet -ErrorAction SilentlyContinue
if ($ping) {
    Write-Host "  [OK] Ping CS-SERVER: reachable" -ForegroundColor Green
} else {
    Write-Host "  [WARN] Ping CS-SERVER: no response (ICMP may be filtered)" -ForegroundColor Yellow
}

# --- Summary ---
Write-Host "`n========================================" -ForegroundColor Cyan
if ($issues.Count -eq 0) {
    Write-Host "ALL CHECKS PASSED" -ForegroundColor Green
} else {
    Write-Host "ISSUES FOUND ($($issues.Count)):" -ForegroundColor Red
    foreach ($i in $issues) {
        Write-Host "  - $i" -ForegroundColor Red
    }
    Write-Host "`nTroubleshooting:" -ForegroundColor Yellow
    Write-Host "  - Run: gpupdate /force" -ForegroundColor Yellow
    Write-Host "  - Log off and log on again (for user-level GPOs)" -ForegroundColor Yellow
    Write-Host "  - Check: gpresult /r (for GPO details)" -ForegroundColor Yellow
}
Write-Host "========================================" -ForegroundColor Cyan
