# Set-MachineNumber.ps1 - Update UDC + eDNC machine number on a Standard shopfloor PC
#
# Purpose:
#   Both UDC and eDNC use the same per-machine identifier ("Workstation Number" /
#   "Machine Number"). On Standard PCs imaged via PXE preinstall, both are installed
#   with a placeholder. When the PC is brought to its physical machine and assigned
#   a real number, this helper updates both apps in one step.
#
# Persistence locations updated:
#   1. UDC:  C:\ProgramData\UDC\udc_settings.json  (GeneralSettings.MachineNumber)
#   2. eDNC: HKLM:\SOFTWARE\WOW6432Node\GE Aircraft Engines\DNC\General\MachineNo
#
# After updating, kills any running UDC.exe and relaunches it with the new args
# so the in-memory state matches the persisted value.
#
# Run as SupportUser (admin). Requires write access to ProgramData and HKLM.

Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

. "$PSScriptRoot\..\Shopfloor\lib\Get-PCProfile.ps1"
. "$PSScriptRoot\..\Shopfloor\lib\Update-MachineNumber.ps1"

$site = if ($siteConfig) { $siteConfig.siteName } else { 'West Jefferson' }

# --- Read current values for display ---
$currentMN   = Get-CurrentMachineNumber
$currentUdc  = $currentMN.Udc
$currentEdnc = $currentMN.Ednc

# --- Show prompt with current state ---
$promptLines = @()
$promptLines += "Current UDC machine number:  $(if ($currentUdc)  { $currentUdc }  else { '(not set)' })"
$promptLines += "Current eDNC machine number: $(if ($currentEdnc) { $currentEdnc } else { '(not set)' })"
$promptLines += ""
$promptLines += "Enter the new Machine Number for this PC:"
$prompt = $promptLines -join "`n"

$new = [Microsoft.VisualBasic.Interaction]::InputBox($prompt, "Set Machine Number", "")

if ([string]::IsNullOrWhiteSpace($new)) {
    Write-Host "Cancelled."
    exit 0
}

$new = $new.Trim()

# --- Validate: digits only (loosen if you need alphanumerics) ---
if ($new -notmatch '^\d+$') {
    [System.Windows.Forms.MessageBox]::Show(
        "Machine number must be digits only.`n`nYou entered: '$new'",
        "Invalid Machine Number",
        [System.Windows.Forms.MessageBoxButtons]::OK,
        [System.Windows.Forms.MessageBoxIcon]::Error
    ) | Out-Null
    exit 1
}

$mnResult = Update-MachineNumber -NewNumber $new -Site $site

$results = @()
if ($mnResult.UdcUpdated) {
    Write-Host "UDC: $currentUdc -> $new"
    $results += "UDC updated to $new"
} elseif (-not (Test-Path 'C:\ProgramData\UDC\udc_settings.json')) {
    $results += "UDC: settings file missing (run UDC.exe once first)"
}
if ($mnResult.EdncUpdated) {
    Write-Host "eDNC: $currentEdnc -> $new"
    $results += "eDNC updated to $new"
} elseif (-not (Test-Path 'HKLM:\SOFTWARE\WOW6432Node\GE Aircraft Engines\DNC\General')) {
    $results += "eDNC: registry key missing (eDNC not installed?)"
}
foreach ($err in $mnResult.Errors) {
    Write-Warning $err
    $results += $err
}
if ($mnResult.UdcUpdated) { Write-Host "UDC.exe relaunched." }

# --- Show summary ---
$summary = ($results -join "`n") + "`n`nTo apply eDNC changes, restart any running DncMain.exe."
[System.Windows.Forms.MessageBox]::Show(
    $summary,
    "Set Machine Number - Done",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Information
) | Out-Null
