$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"
$kotlinCache = Join-Path $projectRoot "build-windows\.kotlin"
$env:GRADLE_USER_HOME = Join-Path $projectRoot ".gradle-user"
$bootstrap = Join-Path $PSScriptRoot "bootstrap-dependencies.ps1"

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 --project-cache-dir $projectCache `
    '-Pmaptools.buildRoot=../build-validation' `
    "-Pkotlin.project.persistent.dir=$kotlinCache" `
    :core:desktopTest test :desktopApp:createDistributable
exit $LASTEXITCODE
