<#
RefDrop installer — per-user, no admin required.

Copies RefDrop.exe into %LOCALAPPDATA%\Programs\RefDrop\, drops a Start
menu shortcut, and registers an "Add or Remove Programs" entry pointing
at Uninstall.ps1. Run by right-clicking → "Run with PowerShell" on the
extracted ZIP.

Why per-user: avoids the UAC elevation prompt and lets non-admin users
install. Side-effect: each Windows account gets its own copy. That's
the right trade-off for a personal-tool category.
#>

[CmdletBinding()]
param()

$ErrorActionPreference = 'Stop'

# Resolve paths
$installRoot = Join-Path $env:LOCALAPPDATA 'Programs\RefDrop'
$exeName = 'RefDrop.exe'
$exeSource = Join-Path $PSScriptRoot $exeName
$exeDest = Join-Path $installRoot $exeName
$uninstallScriptSource = Join-Path $PSScriptRoot 'Uninstall.ps1'
$uninstallScriptDest = Join-Path $installRoot 'Uninstall.ps1'
$licenseSource = Join-Path $PSScriptRoot 'LICENSE'
$licenseDest = Join-Path $installRoot 'LICENSE'

# Sanity check
if (-not (Test-Path $exeSource)) {
    Write-Host "✗ RefDrop.exe not found next to Install.ps1." -ForegroundColor Red
    Write-Host "  Make sure you extracted the entire ZIP, then run from the extracted folder."
    exit 1
}

Write-Host ""
Write-Host "Installing RefDrop to $installRoot"
Write-Host ""

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

# Copy program files
New-Item -ItemType Directory -Path $installRoot -Force | Out-Null
Copy-Item $exeSource $exeDest -Force
Copy-Item $uninstallScriptSource $uninstallScriptDest -Force
if (Test-Path $licenseSource) { Copy-Item $licenseSource $licenseDest -Force }
Write-Host "  ✓ Copied program files"

# Start Menu shortcut
$startMenu = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'
$shortcutPath = Join-Path $startMenu 'RefDrop.lnk'
$wshShell = New-Object -ComObject WScript.Shell
$shortcut = $wshShell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $exeDest
$shortcut.WorkingDirectory = $installRoot
$shortcut.IconLocation = "$exeDest,0"
$shortcut.Description = 'RefDrop — Windows screenshot tool'
$shortcut.Save()
Write-Host "  ✓ Created Start Menu shortcut"

# Add/Remove Programs registry entry (per-user)
$regKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\RefDrop'
$pwsh = (Get-Command powershell.exe).Source
New-Item -Path $regKey -Force | Out-Null
$installSize = [math]::Round((Get-Item $exeDest).Length / 1KB)

# Read version from the EXE if available; fall back to "0.3.1"
$displayVersion = '0.3.1'
try {
    $vi = (Get-Item $exeDest).VersionInfo
    if ($vi -and $vi.ProductVersion) { $displayVersion = $vi.ProductVersion }
} catch { }

Set-ItemProperty -Path $regKey -Name 'DisplayName'     -Value 'RefDrop'
Set-ItemProperty -Path $regKey -Name 'DisplayIcon'     -Value $exeDest
Set-ItemProperty -Path $regKey -Name 'DisplayVersion'  -Value $displayVersion
Set-ItemProperty -Path $regKey -Name 'Publisher'       -Value 'mirokim'
Set-ItemProperty -Path $regKey -Name 'InstallLocation' -Value $installRoot
Set-ItemProperty -Path $regKey -Name 'EstimatedSize'   -Value $installSize -Type DWord
Set-ItemProperty -Path $regKey -Name 'NoModify'        -Value 1 -Type DWord
Set-ItemProperty -Path $regKey -Name 'NoRepair'        -Value 1 -Type DWord
Set-ItemProperty -Path $regKey -Name 'URLInfoAbout'    -Value 'https://refdrop.mmwo.io'
Set-ItemProperty -Path $regKey -Name 'UninstallString' -Value "`"$pwsh`" -ExecutionPolicy Bypass -File `"$uninstallScriptDest`""
Write-Host "  ✓ Registered with Add or Remove Programs"

Write-Host ""
Write-Host "✓ Installed successfully" -ForegroundColor Green
Write-Host "  Launch from Start menu or run: $exeDest"
Write-Host ""

# Offer to launch immediately
$ans = Read-Host "Launch RefDrop now? (Y/n)"
if ([string]::IsNullOrWhiteSpace($ans) -or $ans -match '^[yY]') {
    Start-Process $exeDest
    Write-Host "  RefDrop is starting — look for the tray icon."
}
