# RescuePC Protected Module - DO NOT EDIT
# Source code is maintained separately in the development repository.

function Get-WindowsUpdateProbes {
    [CmdletBinding()]
    param()

    $probes = [ordered]@{
        timestamp = (Get-Date).ToUniversalTime().ToString("o")
    }

    $wuServices = @('wuauserv', 'BITS', 'CryptSvc', 'msiserver', 'TrustedInstaller')
    $probes.services = [ordered]@{}
    foreach ($svc in $wuServices) {
        try {
            $service = Get-Service -Name $svc -ErrorAction SilentlyContinue
            if ($service) {
                $probes.services[$svc] = [ordered]@{
                    status = $service.Status.ToString()
                    startType = $service.StartType.ToString()
                }
            }
        } catch {}
    }

    try {
        $sdPath = "$env:SystemRoot\SoftwareDistribution"
        if (Test-Path $sdPath) {
            $size = (Get-ChildItem -Path $sdPath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
            $probes.softwareDistributionSizeMB = [math]::Round($size / 1MB, 2)
        }
    } catch { $probes.softwareDistributionSizeMB = -1 }

    try {
        $catPath = "$env:SystemRoot\System32\catroot2"
        if (Test-Path $catPath) {
            $size = (Get-ChildItem -Path $catPath -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
            $probes.catroot2SizeMB = [math]::Round($size / 1MB, 2)
        }
    } catch { $probes.catroot2SizeMB = -1 }

    try {
        $auSession = New-Object -ComObject Microsoft.Update.AutoUpdate -ErrorAction SilentlyContinue
        if ($auSession) {
            $probes.lastSearchTime = $auSession.Results.LastSearchSuccessDate
            $probes.lastInstallTime = $auSession.Results.LastInstallationSuccessDate
        }
    } catch { $probes.lastSearchTime = $null }

    try {
        $updateSession = New-Object -ComObject Microsoft.Update.Session -ErrorAction SilentlyContinue
        if ($updateSession) {
            $searcher = $updateSession.CreateUpdateSearcher()
            $searchResult = $searcher.Search("IsInstalled=0 and Type='Software'")
            $probes.pendingUpdatesCount = $searchResult.Updates.Count
        }
    } catch { $probes.pendingUpdatesCount = -1 }

    try {
        $wuDlls = @('wuapi.dll', 'wuaueng.dll', 'wuaueng1.dll', 'wucltui.dll', 'wups.dll', 'wups2.dll', 'wuweb.dll')
        $probes.registeredComponents = 0
        foreach ($dll in $wuDlls) {
            $dllPath = "$env:SystemRoot\System32\$dll"
            if (Test-Path $dllPath) {
                $probes.registeredComponents++
            }
        }
    } catch { $probes.registeredComponents = -1 }

    try {
        $wuLog = Get-WindowsUpdateLog -ErrorAction SilentlyContinue 2>$null
        $probes.wuLogAccessible = $true
    } catch { $probes.wuLogAccessible = $false }

    try {
        $wuRegPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate"
        if (Test-Path $wuRegPath) {
            $probes.wuRegistryExists = $true
        } else {
            $probes.wuRegistryExists = $false
        }
    } catch { $probes.wuRegistryExists = $null }

    return $probes
}

function Compare-WindowsUpdateProbes {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [hashtable]$Before,

        [Parameter(Mandatory)]
        [hashtable]$After
    )

    $changes = @()

    if ($Before.services -and $After.services) {
        foreach ($svc in $Before.services.Keys) {
            if ($After.services[$svc]) {
                if ($Before.services[$svc].status -ne $After.services[$svc].status) {
                    $changes += [ordered]@{
                        type = "service_status"
                        name = $svc
                        before = $Before.services[$svc].status
                        after = $After.services[$svc].status
                    }
                }
            }
        }
    }

    if ($Before.softwareDistributionSizeMB -gt 0 -and $After.softwareDistributionSizeMB -gt 0) {
        $diff = $Before.softwareDistributionSizeMB - $After.softwareDistributionSizeMB
        if ([math]::Abs($diff) -gt 10) {
            $changes += [ordered]@{
                type = "folder_size"
                name = "SoftwareDistribution"
                before = "$($Before.softwareDistributionSizeMB) MB"
                after = "$($After.softwareDistributionSizeMB) MB"
                freedMB = [math]::Round($diff, 2)
            }
        }
    }

    if ($Before.pendingUpdatesCount -ge 0 -and $After.pendingUpdatesCount -ge 0) {
        if ($Before.pendingUpdatesCount -ne $After.pendingUpdatesCount) {
            $changes += [ordered]@{
                type = "pending_updates"
                before = $Before.pendingUpdatesCount
                after = $After.pendingUpdatesCount
            }
        }
    }

    return $changes
}

Export-ModuleMember -Function @(
    'Get-WindowsUpdateProbes',
    'Compare-WindowsUpdateProbes'
)