# 09-Setup-Display.ps1 -- Display pc-type setup (runs after Shopfloor baseline)
#
# Display = lobby display / dashboard kiosk PC. Kiosk app itself
# (LobbyDisplay or Dashboard) installs via preinstall.json (Install-
# KioskApp.cmd reads display-type.txt). No OpenText, no eDNC, no UDC.
#
# This script applies Edge kiosk-mode relaunch policies so the
# "An update is available - restart Edge" dialog auto-clears without
# requiring keyboard/mouse interaction (display bays have neither).
#
# Refs:
#   https://learn.microsoft.com/en-us/deployedge/microsoft-edge-browser-policies/relaunchnotification
#   https://learn.microsoft.com/en-us/deployedge/microsoft-edge-configure-kiosk-mode

$ErrorActionPreference = 'Continue'

Write-Host '=== Display Setup ==='

$pxeStatusLib = Join-Path $PSScriptRoot '..\Shopfloor\lib\Send-PxeStatus.ps1'
if (Test-Path $pxeStatusLib) {
    try { . $pxeStatusLib; Send-PxeStatus -Stage '09-Setup-Display: starting' -StageIndex 3 -StageTotal 8 } catch { }
}

# --- Edge relaunch policies (suppress update-prompt dialog on kiosks) ---
$edgePolicy = 'HKLM:\SOFTWARE\Policies\Microsoft\Edge'
if (-not (Test-Path -LiteralPath $edgePolicy)) {
    New-Item -Path $edgePolicy -Force | Out-Null
}

# RelaunchNotification = 2 (Required): Edge auto-restarts itself after the
# notification period. Display has no operator to click "dismiss" so this
# is the only mode that recovers without intervention.
New-ItemProperty -Path $edgePolicy -Name 'RelaunchNotification'        -Value 2        -PropertyType DWord -Force | Out-Null

# RelaunchNotificationPeriod (ms): time before forced auto-restart.
# 3600000 ms = 1 hour. Short window minimises how long the dialog is on
# screen but still gives an active session a chance to finish.
New-ItemProperty -Path $edgePolicy -Name 'RelaunchNotificationPeriod'  -Value 3600000  -PropertyType DWord -Force | Out-Null

# RelaunchHeadsUpPeriod (ms): final warning duration before auto-restart.
# 60000 ms = 1 min. Trims the visible warning to a minute before relaunch.
New-ItemProperty -Path $edgePolicy -Name 'RelaunchHeadsUpPeriod'       -Value 60000    -PropertyType DWord -Force | Out-Null

# RelaunchWindow: schedules the forced restart in an overnight window
# (02:00-04:00) so business-hour updates wait until off-hours, leaving the
# dialog effectively invisible during the day. JSON format per MS docs.
$relaunchWindow = '{"entries":[{"start":{"hour":2,"minute":0},"duration_mins":120}]}'
New-ItemProperty -Path $edgePolicy -Name 'RelaunchWindow'              -Value $relaunchWindow -PropertyType String -Force | Out-Null

Write-Host "  Edge RelaunchNotification=2 (Required, auto-restart)"
Write-Host "  Edge RelaunchNotificationPeriod=1h"
Write-Host "  Edge RelaunchHeadsUpPeriod=1m"
Write-Host "  Edge RelaunchWindow=02:00-04:00"

if (Get-Command Send-PxeStatus -ErrorAction SilentlyContinue) {
    Send-PxeStatus -Stage '09-Setup-Display: complete' -StageIndex 4 -StageTotal 8
}
Write-Host '=== Display Setup Complete ==='
