# Kraken Player - uninstaller. Run elevated:
#   powershell -ExecutionPolicy Bypass -File .\uninstall.ps1
#
# Reverses what install.ps1 set up. By default it KEEPS %ProgramData%\KrakenPlayer
# (device pairing/identity) so a reinstall stays paired; pass -Purge to remove it.
# Removing the kiosk account is destructive, so it is gated behind confirmation
# and must NEVER be part of a routine update/reinstall flow.
[CmdletBinding()]
param(
  [string]$KioskUser = "KrakenKiosk",
  [switch]$RemoveKioskAccount,
  [switch]$Purge,
  [switch]$Force
)

$ErrorActionPreference = "Continue"

$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  Write-Error "Run this uninstaller from an elevated (Administrator) PowerShell."
  exit 1
}

$installDir = Join-Path $env:ProgramFiles "KrakenPlayer"
$dataDir    = Join-Path $env:ProgramData "KrakenPlayer"

Write-Host "==> Stopping the player + Scheduled Task..."
Stop-ScheduledTask -TaskName "KrakenPlayer" -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskName "KrakenPlayer" -Confirm:$false -ErrorAction SilentlyContinue
Get-Process kraken-player -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
# Kill the kiosk Chrome/Edge the player launched, so uninstall clears the on-screen kiosk.
Get-CimInstance Win32_Process | Where-Object { ($_.Name -eq "chrome.exe" -or $_.Name -eq "msedge.exe") -and $_.CommandLine -like "*KrakenPlayer*chromium*" } |
  ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
# And the Vision AI detector + any camera-holding ffmpeg: the hard kill above
# skips the player's own cleanup, and a leftover detector keeps the webcam open.
Get-Process kraken-detector -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Get-CimInstance Win32_Process | Where-Object { $_.Name -eq "ffmpeg.exe" -and $_.CommandLine -like "*dshow*" } |
  ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }

Write-Host "==> Removing Defender exclusions..."
Remove-MpPreference -ExclusionPath $installDir -ErrorAction SilentlyContinue
Remove-MpPreference -ExclusionPath $dataDir -ErrorAction SilentlyContinue
Remove-MpPreference -ExclusionProcess "kraken-player.exe" -ErrorAction SilentlyContinue
Remove-MpPreference -ExclusionProcess "ffmpeg.exe" -ErrorAction SilentlyContinue

Write-Host "==> Clearing auto-login (Winlogon AutoAdminLogon)..."
$winlogon = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path $winlogon -Name "AutoAdminLogon" -Value "0" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $winlogon -Name "DefaultPassword" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $winlogon -Name "DefaultUserName" -ErrorAction SilentlyContinue

Write-Host "==> Removing shortcuts..."
Remove-Item (Join-Path $env:ProgramData "Microsoft\Windows\Start Menu\Programs\Kraken Player") -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item (Join-Path ([Environment]::GetFolderPath("Startup")) "Kraken Player.lnk") -Force -ErrorAction SilentlyContinue

Write-Host "==> Removing install directory..."
Remove-Item $installDir -Recurse -Force -ErrorAction SilentlyContinue

if ($RemoveKioskAccount) {
  if (-not $Force) {
    $ans = Read-Host "Remove the local kiosk account '$KioskUser' and its profile? This is destructive. [y/N]"
    if ($ans -ne "y") { Write-Host "Keeping the kiosk account."; $RemoveKioskAccount = $false }
  }
  if ($RemoveKioskAccount) {
    Write-Host "==> Removing kiosk account '$KioskUser'..."
    Remove-LocalUser -Name $KioskUser -ErrorAction SilentlyContinue
  }
}

if ($Purge) {
  Write-Host "==> -Purge: removing $dataDir (device pairing/identity will be lost)..."
  Remove-Item $dataDir -Recurse -Force -ErrorAction SilentlyContinue
} else {
  Write-Host "==> Keeping $dataDir (device stays paired). Pass -Purge to remove it."
}

Write-Host ""
Write-Host "Kraken Player uninstalled."
