# RescuePC Protected Module - DO NOT EDIT
# Source code is maintained separately in the development repository.

$script:CurrentUsername = $env:USERNAME
$script:CurrentHostname = $env:COMPUTERNAME

function Remove-PII {
    param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [AllowEmptyString()]
        [string]$Text,

        [switch]$PreserveStructure
    )

    if ([string]::IsNullOrEmpty($Text)) {
        return $Text
    }

    $result = $Text

    if (-not [string]::IsNullOrEmpty($script:CurrentUsername)) {
        $result = $result -replace "(?i)\\Users\\$([regex]::Escape($script:CurrentUsername))\\", '\Users\[USER]\'
        $result = $result -replace "(?i)\\Users\\$([regex]::Escape($script:CurrentUsername))(?=\s|$|[^\w])", '\Users\[USER]'
    }

    $result = $result -replace '(?i)C:\\Users\\([^\\]+)\\', 'C:\Users\[USER]\'

    if (-not [string]::IsNullOrEmpty($script:CurrentHostname)) {
        $result = $result -replace "(?i)\b$([regex]::Escape($script:CurrentHostname))\b", '[HOSTNAME]'
    }

    $result = $result -replace '(?i)\b(DESKTOP|LAPTOP|PC|WORKSTATION|SERVER)-[A-Z0-9]{5,}\b', '[HOSTNAME]'

    $result = $result -replace '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b', '[EMAIL]'

    $result = $result -replace '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b', '[IP]'

    $result = $result -replace '\b([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b', '[MAC]'

    $result = $result -replace '(?i)\\\\([^\\]+)\\([^\\]+)', '\\[SERVER]\[SHARE]'

    $result = $result -replace '(?i)C:\\Users\\[^\\]+\\AppData', 'C:\Users\[USER]\AppData'

    $result = $result -replace '(?i)C:\\Users\\[^\\]+\\Local Settings', 'C:\Users\[USER]\Local Settings'

    return $result
}

function Clear-PIIFromLogFile {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [string]$OutputPath
    )

    if (-not (Test-Path $Path)) {
        Write-Warning "Log file not found: $Path"
        return $false
    }

    try {
        $content = Get-Content -Path $Path -Raw -ErrorAction Stop
        $scrubbed = Remove-PII -Text $content

        $targetPath = if ($OutputPath) { $OutputPath } else { $Path }
        $scrubbed | Set-Content -Path $targetPath -Encoding UTF8 -Force

        return $true
    }
    catch {
        Write-Warning "Failed to scrub log file: $($_.Exception.Message)"
        return $false
    }
}

function Get-ScrubStats {
    param(
        [Parameter(Mandatory=$true)]
        [string]$Text
    )

    $stats = @{
        UserPaths = ([regex]::Matches($Text, '(?i)C:\\Users\\[^\\]+\\')).Count
        Hostnames = ([regex]::Matches($Text, '(?i)\b(DESKTOP|LAPTOP|PC|WORKSTATION|SERVER)-[A-Z0-9]{5,}\b')).Count
        Emails = ([regex]::Matches($Text, '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b')).Count
        IPs = ([regex]::Matches($Text, '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')).Count
        MACs = ([regex]::Matches($Text, '\b([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}\b')).Count
        UNCPaths = ([regex]::Matches($Text, '(?i)\\\\[^\\]+\\[^\\]+')).Count
    }

    $stats.Total = $stats.UserPaths + $stats.Hostnames + $stats.Emails + $stats.IPs + $stats.MACs + $stats.UNCPaths

    return [PSCustomObject]$stats
}

Export-ModuleMember -Function Remove-PII, Clear-PIIFromLogFile, Get-ScrubStats