#Requires -Version 7.4
# Pagefile Configuration Plugin (Mock)

#region Helper Functions

function Get-PagefileConfiguration {
    # Mock: Get current pagefile settings
    @{
        AutomaticManaged = $false
        CurrentLocation = 'C:\pagefile.sys'
        InitialSize = 4096
        MaximumSize = 8192
    }
}

function Test-DriveExists {
    param([string]$DriveLetter)
    # Mock: Check if drive exists
    $DriveLetter -match '^[C-Z]:?$'
}

function Set-PagefileLocation {
    param(
        [string]$Path,
        [int]$SizeMB
    )
    Write-vSEALLog "Configuring pagefile at: $Path"
    Write-vSEALLog "Size: ${SizeMB}MB (initial and maximum)"
    # Mock: In production would use WMI/CIM to configure pagefile
    # Disable automatic management first
    # Set-CimInstance -Query "SELECT * FROM Win32_ComputerSystem" -Property @{AutomaticManagedPagefile=$false}
    # Configure pagefile
    # New-CimInstance -ClassName Win32_PageFileSetting -Property @{Name=$Path; InitialSize=$SizeMB; MaximumSize=$SizeMB}
    Write-vSEALLog "Pagefile configured (mock) - requires reboot"
}

#endregion

#region Plugin Contract

function Get-PluginConfigSchema {
    @{
        location = @{
            Type        = 'string'
            Default     = $null
            Required    = $true
            Description = 'Pagefile path (e.g., D:\pagefile.sys)'
        }
        size_mb = @{
            Type        = 'int'
            Default     = 4096
            Required    = $false
            Description = 'Pagefile size in MB (used for both initial and maximum)'
        }
    }
}

function Test-Applicable {
    param($Globals)

    # Always applicable on workstations
    if ($Globals.OS.IsServer) {
        return @{ Applicable = $false; Reason = 'Server pagefile typically managed by policy' }
    }

    return @{ Applicable = $true; Reason = $null }
}

function Get-PlannedActions {
    param($Config, $Globals, $Phase)

    @(
        "Disable automatic pagefile management"
        "Configure pagefile at: $($Config.location)"
        "Set pagefile size to: $($Config.size_mb)MB"
        "Remove existing pagefile from C: drive (if moving)"
    )
}

function Invoke-Seal {
    param($Config, $Globals)

    $current = Get-PagefileConfiguration
    Write-vSEALLog "Current pagefile: $($current.CurrentLocation), Size: $($current.InitialSize)MB"

    # Validate target drive exists
    $targetDrive = Split-Path $Config.location -Qualifier
    if (-not (Test-DriveExists -DriveLetter $targetDrive)) {
        Write-vSEALLog "Target drive $targetDrive does not exist!" -Level 'ERROR'
        throw "Pagefile target drive $targetDrive not found"
    }

    # Configure new pagefile location
    Set-PagefileLocation -Path $Config.location -SizeMB $Config.size_mb

    # Remove old pagefile if different location
    if ($current.CurrentLocation -ne $Config.location) {
        Write-vSEALLog "Removing old pagefile at: $($current.CurrentLocation)"
        # Mock: Would delete pagefile.sys from old location after reboot
        Write-vSEALLog "Old pagefile will be removed after reboot (mock)"
    }

    Write-vSEALLog "Pagefile configuration complete - reboot required for changes to take effect"
}

# No Invoke-Engage - pagefile is seal-only

#endregion
