#Requires -Version 7.4
# Event Logs Redirect Plugin (Mock)

#region Helper Functions

function Get-EventLogPaths {
    # Mock: Get all event log paths
    @(
        @{ Name = 'Application'; Path = 'C:\Windows\System32\winevt\Logs\Application.evtx' }
        @{ Name = 'Security'; Path = 'C:\Windows\System32\winevt\Logs\Security.evtx' }
        @{ Name = 'System'; Path = 'C:\Windows\System32\winevt\Logs\System.evtx' }
        @{ Name = 'Setup'; Path = 'C:\Windows\System32\winevt\Logs\Setup.evtx' }
    )
}

function Set-EventLogPath {
    param(
        [string]$LogName,
        [string]$NewPath
    )
    Write-vSEALLog "Redirecting $LogName log to: $NewPath" -Level 'DEBUG'
    # Mock: In production would use wevtutil or registry
    # wevtutil sl $LogName /lfn:"$NewPath"
}

function New-DirectoryIfNotExists {
    param([string]$Path)
    if (-not (Test-Path $Path)) {
        Write-vSEALLog "Creating directory: $Path"
        # Mock: New-Item -Path $Path -ItemType Directory -Force
        Write-vSEALLog "Directory created (mock)"
    }
}

#endregion

#region Plugin Contract

function Get-PluginConfigSchema {
    @{
        path = @{
            Type        = 'string'
            Default     = 'EventLogs'
            Required    = $false
            Description = 'Subdirectory on persistent disk (joined with persistent_disk)'
        }
        logs = @{
            Type        = 'string[]'
            Default     = @('Application', 'Security', 'System')
            Required    = $false
            Description = 'Event logs to redirect'
        }
    }
}

function Test-Applicable {
    param($Globals)

    # Requires persistent disk for redirects to make sense
    if (-not $Globals.PersistentDisk) {
        return @{ Applicable = $false; Reason = 'No persistent disk configured' }
    }

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

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

    $targetPath = Join-Path $Globals.PersistentDisk $Config.path
    $logs = $Config.logs ?? @('Application', 'Security', 'System')

    $actions = @(
        "Create directory: $targetPath"
    )

    foreach ($log in $logs) {
        $actions += "Redirect $log log to $targetPath\$log.evtx"
    }

    return $actions
}

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

    # Build target path
    $targetPath = Join-Path $Globals.PersistentDisk $Config.path
    Write-vSEALLog "Event log target directory: $targetPath"

    # Create target directory
    New-DirectoryIfNotExists -Path $targetPath

    # Get current log configuration
    $currentLogs = Get-EventLogPaths
    $logsToRedirect = $Config.logs ?? @('Application', 'Security', 'System')

    # Redirect each configured log
    foreach ($logName in $logsToRedirect) {
        $currentLog = $currentLogs | Where-Object { $_.Name -eq $logName }
        if ($currentLog) {
            $newPath = Join-Path $targetPath "$logName.evtx"
            Set-EventLogPath -LogName $logName -NewPath $newPath
        }
        else {
            Write-vSEALLog "Event log '$logName' not found - skipping" -Level 'WARNING'
        }
    }

    Write-vSEALLog "Event log redirection complete - $($logsToRedirect.Count) logs configured"
}

# No Invoke-Engage - redirects are seal-only (paths persist)

#endregion
