#Requires -Version 5.1
param([string]$InstallDir = "C:\\DPF")

$ErrorActionPreference = "Stop"

$DPF_DIR = [System.IO.Path]::GetFullPath($InstallDir)
$AUTOSTART_TASK_NAME = "DPF-AutoStart"

Write-Host ""
Write-Host "========================================================" -ForegroundColor Red
Write-Host "  Digital Product Factory  Uninstall                 " -ForegroundColor Red
Write-Host "  This will remove the platform and all its data      " -ForegroundColor Red
Write-Host "========================================================" -ForegroundColor Red
Write-Host ""
Write-Host "This will:" -ForegroundColor Yellow
Write-Host "   Stop and remove all Docker containers"
Write-Host "   Delete all platform data (database, AI models, messages)"
Write-Host "   Remove the $DPF_DIR directory"
Write-Host "   Remove the DPF AutoStart scheduled task"
Write-Host "   Remove DPF from your PATH"
Write-Host ""
Write-Host "Docker Desktop will NOT be uninstalled (you may need it for other things)."
Write-Host ""

$confirm = Read-Host "Type 'yes' to confirm uninstall"
if ($confirm -ne "yes") {
    Write-Host "Uninstall cancelled." -ForegroundColor Green
    exit 0
}

Write-Host ""

Write-Host "========================================================" 

Write-Host "Step 1: Stopping and removing containers..." -ForegroundColor Cyan
if (Test-Path "$DPF_DIR\docker-compose.yml") {
    try {
        Set-Location $DPF_DIR
        docker compose down -v --remove-orphans 2>$null
        Write-Host "   Containers and volumes removed" -ForegroundColor Green
    } catch {
        Write-Host "   Could not stop containers (Docker may not be running)" -ForegroundColor Yellow
    }
} else {
    Write-Host "   No docker-compose.yml found, skipping" -ForegroundColor Yellow
}

Write-Host "========================================================" 

Write-Host "Step 2: Removing Docker images..." -ForegroundColor Cyan
try {
    $images = docker images --filter "reference=*dpf*" --filter "reference=*opendigitalproductfactory*" -q 2>$null
    if ($images) {
        docker rmi $images -f 2>$null
    }
    # Also remove the built portal images
    $composeImages = docker images --filter "reference=*feature-windows-installer*" --filter "reference=*c-dpf*" -q 2>$null
    if ($composeImages) {
        docker rmi $composeImages -f 2>$null
    }
    Write-Host "   Docker images cleaned" -ForegroundColor Green
} catch {
    Write-Host "   Could not remove some images" -ForegroundColor Yellow
}

Write-Host "========================================================" 

Write-Host "Step 3: Removing $DPF_DIR..." -ForegroundColor Cyan
Set-Location $env:USERPROFILE  # Move out of the directory first

# Post-relocation (DPF_BACKUPS_HOST_PATH in .env), operator backup history
# lives OUTSIDE the install root at $DPF_DIR-backups\ and is untouched by
# the Step 3 rm. This block handles the legacy in-tree case: if backups
# are still inside $DPF_DIR\backups\ from a pre-relocation install, prompt
# the operator (defaulting to PRESERVE) before the rm destroys them.
# On a fully-relocated install $DPF_DIR\backups\ does not exist and this
# block is a no-op.
$backupsSrc = Join-Path $DPF_DIR "backups"
$preserveBackups = $false
if (Test-Path $backupsSrc) {
    $hasContent = $false
    try {
        $first = Get-ChildItem -LiteralPath $backupsSrc -Force -ErrorAction SilentlyContinue | Select-Object -First 1
        $hasContent = [bool]$first
    } catch {
        $hasContent = $false
    }
    if ($hasContent) {
        Write-Host ""
        Write-Host "   Found legacy in-tree backups at $backupsSrc" -ForegroundColor Yellow
        Write-Host "   (Relocated installs keep backups at $DPF_DIR-backups\ outside the install root,"
        Write-Host "   which uninstall does NOT touch. These appear to predate the relocation.)"
        Write-Host "   These are the only persisted copies of your database dumps."
        $keep = Read-Host "   Preserve them at $DPF_DIR-backups\ for a future install? (yes/no, default yes)"
        if ($keep -eq "" -or $keep -eq "yes" -or $keep -eq "y") {
            $preserveBackups = $true
        }
    }
}

