param(
    [string]$Configuration = "Release",
    [string]$Runtime = "win-x64"
)

$ErrorActionPreference = "Stop"

$root = Split-Path -Parent $PSScriptRoot
$appProject = Join-Path $root "AutorentWinForms.csproj"
$installerProject = Join-Path $root "installer\AutorentSetup.csproj"
$publishDir = Join-Path $root "publish\AutorentWinForms"
$installerZip = Join-Path $root "installer\app.zip"
$distDir = Join-Path $root "dist"

Write-Host "Publishing application..."
dotnet publish $appProject `
    --configuration $Configuration `
    --runtime $Runtime `
    --self-contained true `
    -p:PublishSingleFile=true `
    -p:IncludeNativeLibrariesForSelfExtract=true `
    -p:EnableCompressionInSingleFile=true `
    -p:DebugType=None `
    -p:DebugSymbols=false `
    --output $publishDir

Get-ChildItem -LiteralPath $publishDir -Filter "*.pdb" -File | Remove-Item -Force

if (Test-Path $installerZip) {
    Remove-Item -LiteralPath $installerZip -Force
}

Write-Host "Packing application files..."
Compress-Archive -Path (Join-Path $publishDir "*") -DestinationPath $installerZip -Force

Write-Host "Building graphical installer..."
dotnet publish $installerProject `
    --configuration $Configuration `
    --runtime $Runtime `
    --self-contained true `
    -p:PublishSingleFile=true `
    -p:IncludeNativeLibrariesForSelfExtract=true `
    -p:EnableCompressionInSingleFile=true `
    -p:DebugType=None `
    -p:DebugSymbols=false `
    --output $distDir

Get-ChildItem -LiteralPath $distDir -Filter "*.pdb" -File | Remove-Item -Force

Write-Host ""
Write-Host "Installer is ready:"
Write-Host "  $(Join-Path $distDir 'AutorentSetup.exe')"
