#Requires -Version 7.4

function Resolve-vSEALLogPath {
    <#
    .SYNOPSIS
    Resolves the log directory path for a vSEAL execution phase.

    .DESCRIPTION
    Determines the log output directory based on the phase-specific configuration
    (seal_path or engage_path). Supports both absolute and relative paths; relative
    paths are resolved against the system drive or, for the Engage phase, the configured
    persistent disk. Creates the directory if it does not exist. Falls back to
    C:\ProgramData\vSEAL\Logs if the configured path cannot be created.

    .PARAMETER Phase
    The execution phase to resolve the log path for. Must be 'Seal', 'Engage', or 'Rollback'.

    .PARAMETER Configuration
    The parsed vSEAL configuration hashtable containing logging.seal_path,
    logging.engage_path, and optionally storage.persistent_disk settings.
    #>
    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('Seal', 'Engage', 'Rollback')]
        [string]$Phase,

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

    # Get configured path for this phase
    $configuredPath = if ($Phase -eq 'Seal') {
        $Configuration.logging.seal_path
    } else {
        $Configuration.logging.engage_path
    }

    # Default if not set
    $defaultPath = 'C:\ProgramData\vSEAL\Logs'
    if (-not $configuredPath) {
        $configuredPath = $defaultPath
    }

    # Absolute path - use as-is
    if ([System.IO.Path]::IsPathRooted($configuredPath)) {
        $resolvedPath = $configuredPath
    }
    else {
        # Relative path - resolve based on phase
        $baseDrive = $env:SystemDrive

        if ($Phase -eq 'Engage') {
            $persistentDisk = $Configuration.storage.persistent_disk
            if ($persistentDisk -and (Test-Path -Path $persistentDisk -PathType Container)) {
                $baseDrive = $persistentDisk.TrimEnd('\')
            }
        }

        $resolvedPath = Join-Path -Path $baseDrive -ChildPath $configuredPath
    }

    # Try to ensure directory exists
    if (-not (Test-Path -Path $resolvedPath -PathType Container)) {
        try {
            $null = New-Item -Path $resolvedPath -ItemType Directory -Force
            Write-vSEALLog "Created log directory: $resolvedPath" -Level 'VERBOSE'
        }
        catch {
            # Configured path failed - try fallback
            Write-vSEALLog "Cannot use configured log path '$resolvedPath': $($_.Exception.Message). Falling back to default." -Level 'WARNING'

            if (-not (Test-Path -Path $defaultPath -PathType Container)) {
                try {
                    $null = New-Item -Path $defaultPath -ItemType Directory -Force
                    Write-vSEALLog "Created default log directory: $defaultPath" -Level 'VERBOSE'
                }
                catch {
                    throw "Cannot create log directory. Tried '$resolvedPath' and '$defaultPath'. Aborting vSEAL execution."
                }
            }
            $resolvedPath = $defaultPath
        }
    }

    return $resolvedPath
}
