# YarMenu Accounting — نسخ إلى AppData (اختياري) + اختصار سطح المكتب
# Usage: powershell -ExecutionPolicy Bypass -File create_desktop_shortcut.ps1
# Run from extracted dist folder (same folder as yarmenu_accounting.exe)

param(
    [string]$SourceDir = $PSScriptRoot,
    [string]$InstallDir = "$env:LOCALAPPDATA\YarMenu\Accounting",
    [switch]$PortableShortcut
)

$ErrorActionPreference = 'Stop'
$ExeName = 'yarmenu_accounting.exe'

function Write-Step($msg) {
    Write-Host "`n==> $msg" -ForegroundColor Cyan
}

$SourceDir = (Resolve-Path $SourceDir).Path
$sourceExe = Join-Path $SourceDir $ExeName
if (-not (Test-Path $sourceExe)) {
    throw "Not found: $sourceExe`nRun this script from the YarMenu Accounting folder."
}

$targetDir = if ($PortableShortcut) { $SourceDir } else { $InstallDir }
$targetExe = Join-Path $targetDir $ExeName

if (-not $PortableShortcut) {
    Write-Step "Copying app to $InstallDir"
    if (Test-Path $InstallDir) {
        Remove-Item -Path $InstallDir -Recurse -Force
    }
    New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
    Copy-Item -Path (Join-Path $SourceDir '*') -Destination $InstallDir -Recurse -Force
}

$desktop = [Environment]::GetFolderPath('Desktop')
$shortcutPath = Join-Path $desktop 'YarMenu Accounting.lnk'

Write-Step 'Creating desktop shortcut'
$wsh = New-Object -ComObject WScript.Shell
$shortcut = $wsh.CreateShortcut($shortcutPath)
$shortcut.TargetPath = $targetExe
$shortcut.WorkingDirectory = $targetDir
$shortcut.Description = 'YarMenu Accounting'
$shortcut.IconLocation = "$targetExe,0"
$shortcut.Save()

$registerScript = Join-Path $PSScriptRoot 'register_windows_app.ps1'
if ((Test-Path $registerScript) -and -not $PortableShortcut) {
    Write-Step 'Registering in Windows Installed apps'
    & $registerScript -InstallDir $InstallDir
}

Write-Host ''
Write-Host 'Desktop shortcut created.' -ForegroundColor Green
Write-Host "  Shortcut: $shortcutPath"
Write-Host "  App:      $targetExe"
Write-Host ''
