#Requires -RunAsAdministrator
<#
.SYNOPSIS
    Phase 2.4: Set up print server on CS-SERVER.
.DESCRIPTION
    Creates TCP/IP printer ports for all managed printers.
    Drivers must be installed manually before uncommenting the Add-Printer lines.
    Run on CS-SERVER via ScreenConnect.
.NOTES
    Download drivers from manufacturer websites FIRST:
    - Epson ET-5800: https://epson.com/Support/Printers/All-In-Ones/ET-Series/Epson-ET-5800/s/SPT_C11CJ30201
    - Canon MF455DW: https://www.usa.canon.com/support/p/imageclass-mf455dw
    - Canon MF451CDW: https://www.usa.canon.com/support/p/imageclass-mf451dw
    - Brother MFC-L8900CDW: https://www.brother-usa.com/support/mfcl8900cdw
    - Konica Minolta Bizhub C368: https://www.konicaminolta.us/en-us/support/download-centre
    Install drivers on CS-SERVER, then uncomment the Add-Printer section below.
#>

Write-Host "=== Phase 2.4: Print Server Setup ===" -ForegroundColor Cyan
Write-Host ""

# --- Ensure Print Server feature is installed ---
Write-Host "--- Checking Print Server role ---" -ForegroundColor Yellow
$printFeature = Get-WindowsFeature -Name Print-Server -ErrorAction SilentlyContinue
if ($printFeature -and -not $printFeature.Installed) {
    Install-WindowsFeature -Name Print-Server -IncludeManagementTools
    Write-Host "  [OK] Print Server role installed" -ForegroundColor Green
} else {
    Write-Host "  [OK] Print Server role already installed" -ForegroundColor Green
}

# --- Define printers ---
$printers = @(
    @{ Name = "Front Desk - Epson ET-5800";         IP = "192.168.2.147";  ShareName = "FrontDesk-Epson";    Driver = "EPSON ET-5800 Series" }
    @{ Name = "Business Office - Canon MF455DW";     IP = "192.168.3.227";  ShareName = "BizOffice-Canon";   Driver = "Canon Generic Plus UFR II" }
    @{ Name = "Marketing - Brother MFC-L8900CDW";    IP = "192.168.2.21";   ShareName = "Marketing-Brother"; Driver = "Brother MFC-L8900CDW series" }
    @{ Name = "206 Health - Bizhub C368";            IP = "192.168.1.138";  ShareName = "Health206-Bizhub";   Driver = "KONICA MINOLTA Universal PCL" }
    @{ Name = "206 Nurse Station - Brother MFC-L8900CDW"; IP = "10.0.20.69"; ShareName = "Health206-Brother"; Driver = "Brother MFC-L8900CDW series" }
    @{ Name = "MemCare MedTech - Brother (model TBD)"; IP = "192.168.2.53"; ShareName = "MemCare-Brother"; Driver = "TBD" }
    @{ Name = "MemCare Director - Canon MF451CDW";   IP = "192.168.3.52";   ShareName = "MemDir-Canon";      Driver = "Canon Generic Plus UFR II" }
    @{ Name = "Kitchen Printer";                     IP = "192.168.0.121";  ShareName = "Kitchen";           Driver = "TBD" }
)

# --- Create TCP/IP printer ports ---
Write-Host "`n--- Creating Printer Ports ---" -ForegroundColor Yellow

foreach ($p in $printers) {
    $portName = "TCP_$($p.IP)"
    try {
        $existing = Get-PrinterPort -Name $portName -ErrorAction SilentlyContinue
        if (-not $existing) {
            Add-PrinterPort -Name $portName -PrinterHostAddress $p.IP
            Write-Host "  [OK] Created port: $portName ($($p.Name))" -ForegroundColor Green
        } else {
            Write-Host "  [SKIP] Port $portName already exists" -ForegroundColor DarkGray
        }
    }
    catch {
        Write-Host "  [ERROR] Failed to create port $portName : $_" -ForegroundColor Red
    }
}

# --- Test connectivity to each printer ---
Write-Host "`n--- Testing Printer Connectivity ---" -ForegroundColor Yellow

foreach ($p in $printers) {
    $result = Test-Connection -ComputerName $p.IP -Count 1 -Quiet -ErrorAction SilentlyContinue
    if ($result) {
        Write-Host "  [OK] $($p.Name) ($($p.IP)) - reachable" -ForegroundColor Green
    } else {
        Write-Host "  [WARN] $($p.Name) ($($p.IP)) - NOT reachable" -ForegroundColor Yellow
    }
}

# --- Add shared printers (UNCOMMENT after installing drivers) ---
<#
Write-Host "`n--- Creating Shared Printers ---" -ForegroundColor Yellow

foreach ($p in $printers) {
    if ($p.Driver -eq "TBD") {
        Write-Host "  [SKIP] $($p.Name) - driver not specified" -ForegroundColor Yellow
        continue
    }

    $portName = "TCP_$($p.IP)"
    try {
        $existing = Get-Printer -Name $p.Name -ErrorAction SilentlyContinue
        if (-not $existing) {
            Add-Printer -Name $p.Name -DriverName $p.Driver -PortName $portName -Shared -ShareName $p.ShareName -Published
            Write-Host "  [OK] Created printer: $($p.Name) (\\CS-SERVER\$($p.ShareName))" -ForegroundColor Green
        } else {
            Write-Host "  [SKIP] Printer $($p.Name) already exists" -ForegroundColor DarkGray
        }
    }
    catch {
        Write-Host "  [ERROR] Failed to create $($p.Name): $_" -ForegroundColor Red
        Write-Host "    Verify driver '$($p.Driver)' is installed: Get-PrinterDriver" -ForegroundColor Yellow
    }
}
#>

# --- Summary ---
Write-Host "`n=== Print Server Summary ===" -ForegroundColor Cyan

Write-Host "`nPrinter Ports:" -ForegroundColor Yellow
Get-PrinterPort | Where-Object { $_.Name -like "TCP_*" } |
    Select-Object Name, PrinterHostAddress |
    Format-Table -AutoSize

Write-Host "Installed Printer Drivers:" -ForegroundColor Yellow
Get-PrinterDriver | Select-Object Name | Format-Table -AutoSize

Write-Host "Shared Printers:" -ForegroundColor Yellow
Get-Printer | Where-Object { $_.Shared } |
    Select-Object Name, PortName, ShareName, DriverName |
    Format-Table -AutoSize

Write-Host "`n=== Print Server Setup Complete ===" -ForegroundColor Cyan
Write-Host "Next steps:" -ForegroundColor Green
Write-Host "  1. Download and install printer drivers (see .NOTES in script header)"
Write-Host "  2. Uncomment the 'Add shared printers' section and re-run"
Write-Host "  3. Print a test page from CS-SERVER to each printer"
Write-Host "  4. Create GPOs in GPMC (see phase2-server-prep.md section 2.5)"
