apply plugin: 'java-library'
apply plugin: 'maven-publish'
apply plugin: 'signing'

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

dependencies {
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1'
    implementation 'com.auth0:java-jwt:4.5.0'
    implementation 'org.bouncycastle:bcprov-jdk18on:1.81'

    testImplementation 'org.junit.jupiter:junit-jupiter:5.13.1'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
    testImplementation 'org.mockito:mockito-core:5.18.0'
}

repositories {
    mavenCentral()
}

java {
    withSourcesJar()
    withJavadocJar()
}

javadoc {
    source = sourceSets.main.allJava
}

signing {
    def signingKey = findProperty("signingKey")
    def signingPassword = findProperty("signingPassword")
    useInMemoryPgpKeys(signingKey, signingPassword)
    sign publishing.publications
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'com.apple.itunes.storekit'
            artifactId = 'app-store-server-library'

            from components.java
            pom {
                name = "App Store Server Library"
                description = "The Java server library for the App Store Server API and App Store Server Notifications"
                url = "https://github.com/apple/app-store-server-library-java/"
                licenses {
                    license {
                        name = "MIT License"
                        url = "https://github.com/apple/app-store-server-library-java/blob/main/LICENSE.txt"
                    }
                }
                developers {
                    developer {
                        id = 'app-store-server-library-project-authors'
                        name = 'App Store Server Library project authors'
                        email = 'app-store-server-library@group.apple.com'
                    }
                }
                scm {
                    connection = 'scm:git:git://github.com:apple/app-store-server-library-java.git'
                    developerConnection = 'scm:git:ssh://github.com:apple/app-store-server-library-java.git'
                    url = 'https://github.com/apple/app-store-server-library-java/'
                }
            }
        }
    }
    repositories {
        maven {
            credentials {
                username findProperty("mavenUsername")
                password findProperty("mavenPassword")
            }
            if(project.version.endsWith('-SNAPSHOT')) {
                url "https://central.sonatype.com/repository/maven-snapshots/"
            } else {
                url "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
            }
        }
    }
}

tasks.named('publish') {
    finalizedBy tasks.named('postRelease')
}

tasks.register('postRelease') {
    doLast {
        if (!project.version.endsWith('-SNAPSHOT')) {
            def username = findProperty("mavenUsername")
            def password = findProperty("mavenPassword")
            def url = "https://ossrh-staging-api.central.sonatype.com/manual/upload/defaultRepository/com.apple.itunes.storekit"
            def connection = new URL(url).openConnection() as HttpURLConnection
            connection.setRequestMethod("POST")
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
            connection.setRequestProperty('Authorization', 'Basic ' + "${username}:${password}".bytes.encodeBase64().toString())
            def responseCode = connection.responseCode
            if (responseCode == 200) {
                def response = connection.inputStream.text
                println "Success: $response"
            } else {
                def error = connection.errorStream?.text ?: "No error details"
                println "Error $responseCode: $error"
            }
        }
    }
}

test {
    useJUnitPlatform()
}
