#Requires -Version 7.4
# Microsoft Office KMS Rearm Plugin (Mock)

#region Helper Functions

function Get-OfficeInstallation {
    # Mock: Detect Office installation
    @{
        Installed = $true
        Version = '16.0'  # Office 2016/2019/M365
        Edition = 'Microsoft 365 Apps for Enterprise'
        ClickToRun = $true
        Path = 'C:\Program Files\Microsoft Office\root\Office16'
        LicenseStatus = 'Licensed'
    }
}

function Get-OfficeRearmCount {
    # Mock: Get remaining rearms
    # In production would query: cscript ospp.vbs /dstatus
    @{
        RemainingRearms = 5
        MaxRearms = 5
        LastRearmDate = $null
    }
}

function Invoke-OfficeRearm {
    Write-vSEALLog "Executing Office license rearm..."
    # Mock: In production would run:
    # cscript "C:\Program Files\Microsoft Office\Office16\ospp.vbs" /rearm
    # Or for Click-to-Run:
    # cscript "C:\Program Files\Microsoft Office\root\Office16\ospp.vbs" /rearm
    Write-vSEALLog "Office license rearmed (mock)"
}

function Clear-OfficeActivationCache {
    Write-vSEALLog "Clearing Office activation cache..."
    $cachePaths = @(
        "$env:LOCALAPPDATA\Microsoft\Office\Licenses"
        "$env:LOCALAPPDATA\Microsoft\Office\16.0\Licensing"
    )
    foreach ($path in $cachePaths) {
        Write-vSEALLog "Clearing: $path" -Level 'DEBUG'
        # Mock: Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
    }
    Write-vSEALLog "Activation cache cleared (mock)"
}

#endregion

#region Plugin Contract

function Get-PluginConfigSchema {
    @{
        rearm = @{
            Type        = 'bool'
            Default     = $true
            Required    = $false
            Description = 'Rearm Office KMS license for imaging'
        }
        clear_cache = @{
            Type        = 'bool'
            Default     = $true
            Required    = $false
            Description = 'Clear Office activation/licensing cache'
        }
    }
}

function Test-Applicable {
    param($Globals)

    $office = Get-OfficeInstallation
    if (-not $office.Installed) {
        return @{ Applicable = $false; Reason = 'Microsoft Office not installed' }
    }

    # Check if volume licensed (KMS/MAK)
    # Mock: Assume volume licensing for demo
    return @{ Applicable = $true; Reason = $null }
}

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

    $actions = @()

    if ($Config.rearm) {
        $actions += 'Rearm Office KMS license'
    }
    if ($Config.clear_cache) {
        $actions += 'Clear Office activation cache'
    }

    return $actions
}

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

    $office = Get-OfficeInstallation
    Write-vSEALLog "Office detected: $($office.Edition), Version: $($office.Version)"

    # Check rearm count
    $rearmInfo = Get-OfficeRearmCount
    Write-vSEALLog "Remaining rearms: $($rearmInfo.RemainingRearms)/$($rearmInfo.MaxRearms)"

    if ($rearmInfo.RemainingRearms -le 0) {
        Write-vSEALLog "No rearms remaining - skipping rearm step" -Level 'WARNING'
    }
    elseif ($Config.rearm) {
        Invoke-OfficeRearm
    }

    # Clear activation cache
    if ($Config.clear_cache) {
        Clear-OfficeActivationCache
    }

    Write-vSEALLog "Office sealing complete"
}

# No Invoke-Engage - Office reactivates automatically via KMS

#endregion
