plugins {
    id 'java-library'
    id 'maven-publish'
    id 'signing'
    id "com.diffplug.spotless" version "7.2.1"
    id "io.github.gradle-nexus.publish-plugin" version "2.0.0"
}

group = 'io.flipt'
// x-release-please-start-version
version = '1.3.0'
// x-release-please-end
description = 'Flipt Server SDK'

repositories {
    mavenCentral()
}

java {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    withSourcesJar()
    withJavadocJar()
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.20.0'
    implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.20.0'
    implementation 'com.squareup.okhttp3:okhttp:5.1.0'
    testImplementation platform('org.junit:junit-bom:5.11.4')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.20.0'
    api 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.19.2'
}

spotless {
    java {
        importOrder()

        removeUnusedImports()

        cleanthat()

        googleJavaFormat()

        formatAnnotations()
    }
}

tasks.named('test', Test) {
    useJUnitPlatform()

    maxHeapSize = '1G'

    testLogging {
        events "passed"
    }
}

test {
    useJUnitPlatform()
}

publishing {
    publications {
        maven(MavenPublication) {
            artifactId = 'flipt-java'
            from components.java

            pom {
                name = 'Flipt Java SDK'
                description = 'Flipt Java SDK'
                url = 'https://github.com/flipt-io/flipt-server-sdks/tree/main/flipt-java'

                licenses {
                    license {
                        name = 'MIT'
                        url = 'https://opensource.org/license/mit/'
                    }
                }

                developers {
                    developer {
                        id = 'flipt-io'
                        name = 'Flipt'
                        email = 'devs@flipt.io'
                    }
                }

                scm {
                    connection = 'scm:git:git://github.com/flipt-io/flipt-server-sdks.git'
                    developerConnection = 'scm:git:ssh://github.com/flipt-io/flipt-server-sdks.git'
                    url = 'https://github.com/flipt-io/flipt-server-sdks/tree/main/flipt-java'
                }
            }
        }
    }
    repositories {
        maven {
            name = 'ossrh-staging-api'
            url = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
            credentials {
                username = System.getenv("SONATYPE_PORTAL_USERNAME")
                password = System.getenv("SONATYPE_PORTAL_PASSWORD")
            }
        }
    }
}

signing {
    def signingKey = System.getenv('PGP_PRIVATE_KEY')
    def signingPassphrase = System.getenv('PGP_PASSPHRASE')
    useInMemoryPgpKeys(signingKey, signingPassphrase)
    sign publishing.publications.maven
}

nexusPublishing {
    repositories {
        sonatype {
            nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
            snapshotRepositoryUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/content/repositories/snapshots/"))
            username = System.getenv("SONATYPE_PORTAL_USERNAME")
            password = System.getenv("SONATYPE_PORTAL_PASSWORD")
        }
    }
    // Configure transition checks for auto-release
    transitionCheckOptions {
        maxRetries.set(60)  // Number of times to retry
        delayBetween.set(java.time.Duration.ofSeconds(10))  // Time between retries
    }
}
