<#
RefDrop uninstaller — reverses Install.ps1.

Removes the program directory, Start menu shortcut, autostart-Run-key
entry (if present), and the Add/Remove Programs registration. Leaves
%APPDATA%\RefDrop\ alone — that's the user's settings/history/logs and
shouldn't be wiped without consent. The script offers to wipe that
data folder at the end.

Run by Add/Remove Programs, or by hand:
  powershell -ExecutionPolicy Bypass -File Uninstall.ps1
#>

[CmdletBinding()]
param()

$ErrorActionPreference = 'Continue'   # press through individual failures

$installRoot = Join-Path $env:LOCALAPPDATA 'Programs\RefDrop'
$startMenu = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'
$shortcutPath = Join-Path $startMenu 'RefDrop.lnk'
$regKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\RefDrop'
$autostartKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
$dataDir = Join-Path $env:APPDATA 'RefDrop'

Write-Host ""
Write-Host "Uninstalling RefDrop"
Write-Host ""

# Stop running instance
Get-Process RefDrop -ErrorAction SilentlyContinue | ForEach-Object {
    Write-Host "  Stopping running RefDrop (pid $($_.Id))…"
    Stop-Process -Id $_.Id -Force
    Start-Sleep -Milliseconds 500
}

# Remove autostart entry (if Settings → Autostart was on)
try {
    if (Get-ItemProperty -Path $autostartKey -Name 'RefDrop' -ErrorAction SilentlyContinue) {
        Remove-ItemProperty -Path $autostartKey -Name 'RefDrop' -ErrorAction SilentlyContinue
        Write-Host "  ✓ Removed autostart entry"
    }
} catch { }

# Remove Start menu shortcut
if (Test-Path $shortcutPath) {
    Remove-Item $shortcutPath -Force -ErrorAction SilentlyContinue
    Write-Host "  ✓ Removed Start Menu shortcut"
}

# Remove Add/Remove Programs entry
if (Test-Path $regKey) {
    Remove-Item -Path $regKey -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "  ✓ Removed Add/Remove Programs entry"
}

# Remove install root — note: Uninstall.ps1 is INSIDE this directory.
# We can't delete the file we're currently executing, so spawn a tiny
# detached cleanup that waits for our pid to exit, then nukes the dir.
if (Test-Path $installRoot) {
    $cleanup = @"
@echo off
:wait
tasklist /FI "PID eq $PID" 2>NUL | find "$PID" >NUL
if not errorlevel 1 (timeout /t 1 /nobreak >NUL & goto wait)
rmdir /S /Q "$installRoot"
(goto) 2>nul & del "%~f0"
"@
    $cleanupPath = Join-Path $env:TEMP 'refdrop-cleanup.cmd'
    Set-Content -Path $cleanupPath -Value $cleanup -Encoding ASCII
    Start-Process -FilePath $cleanupPath -WindowStyle Hidden
    Write-Host "  ✓ Scheduled program directory cleanup"
}

# Optional: wipe user data (settings, history, logs)
Write-Host ""
if (Test-Path $dataDir) {
    Write-Host "User data lives at $dataDir"
    Write-Host "  - settings.json"
    Write-Host "  - history.db"
    Write-Host "  - logs\"
    Write-Host "  - updates\ (staged updater downloads, if any)"
    $ans = Read-Host "Delete user data too? (y/N)"
    if ($ans -match '^[yY]') {
        Remove-Item $dataDir -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "  ✓ User data removed"
    } else {
        Write-Host "  Keeping user data — your captures stay safe."
    }
}

Write-Host ""
Write-Host "✓ Uninstalled" -ForegroundColor Green
Write-Host ""
