#Requires -Version 7.4

function Get-vSEALPendingReboot {
    <#
    .SYNOPSIS
        Gets pending reboot status for the local computer.

    .DESCRIPTION
        Checks multiple sources to determine if a reboot is pending:
        - Component Based Servicing
        - Windows Update
        - SCCM Client
        - Intune Management Extension
        - App-V
        - Pending file rename operations

    .EXAMPLE
        Get-vSEALPendingReboot

    .EXAMPLE
        if ((Get-vSEALPendingReboot).IsSystemRebootPending) {
            Write-Host "Reboot required!"
        }

    .EXAMPLE
        $reboot = Get-vSEALPendingReboot
        if ($reboot.ErrorMsg) {
            Write-Warning "Errors encountered: $($reboot.ErrorMsg -join '; ')"
        }

    .OUTPUTS
        [PSCustomObject] with reboot status properties including ErrorMsg array.
    #>
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param()

    $computerName = [System.Net.Dns]::GetHostName()
    Write-vSEALLog "Checking pending reboot status on [$computerName]..." -Level VERBOSE

    # Initialize error collection
    $errorMessages = [System.Collections.Generic.List[string]]::new()

    # Calculate boot time using TickCount64 (PowerShell 7 native)
    $lastBootUpTime = (Get-Date) - [TimeSpan]::FromMilliseconds([Environment]::TickCount64)

    # Component Based Servicing
    $isCBServicingRebootPending = $false
    try {
        $isCBServicingRebootPending = Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'
    }
    catch {
        $errorMessages.Add("CBS check failed: $($_.Exception.Message)")
    }

    # Windows Update
    $isWindowsUpdateRebootPending = $false
    try {
        $isWindowsUpdateRebootPending = Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
    }
    catch {
        $errorMessages.Add("Windows Update check failed: $($_.Exception.Message)")
    }

    # App-V
    $isAppVRebootPending = $false
    try {
        $isAppVRebootPending = Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\AppV\Client\PendingTasks'
    }
    catch {
        $errorMessages.Add("App-V check failed: $($_.Exception.Message)")
    }

    # Pending file rename operations
    $pendingFileRenameOperations = $null
    $isFileRenameRebootPending = $false
    try {
        $sessionManager = Get-ItemProperty -LiteralPath 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -ErrorAction SilentlyContinue
        if ($sessionManager.PendingFileRenameOperations) {
            $pendingFileRenameOperations = $sessionManager.PendingFileRenameOperations
            $isFileRenameRebootPending = $true
        }
    }
    catch {
        $errorMessages.Add("File rename check failed: $($_.Exception.Message)")
    }

    # SCCM Client
    $isSCCMClientRebootPending = $null
    try {
        $ccmNamespace = Get-CimInstance -Namespace root -ClassName __NAMESPACE -Filter "Name='ccm'" -ErrorAction SilentlyContinue
        if ($ccmNamespace) {
            $sccmStatus = Invoke-CimMethod -Namespace root/ccm/ClientSDK -ClassName CCM_ClientUtilities -MethodName DetermineIfRebootPending -ErrorAction SilentlyContinue
            if ($sccmStatus -and $sccmStatus.ReturnValue -eq 0) {
                $isSCCMClientRebootPending = $sccmStatus.IsHardRebootPending -or $sccmStatus.RebootPending
            }
        }
    }
    catch {
        $errorMessages.Add("SCCM check failed: $($_.Exception.Message)")
        Write-vSEALLog "Failed to query SCCM client: $_" -Level VERBOSE
    }

    # Intune Management Extension
    $isIntuneClientRebootPending = $false
    try {
        $isIntuneClientRebootPending = Test-Path -LiteralPath 'HKLM:\SOFTWARE\Microsoft\IntuneManagementExtension\RebootSettings\RebootFlag'
    }
    catch {
        $errorMessages.Add("Intune check failed: $($_.Exception.Message)")
    }

    # Aggregate system reboot pending
    $isSystemRebootPending = $isCBServicingRebootPending -or
                             $isWindowsUpdateRebootPending -or
                             $isFileRenameRebootPending -or
                             ($isSCCMClientRebootPending -eq $true)

    $result = [PSCustomObject]@{
        ComputerName                 = $computerName
        LastBootUpTime               = $lastBootUpTime
        IsSystemRebootPending        = $isSystemRebootPending
        IsCBServicingRebootPending   = $isCBServicingRebootPending
        IsWindowsUpdateRebootPending = $isWindowsUpdateRebootPending
        IsSCCMClientRebootPending    = $isSCCMClientRebootPending
        IsIntuneClientRebootPending  = $isIntuneClientRebootPending
        IsAppVRebootPending          = $isAppVRebootPending
        IsFileRenameRebootPending    = $isFileRenameRebootPending
        PendingFileRenameOperations  = $pendingFileRenameOperations
        ErrorMsg                     = if ($errorMessages.Count -gt 0) { $errorMessages.ToArray() } else { @() }
    }

    Write-vSEALLog "Pending reboot status: IsSystemRebootPending=$($result.IsSystemRebootPending)" -Level VERBOSE

    return $result
}
