$ErrorActionPreference = 'Stop'

# Custom uninstallation steps
Write-Host "Starting custom uninstallation steps for $env:ChocolateyPackageName..."

# Step 1: Delete the installed 'oto.exe' file
$installPath = Join-Path $env:LOCALAPPDATA "com.toki-labs.oto"
$otoExePath = Join-Path $installPath "oto.exe"

if (Test-Path $otoExePath) {
    Write-Host "Deleting $otoExePath..."
    Remove-Item -Path $otoExePath -Force
    Write-Host "File $otoExePath deleted."
} else {
    Write-Host "File $otoExePath not found. Skipping deletion."
}

# Step 2: Remove installation path from the PATH environment variable
Write-Host "Removing '$installPath' from PATH environment variable..."
$envPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::User)
if ($envPath -match [regex]::Escape($installPath)) {
    $newPath = ($envPath -split ';') -notmatch [regex]::Escape($installPath) -join ';'
    [System.Environment]::SetEnvironmentVariable("Path", $newPath, [System.EnvironmentVariableTarget]::User)
    Write-Host "Removed '$installPath' from PATH."
} else {
    Write-Host "'$installPath' not found in PATH. No changes made."
}

# Skip Chocolatey uninstallation logic since no registry uninstall key is available
Write-Host "Skipping Chocolatey uninstallation logic as no uninstall registry key exists."

Write-Host "Custom uninstallation steps for $env:ChocolateyPackageName completed successfully."
