plugins {
    id 'signing'

    id 'pl.allegro.tech.build.axion-release' version '1.15.0'
    id 'org.jetbrains.kotlin.jvm' version '1.9.25'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.9.25'
    id 'org.jetbrains.kotlin.plugin.allopen' version '1.9.25'
    id 'org.jlleitschuh.gradle.ktlint' version '10.2.1'
    id 'org.jlleitschuh.gradle.ktlint-idea' version '10.2.0'
    id 'io.gitlab.arturbosch.detekt' version '1.23.6'
    id 'io.github.gradle-nexus.publish-plugin' version '1.0.0'
    id 'io.spring.dependency-management' version '1.1.6'
    id 'com.adarshr.test-logger' version '4.0.0'
}

scmVersion {
    tag {
        prefix = "envoy-control-"
    }
    versionCreator 'versionWithBranch'
}

allprojects {

    project.group = 'pl.allegro.tech.servicemesh'
    project.version = scmVersion.version

    repositories {
        mavenCentral()
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
            mavenContent { snapshotsOnly() }
        }
    }

    apply plugin: 'kotlin'
    apply plugin: 'kotlin-spring'
    apply plugin: 'io.spring.dependency-management'

    project.ext.versions = [
        java_controlplane: '1.0.49',
        spring_boot      : '3.4.5',
        grpc             : '1.48.1',
        ecwid_consul     : '1.4.1',
        toxiproxy        : '2.1.3',
        consul_recipes   : '0.9.1',
        bytebuddy        : '1.15.1',
        re2j             : '1.3',
        xxhash           : '0.10.1',
        dropwizard       : '4.2.26',
        okhttp           : '4.12.0'
    ]

    dependencyManagement {
        imports {
            mavenBom("org.springframework.boot:spring-boot-dependencies:3.4.5") {
                bomProperty("kotlin.version", "1.9.23")
            }
        }
    }
}

nexusPublishing {
    repositories {
        sonatype {
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
            username = System.getenv("SONATYPE_USERNAME")
            password = System.getenv("SONATYPE_PASSWORD")
        }
    }
}

subprojects {

    apply plugin: 'maven-publish'
    apply plugin: 'org.jlleitschuh.gradle.ktlint'
    apply plugin: 'io.gitlab.arturbosch.detekt'
    apply plugin: 'signing'

    ktlint {
        enableExperimentalRules = false
        verbose = true
        disabledRules = ["parameter-list-wrapping", "indent", "colon-spacing", "import-ordering"]
    }

    test {
        jvmArgs('-Djunit.jupiter.extensions.autodetection.enabled=true')
        useJUnitPlatform()
        testLogging {
            events "passed", "skipped", "failed"
            exceptionFormat = 'full'
        }
    }

    if (System.getenv("GPG_KEY_ID")) {
        signing {
            useInMemoryPgpKeys(
                System.getenv("GPG_KEY_ID"),
                System.getenv("GPG_PRIVATE_KEY"),
                System.getenv("GPG_PRIVATE_KEY_PASSWORD")
            )
            sign publishing.publications
        }
    }

    java {
        withSourcesJar()
        withJavadocJar()
    }

    publishing {
        publications {
            sonatype(MavenPublication) { publication ->
                from project.components.java
                pom {
                    name = project.name
                    description = 'Production-ready Control Plane for Service Mesh based on Envoy Proxy.'
                    url = 'https://github.com/allegro/envoy-control'
                    inceptionYear = '2019'

                    scm {
                        url = 'https://github.com/allegro/envoy-control'
                        connection = 'scm:git@github.com:allegro/envoy-control.git'
                        developerConnection = 'scm:git@github.com:allegro/envoy-control.git'
                    }

                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        }
                    }

                    developers {
                        developer {
                            id = 'skylab'
                            name = 'Team Skylab-Helix'
                        }
                    }
                }
            }
        }

    }

    kotlin {
        jvmToolchain(17)
    }

    dependencies {
        testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api'
        testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params'
        testImplementation group: 'org.assertj', name: 'assertj-core'
        testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine'
    }

    detekt {
        toolVersion = "1.23.6"
        input = files("src/main/kotlin", "src/test/kotlin")
        config = files("$rootDir/config/detekt/detekt.yml")
        buildUponDefaultConfig = true
    }
}