if ($preserveBackups) {
    $preserveDir = "$DPF_DIR-backups"
    try {
        if (-not (Test-Path $preserveDir)) {
            New-Item -ItemType Directory -Path $preserveDir | Out-Null
        }
        Get-ChildItem -LiteralPath $backupsSrc -Force | ForEach-Object {
            $dest = Join-Path $preserveDir $_.Name
            if (Test-Path $dest) {
                Write-Host "   Skipped $($_.Name): already present in $preserveDir" -ForegroundColor Yellow
            } else {
                Move-Item -LiteralPath $_.FullName -Destination $dest -Force
            }
        }
        Write-Host "   Backups preserved at $preserveDir" -ForegroundColor Green
    } catch {
        Write-Host "   Could not preserve backups: $_" -ForegroundColor Red
        Write-Host "   Refusing to delete $DPF_DIR while backups are present." -ForegroundColor Red
        Write-Host "   Move $backupsSrc somewhere safe by hand, then re-run." -ForegroundColor Red
        exit 1
    }
}

if (Test-Path $DPF_DIR) {
    try {
        Remove-Item $DPF_DIR -Recurse -Force
        Write-Host "   $DPF_DIR removed" -ForegroundColor Green
    } catch {
        Write-Host "   Could not fully remove $DPF_DIR  some files may be locked" -ForegroundColor Yellow
        Write-Host "    Try closing any open files or terminals in that directory, then delete manually" -ForegroundColor Yellow
    }
} else {
    Write-Host "   $DPF_DIR not found, skipping" -ForegroundColor Yellow
}

Write-Host "========================================================" 

Write-Host "Step 4: Removing from PATH..." -ForegroundColor Cyan
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -like "*$DPF_DIR*") {
    $newPath = ($userPath -split ";" | Where-Object { $_ -ne $DPF_DIR -and $_ -ne "" }) -join ";"
    [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
    Write-Host "   Removed $DPF_DIR from PATH" -ForegroundColor Green
} else {
    Write-Host "   Not in PATH, skipping" -ForegroundColor Yellow
}

Write-Host "========================================================" 

Write-Host "Step 5: Removing DPF auto-start task..." -ForegroundColor Cyan
try {
    if (Get-ScheduledTask -TaskName $AUTOSTART_TASK_NAME -ErrorAction SilentlyContinue) {
        Unregister-ScheduledTask -TaskName $AUTOSTART_TASK_NAME -Confirm:$false -ErrorAction Stop
Write-Host "   Removed DPF auto-start task" -ForegroundColor Green
    } else {
Write-Host "   Auto-start task not found, skipping" -ForegroundColor Yellow
    }
} catch {
    Write-Host "  Could not remove auto-start task (continuing uninstall)" -ForegroundColor Yellow
}
Write-Host "Step 6: Cleaning up..." -ForegroundColor Cyan
# Remove any temp files from install
Remove-Item "$env:TEMP\dpf-*" -Force -ErrorAction SilentlyContinue
Remove-Item "$env:TEMP\DockerDesktopInstaller.exe" -Force -ErrorAction SilentlyContinue
Write-Host "   Temp files cleaned" -ForegroundColor Green

Write-Host "========================================================" 

Write-Host ""
Write-Host "========================================================" -ForegroundColor Green
Write-Host "  Uninstall complete!                                 " -ForegroundColor Green
Write-Host "                                                      " -ForegroundColor Green
Write-Host "  What was removed:                                   " -ForegroundColor Green
Write-Host "   All DPF containers and data volumes               " -ForegroundColor Green
Write-Host "   The $DPF_DIR directory                              " -ForegroundColor Green
Write-Host "   DPF from your system PATH                         " -ForegroundColor Green
Write-Host "   Remove the DPF auto-start task (if configured)"
Write-Host "                                                      " -ForegroundColor Green
Write-Host "  What was kept:                                      " -ForegroundColor Green
Write-Host "   Docker Desktop (uninstall separately if needed)   " -ForegroundColor Green
Write-Host "   WSL2 (Windows feature, safe to keep)              " -ForegroundColor Green
Write-Host "   Platform backups (if you chose to preserve them)  " -ForegroundColor Green
Write-Host "     at $DPF_DIR-backups\                            " -ForegroundColor Green
Write-Host "                                                      " -ForegroundColor Green
Write-Host "  To reinstall, run install-dpf.ps1 again.            " -ForegroundColor Green
Write-Host "========================================================" -ForegroundColor Green



