#Requires -Version 7.4

function Get-vSEALGlobals {
    <#
    .SYNOPSIS
        Builds the globals hashtable passed to vSEAL plugins.

    .DESCRIPTION
        Constructs a comprehensive globals hashtable containing configuration,
        OS information, platform detection, and module metadata. This hashtable
        is passed to all plugins during sealing operations to provide consistent
        context about the execution environment.

    .PARAMETER Config
        The parsed vSEAL configuration hashtable from Import-vSEALConfiguration.

    .PARAMETER ConfigPath
        The absolute path to the configuration file being used.

    .EXAMPLE
        $globals = Get-vSEALGlobals -Config $config -ConfigPath 'C:\vSEAL\config.yaml'

        Builds the globals hashtable with OS and platform information.

    .OUTPUTS
        [hashtable] A hashtable containing:
        - PersistentDisk: Drive letter for persistent storage
        - AnalyzeMode: Boolean indicating analyze-only mode
        - ConfigPath: Path to the configuration file
        - vSEALVersion: Current module version
        - OS: Operating system information hashtable
        - Platform: Platform detection hashtable (Azure, hypervisor, etc.)

    .NOTES
        This is a private function used by Invoke-vSEAL.

    .LINK
        Get-vSEALOperatingSystemInfo
        Get-vSEALAzurePlatformData
        Get-HypervisorPlatformData
    #>
    [CmdletBinding()]
    [OutputType([hashtable])]
    param(
        [Parameter(Mandatory)]
        [hashtable]$Config,

        [Parameter(Mandatory)]
        [string]$ConfigPath
    )

    # Get module version from manifest
    $manifestPath = Join-Path -Path $PSScriptRoot -ChildPath '..\vSEAL.psd1'
    try {
        $manifest = Import-PowerShellDataFile -Path $manifestPath -ErrorAction Stop
        $moduleVersion = $manifest.ModuleVersion
    }
    catch {
        Write-vSEALLog "Failed to read module manifest at '$manifestPath': $($_.Exception.Message)" -Level 'WARNING'
        $moduleVersion = 'Unknown'
    }

    # Get OS info (uses existing public function)
    $osInfo = Get-vSEALOperatingSystemInfo

    # Get Azure platform data (uses existing public function)
    $azureData = Get-vSEALAzurePlatformData

    # Get hypervisor platform data (uses existing private function)
    $hypervisorData = Get-HypervisorPlatformData

    # Safely access optional config sections with null-coalescing
    $persistentDisk = $Config?.storage?.persistent_disk ?? $null

    $globals = @{
        # From config (null-safe access for optional sections)
        PersistentDisk = $persistentDisk

        # Runtime (set by Invoke-vSEAL)
        AnalyzeMode    = $false
        ConfigPath     = $ConfigPath

        # Module
        vSEALVersion   = $moduleVersion

        # OS info (from Get-vSEALOperatingSystemInfo)
        OS = @{
            Name           = $osInfo.Name
            Edition        = $osInfo.Edition
            Version        = $osInfo.Version
            Build          = $osInfo.Version.Build
            ReleaseId      = $osInfo.ReleaseId
            IsServer       = $osInfo.IsServer
            IsWorkstation  = $osInfo.IsWorkstation
            IsMultiSession = $osInfo.IsWorkstationEnterpriseMultiSessionOS
        }

        # Platform detection
        Platform = @{
            IsAzure         = $null -ne $azureData
            IsAVD           = $false  # TODO: implement AVD detection
            IsAVDRegistered = $false
            AzureMetadata   = $azureData

            # Hypervisor detection (from Get-HypervisorPlatformData)
            IsVMware     = $hypervisorData.IsVMware
            IsHyperV     = $hypervisorData.IsHyperV
            IsXenServer  = $hypervisorData.IsXenServer
            IsNutanixAHV = $hypervisorData.IsNutanixAHV
            Hypervisor   = $hypervisorData.Hypervisor
        }
    }

    return $globals
}
