$ErrorActionPreference = 'Stop'

$appName = 'ComputerShopWpf'
$displayName = 'Продажа компьютерной техники'
$sourceDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$installDir = Join-Path $env:LOCALAPPDATA $appName

if (-not (Test-Path -LiteralPath $installDir)) {
    New-Item -ItemType Directory -Path $installDir | Out-Null
}

Get-ChildItem -LiteralPath $sourceDir -Force |
    Where-Object { $_.Name -notin @('install.cmd', 'install.ps1') } |
    Copy-Item -Destination $installDir -Recurse -Force

$exePath = Join-Path $installDir 'ComputerShopWpf.exe'
if (-not (Test-Path -LiteralPath $exePath)) {
    throw "ComputerShopWpf.exe was not copied to $installDir"
}

$databaseDir = Join-Path $installDir 'Database'
if (-not (Test-Path -LiteralPath $databaseDir)) {
    New-Item -ItemType Directory -Path $databaseDir | Out-Null
}

$flatSqlPath = Join-Path $installDir 'create_database.sql'
if (Test-Path -LiteralPath $flatSqlPath) {
    Copy-Item -LiteralPath $flatSqlPath -Destination (Join-Path $databaseDir 'create_database.sql') -Force
}

$desktopPath = [Environment]::GetFolderPath('DesktopDirectory')
$shortcutPath = Join-Path $desktopPath "$displayName.lnk"
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $exePath
$shortcut.WorkingDirectory = $installDir
$shortcut.IconLocation = $exePath
$shortcut.Description = $displayName
$shortcut.Save()

$uninstallShortcutPath = Join-Path $desktopPath "$displayName - удалить.lnk"
$uninstallScript = Join-Path $installDir 'uninstall.ps1'
if (Test-Path -LiteralPath $uninstallScript) {
    $uninstallShortcut = $shell.CreateShortcut($uninstallShortcutPath)
    $uninstallShortcut.TargetPath = 'powershell.exe'
    $uninstallShortcut.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$uninstallScript`""
    $uninstallShortcut.WorkingDirectory = $installDir
    $uninstallShortcut.Description = "Удалить $displayName"
    $uninstallShortcut.Save()
}

$shell.Popup("Приложение установлено в $installDir.`nЯрлык создан на рабочем столе.", 0, $displayName, 64) | Out-Null



