import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.bundling.Zip

plugins {
    kotlin("multiplatform") version "2.4.10" apply false
    kotlin("jvm") version "2.4.10" apply false
    id("org.jetbrains.compose") version "1.12.0" apply false
    id("org.jetbrains.kotlin.plugin.compose") version "2.4.10" apply false
    id("org.cyclonedx.bom") version "3.4.1"
    id("io.github.ben-manes.versions") version "0.61.0"
}

allprojects {
    group = "de.arxsilex.maptools.converter"
    version = providers.gradleProperty("maptoolsVersion").get()

    tasks.withType<DependencyUpdatesTask>().configureEach {
        filterConfigurations = Spec { configuration ->
            !configuration.name.startsWith("composeHotReload")
        }
        rejectVersionIf {
            fun isStable(version: String): Boolean {
                val upper = version.uppercase()
                return listOf("RELEASE", "FINAL", "GA").any(upper::contains) ||
                    version.matches(Regex("^[0-9]+(?:\\.[0-9]+)*(?:[-.]?(?:JRE|ANDROID|R[0-9]+))?$", RegexOption.IGNORE_CASE))
            }
            !isStable(candidate.version) && isStable(currentVersion)
        }
    }
}

// Keep native artifacts separate when Windows and Linux build the same source tree through /mnt/d.
val buildRootPath = providers.gradleProperty("maptools.buildRoot").orElse("../build").get()
val releaseRootPath = providers.gradleProperty("maptools.releaseRoot").orElse("../release").get()
layout.buildDirectory = rootProject.layout.projectDirectory.dir("$buildRootPath/root")

subprojects {
    layout.buildDirectory = rootProject.layout.projectDirectory.dir("$buildRootPath/${project.name}")
}

tasks.register<Zip>("sourceArchive") {
    group = "distribution"
    description = "Erzeugt das plattformübergreifende Quelltextarchiv."
    archiveFileName.set("ArxSilex-MapTools-Converter-${project.version}-src.zip")
    destinationDirectory.set(rootProject.layout.projectDirectory.dir(releaseRootPath))
    from(rootProject.layout.projectDirectory.dir("..")) {
        include(
            "AGENTS.md",
            "CHANGELOG.md",
            "LICENSE",
            "NOTICE.md",
            "README.md",
            ".gitignore",
            "application/**",
            "documentation/**",
            "security/**",
            "scripts/**",
            "screenshots/**",
            "test-data/README.md",
            "test-data/local-datasets.properties.example",
            "release/README.md",
            "Linux/README.md",
        )
        exclude(
            ".gradle*/**",
            "**/.gradle*/**",
            ".kotlin*/**",
            "**/.kotlin*/**",
            ".tools/**",
            "application/.gradle*/**",
            "application/build/**",
            "application/build-validation/**",
            "application/**/build/**",
            "application/worker/libs/*.jar",
            "build*/**",
            "release/*.deb",
            "release/*.exe",
            "release/*.msi",
            "release/*.tar.gz",
            "release/*.zip",
            "release/SHA256SUMS*",
            "Linux/.gradle*/**",
            "Linux/.kotlin/**",
            "Linux/.tools/**",
            "Linux/build/**",
            "Linux/release/**",
            "security/reports/**",
            "temp/**",
        )
    }
}
