param(
    [string]$InstallDirectory = (Split-Path -Path $PSScriptRoot -Parent),
    [switch]$RemoveAgentData
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

$stopScript = Join-Path $InstallDirectory "scripts\Stop-Agent.ps1"
if (Test-Path -Path $stopScript) {
    & powershell -ExecutionPolicy Bypass -File $stopScript -InstallDirectory $InstallDirectory
}

if (Test-Path -Path $InstallDirectory) {
    Remove-Item -Path $InstallDirectory -Recurse -Force
    Write-Host "Removed ImageAnywhere Agent package from $InstallDirectory"
}

if ($RemoveAgentData.IsPresent) {
    $dataRoot = Join-Path $env:LOCALAPPDATA "ImageAnywhere\agent"
    if (Test-Path -Path $dataRoot) {
        Remove-Item -Path $dataRoot -Recurse -Force
        Write-Host "Removed agent data root $dataRoot"
    }
}
else {
    Write-Host "Agent data was preserved under $env:LOCALAPPDATA\ImageAnywhere\agent."
    Write-Host "Re-run with -RemoveAgentData only when it is safe to delete settings, diagnostics, and pairing secrets."
}
