#Requires -Version 7.4

function Read-vSEALStatus {
    <#
    .SYNOPSIS
        Reads vSEAL execution status from status.json.

    .DESCRIPTION
        Returns the vSEAL execution status object containing seal and engage sections,
        or $null if the status file does not exist or is corrupted. This function
        provides a safe read operation that never throws on missing or invalid data.

        When -PersistentDisk is specified, merges seal from the primary location (C:\ProgramData\vSEAL)
        with engage from the persistent disk location. This enables accurate status reporting on
        write-cache VDI where seal runs on master image but engage runs on clones with write cache.

    .PARAMETER PersistentDisk
        Optional drive letter (e.g., 'D:') where engage status is stored on write-cache VDI.
        When specified, the function reads seal from C:\ProgramData\vSEAL\status.json and
        merges in the engage section from {PersistentDisk}\vSEAL\status.json.
        If the secondary file is missing or corrupted, returns primary status without merge.

    .EXAMPLE
        Read-vSEALStatus

        Returns PSCustomObject with seal and engage sections if status.json exists,
        or $null if not found or corrupted.

    .EXAMPLE
        $status = Read-vSEALStatus
        if ($status -and $status.seal.result -eq 'Success') {
            # Seal was successful, proceed with engage
        }

    .EXAMPLE
        Read-vSEALStatus -PersistentDisk 'D:'

        Returns merged status: seal from C:\ProgramData\vSEAL\status.json and
        engage from D:\vSEAL\status.json.

    .OUTPUTS
        [PSCustomObject] with seal and engage sections, or $null if not found/corrupted.
    #>
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter()]
        [string]$PersistentDisk
    )

    # Define status file path
    $statusPath = Join-Path $env:ProgramData 'vSEAL\status.json'

    # Check if file exists
    if (-not (Test-Path -Path $statusPath -PathType Leaf)) {
        Write-vSEALLog "No status.json found at $statusPath" -Level VERBOSE
        return $null
    }

    # Read and parse primary status file
    try {
        $content = Get-Content -Path $statusPath -Raw -Encoding utf8
        $primaryStatus = $content | ConvertFrom-Json
        Write-vSEALLog "Primary status read: seal=$($primaryStatus.seal.result), engage=$($primaryStatus.engage.result)" -Level VERBOSE
    }
    catch {
        Write-vSEALLog "Failed to read status.json: $($_.Exception.Message)" -Level WARNING
        return $null
    }

    # If no persistent disk configured, return primary only
    if ([string]::IsNullOrWhiteSpace($PersistentDisk)) {
        return $primaryStatus
    }

    # Check for engage record on persistent disk
    $secondaryPath = Join-Path $PersistentDisk 'vSEAL\status.json'

    if (-not (Test-Path -Path $secondaryPath -PathType Leaf)) {
        Write-vSEALLog "No secondary status.json at $secondaryPath" -Level VERBOSE
        return $primaryStatus
    }

    try {
        $secondaryContent = Get-Content -Path $secondaryPath -Raw -Encoding utf8
        $secondaryStatus = $secondaryContent | ConvertFrom-Json

        # Merge: seal from primary, engage from secondary
        if ($secondaryStatus.engage) {
            $primaryStatus.engage = $secondaryStatus.engage
            Write-vSEALLog "Merged engage section from $secondaryPath" -Level VERBOSE
        }
    }
    catch {
        Write-vSEALLog "Failed to read secondary status.json: $($_.Exception.Message)" -Level WARNING
        # Return primary without merge - don't fail
    }

    return $primaryStatus
}
