$ErrorActionPreference = "Stop"

$projectRoot = Split-Path -Parent $PSScriptRoot
$applicationDir = Join-Path $projectRoot "application"
$gradleWrapper = Join-Path $applicationDir "gradlew.bat"
$projectCache = Join-Path $projectRoot ".gradle-project-cache"
$env:GRADLE_USER_HOME = Join-Path $projectRoot ".gradle-user"
$bootstrap = Join-Path $PSScriptRoot "bootstrap-dependencies.ps1"
$versionLine = Get-Content (Join-Path $applicationDir "gradle.properties") |
    Where-Object { $_ -match '^maptoolsVersion=' } |
    Select-Object -First 1
if (-not $versionLine) { throw "maptoolsVersion fehlt in application/gradle.properties." }
$version = ($versionLine -split '=', 2)[1].Trim()
$buildDirectoryName = "build-windows-$version"
$buildDirectory = Join-Path $projectRoot $buildDirectoryName
$kotlinCache = Join-Path $buildDirectory ".kotlin"
$releaseDirectory = Join-Path $projectRoot "release\$version"
$windowsReleaseDirectory = Join-Path $releaseDirectory "windows"
$sourceReleaseDirectory = Join-Path $releaseDirectory "source"
$securityReleaseDirectory = Join-Path $releaseDirectory "security"
$securityReportDirectory = Join-Path $projectRoot "security\reports\$version"

if (-not $env:JAVA_HOME) {
    throw "JAVA_HOME ist nicht gesetzt. Bitte Microsoft OpenJDK 25 konfigurieren."
}

& $bootstrap

New-Item -ItemType Directory -Force -Path (Join-Path $kotlinCache "sessions") | Out-Null
& $gradleWrapper -p $applicationDir --no-daemon --project-cache-dir $projectCache `
    "-Pmaptools.buildRoot=../$buildDirectoryName" `
    "-Pmaptools.releaseRoot=../$buildDirectoryName/source" `
    "-Pkotlin.project.persistent.dir=$kotlinCache" `
    :core:desktopTest test :desktopApp:prepareWorkerLauncher cyclonedxBom sourceArchive
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

New-Item -ItemType Directory -Force -Path $windowsReleaseDirectory | Out-Null
New-Item -ItemType Directory -Force -Path $sourceReleaseDirectory | Out-Null
New-Item -ItemType Directory -Force -Path $securityReleaseDirectory | Out-Null
New-Item -ItemType Directory -Force -Path $securityReportDirectory | Out-Null
$generatedSourceArchive = Join-Path $buildDirectory "source\ArxSilex-MapTools-Converter-$version-src.zip"
if (-not (Test-Path -LiteralPath $generatedSourceArchive)) {
    throw "Quellpaket wurde nicht erzeugt: $generatedSourceArchive"
}
Copy-Item -LiteralPath $generatedSourceArchive -Destination $sourceReleaseDirectory -Force
$portableDirectory = Join-Path $buildDirectory "desktopApp\compose\binaries\main\app\ArxSilex MapTools Converter"
Copy-Item -LiteralPath (Join-Path $projectRoot "LICENSE") -Destination $portableDirectory -Force
Copy-Item -LiteralPath (Join-Path $projectRoot "NOTICE.md") -Destination $portableDirectory -Force
$portableArchive = Join-Path $windowsReleaseDirectory "ArxSilex-MapTools-Converter-$version-windows-x64.zip"
Compress-Archive -Path $portableDirectory -DestinationPath $portableArchive -Force

$generatedSbom = Join-Path $buildDirectory "root\reports\cyclonedx\bom.json"
if (-not (Test-Path -LiteralPath $generatedSbom)) {
    throw "CycloneDX-SBOM wurde nicht erzeugt: $generatedSbom"
}
$sbomName = "ArxSilex-MapTools-Converter-$version-windows-x64.cdx.json"
Copy-Item -LiteralPath $generatedSbom -Destination (Join-Path $securityReportDirectory $sbomName) -Force
Copy-Item -LiteralPath $generatedSbom -Destination (Join-Path $securityReleaseDirectory $sbomName) -Force

Write-Host "Windows- und Quellpaket erzeugt: $releaseDirectory"
