$ErrorActionPreference = "Stop"

$root = Split-Path -Parent $PSScriptRoot
$project = Join-Path $root "src\DentalClinic.Desktop\DentalClinic.Desktop.csproj"
$publishRoot = Join-Path $root "publish"
$publishDir = Join-Path $publishRoot ("DentalClinic-" + (Get-Date -Format "yyyyMMddHHmmss"))
$baseInstallDir = Join-Path $env:LOCALAPPDATA "DentalClinicApp"
$installDir = Join-Path $baseInstallDir ("app-" + (Get-Date -Format "yyyyMMddHHmmss"))
$exeName = "DentalClinic.Desktop.exe"
$installedExe = Join-Path $installDir $exeName

Write-Host ""
Write-Host "DentalPro Clinic setup" -ForegroundColor Cyan
Write-Host "======================" -ForegroundColor Cyan

Write-Host "Closing any running instances of DentalPro Clinic..."
Get-Process -Name "DentalClinic.Desktop" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1

if (-not (Get-Command dotnet -ErrorAction SilentlyContinue)) {
    Write-Host "ERROR: .NET SDK was not found. Install .NET SDK 9 or newer, then run setup again." -ForegroundColor Red
    exit 1
}

Write-Host "Publishing desktop app..."
New-Item -ItemType Directory -Force -Path $publishRoot | Out-Null
Write-Host "Restoring publish assets for win-x64..."
dotnet restore $project -r win-x64
if ($LASTEXITCODE -ne 0) {
    Write-Host "ERROR: Restore failed." -ForegroundColor Red
    exit $LASTEXITCODE
}

dotnet publish $project `
    -c Release `
    -r win-x64 `
    --self-contained true `
    -p:PublishSingleFile=true `
    -p:IncludeNativeLibrariesForSelfExtract=true `
    --no-restore `
    -o $publishDir

if ($LASTEXITCODE -ne 0) {
    Write-Host "ERROR: Publish failed." -ForegroundColor Red
    exit $LASTEXITCODE
}

if (-not (Test-Path $publishDir)) {
    Write-Host "ERROR: Publish folder was not created." -ForegroundColor Red
    exit 1
}

Write-Host "Installing to $installDir"
New-Item -ItemType Directory -Force -Path $baseInstallDir | Out-Null
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
Copy-Item -Path (Join-Path $publishDir "*") -Destination $installDir -Recurse -Force

if (-not (Test-Path $installedExe)) {
    Write-Host "ERROR: Application executable was not installed." -ForegroundColor Red
    exit 1
}

Set-Content -Path (Join-Path $baseInstallDir "current.txt") -Value $installedExe -Encoding UTF8

$shell = New-Object -ComObject WScript.Shell
$desktopShortcut = Join-Path ([Environment]::GetFolderPath("Desktop")) "DentalPro Clinic.lnk"
$startMenuDir = Join-Path ([Environment]::GetFolderPath("StartMenu")) "Programs\DentalPro Clinic"
$startMenuShortcut = Join-Path $startMenuDir "DentalPro Clinic.lnk"
New-Item -ItemType Directory -Force -Path $startMenuDir | Out-Null

foreach ($shortcutPath in @($desktopShortcut, $startMenuShortcut)) {
    $shortcut = $shell.CreateShortcut($shortcutPath)
    $shortcut.TargetPath = $installedExe
    $shortcut.WorkingDirectory = $installDir
    $shortcut.Description = "DentalPro Clinic Management System"
    $shortcut.Save()
}

Write-Host ""
Write-Host "Setup completed successfully." -ForegroundColor Green
Write-Host "Desktop shortcut: $desktopShortcut"
Write-Host "Start Menu shortcut: $startMenuShortcut"
Write-Host ""
Write-Host "Starting DentalPro Clinic..."
Start-Process -FilePath $installedExe -WorkingDirectory $installDir
