#Requires -Version 7.4

function Test-OSVersionPrerequisite {
    <#
    .SYNOPSIS
        Validates that the OS is Windows 11 (Build >= 22000) or Windows Server 2022+.

    .DESCRIPTION
        Checks the operating system version to ensure compatibility with vSEAL.
        Supports Windows 11 (build 22000+) and Windows Server 2022 (build 20348+).
        This check is only evaluated when running in a Windows environment.

    .EXAMPLE
        Test-OSVersionPrerequisite

        Returns a result object indicating whether the OS version is supported.

    .OUTPUTS
        [PSCustomObject] with Name, Passed, Message, Required, Current, and HelpUrl properties.
    #>
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param()

    # Define minimum builds
    $windows11MinBuild = 22000      # Windows 11 21H2
    $server2022MinBuild = 20348     # Windows Server 2022

    # Initialize result values
    $passed = $false
    $currentValue = 'Unknown'
    $message = ''

    # Check if we're on Windows
    if ($env:OS -ne 'Windows_NT') {
        # Not Windows - this check is not applicable (will be caught by WindowsEnvironment check)
        return [PSCustomObject]@{
            Name     = 'OS Version'
            Passed   = $false
            Message  = 'Not running on Windows. vSEAL requires Windows 11 or Windows Server 2022+.'
            Required = 'Windows 11 (Build 22000+) or Windows Server 2022+'
            Current  = "Non-Windows environment: $([System.Runtime.InteropServices.RuntimeInformation]::OSDescription)"
            HelpUrl  = 'https://learn.microsoft.com/en-us/windows/release-health/'
        }
    }

    try {
        # Get OS information using CIM (works on Windows)
        # Timeout prevents indefinite hang if WMI becomes unresponsive
        $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem -OperationTimeoutSec 10 -ErrorAction Stop
        $buildNumber = [int]$osInfo.BuildNumber
        $caption = $osInfo.Caption
        $version = $osInfo.Version

        $currentValue = "$caption (Build $buildNumber)"

        # Determine if this is Server or Client OS
        $productType = $osInfo.ProductType
        # ProductType: 1 = Workstation, 2 = Domain Controller, 3 = Server

        if ($productType -eq 1) {
            # Workstation (Windows 10/11)
            $passed = $buildNumber -ge $windows11MinBuild
            $message = if ($passed) {
                "Windows 11 detected (Build $buildNumber). OS version requirement met."
            }
            else {
                "Windows 11 (Build $windows11MinBuild+) required, found Build $buildNumber. Upgrade to Windows 11."
            }
        }
        else {
            # Server OS
            $passed = $buildNumber -ge $server2022MinBuild
            $message = if ($passed) {
                "Windows Server 2022+ detected (Build $buildNumber). OS version requirement met."
            }
            else {
                "Windows Server 2022+ (Build $server2022MinBuild+) required, found Build $buildNumber. Upgrade to Windows Server 2022."
            }
        }
    }
    catch {
        $message = "Failed to determine OS version: $($_.Exception.Message)"
        $currentValue = 'Unable to detect'
    }

    [PSCustomObject]@{
        Name     = 'OS Version'
        Passed   = $passed
        Message  = $message
        Required = 'Windows 11 (Build 22000+) or Windows Server 2022+ (Build 20348+)'
        Current  = $currentValue
        HelpUrl  = 'https://learn.microsoft.com/en-us/windows/release-health/'
    }
}
