ODSPTroubleshooters.psm1

#Requires -Version 5.1

# Dot-source Private helpers first so [ODBLogProcessor] and New-CabinetFile
# are available when Public functions are loaded.
foreach ($file in (Get-ChildItem -Path "$PSScriptRoot\Private\*.ps1")) {
    . $file.FullName
}

foreach ($file in (Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1")) {
    . $file.FullName
}

# Auto-update: always download the latest version from PSGallery on every import,
& {
    $galleryPaths = @('\WindowsPowerShell\Modules\', '\PowerShell\Modules\')
    $isInstalledCopy = $galleryPaths | Where-Object { $PSScriptRoot -match [regex]::Escape($_) }
    if (-not $isInstalledCopy) { return }

    try {
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

        $moduleName  = 'ODSPTroubleshooters'
        $before      = (Get-Module -ListAvailable -Name $moduleName |
                            Sort-Object Version -Descending | Select-Object -First 1).Version

        Install-Module -Name $moduleName -Force -Scope CurrentUser -AllowClobber -ErrorAction Stop

        $after = (Get-Module -ListAvailable -Name $moduleName |
                      Sort-Object Version -Descending | Select-Object -First 1).Version

        if ($null -ne $after -and $after -gt $before) {
            Write-Host "[$moduleName] Updated from $before to $after. Please re-import the module." -ForegroundColor Cyan
        }
    }
    catch {
        # Network unavailable or PSGallery unreachable — continue silently.
    }
}