import io.freefair.gradle.plugins.okhttp.tasks.UploadFile

plugins {
    id "org.springframework.boot" version "3.5.6" apply false
    id "io.freefair.git-version"
    id "io.freefair.okhttp"
    id "io.freefair.maven-central.validate-poms" apply false
    id "io.freefair.aggregate-javadoc"
    id 'org.jreleaser' version '1.20.0'
}

allprojects {

    group = "io.freefair.okhttp-spring-boot"

    plugins.withId("java") {
        java {
            withSourcesJar()
            withJavadocJar()
            toolchain {
                languageVersion = JavaLanguageVersion.of(17)
            }
        }

        tasks.named('jar', Jar) {
            into("META-INF") {
                from rootProject.file("LICENSE")
            }
        }

        dependencies {
            testRuntimeOnly "org.junit.platform:junit-platform-launcher"
            testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
        }
    }

    tasks.withType(JavaCompile).configureEach {
        options.compilerArgs += ['-Werror']
    }

    tasks.withType(Javadoc).configureEach {
        options.addBooleanOption "Xdoclint:-missing", true
    }

    plugins.withId("maven-publish") {

        project.apply plugin: "signing"
        project.apply plugin: "io.freefair.maven-central.validate-poms"

        signing {
            required = { !version.endsWith('SNAPSHOT') && gradle.taskGraph.hasTask("publish") }

            def signingKey = findProperty("signingKey")
            def signingPassword = findProperty("signingPassword")
            useInMemoryPgpKeys(signingKey, signingPassword)
        }

        publishing {
            publications.withType(MavenPublication) {
                pom {
                    name = provider { project.description }
                    description = provider { project.description }
                    url = "https://github.com/freefair/okhttp-spring-boot"
                    licenses {
                        license {
                            name = 'MIT License'
                            url = 'https://opensource.org/licenses/MIT'
                        }
                    }
                    developers {
                        developer {
                            id = 'larsgrefer'
                            name = 'Lars Grefer'
                            email = 'github@larsgrefer.de'
                            timezone = 'Europe/Berlin'
                        }
                    }
                    ciManagement {
                        system = 'GitHub Actions'
                        url = 'https://github.com/freefair/okhttp-spring-boot/actions'
                    }
                    issueManagement {
                        system = 'GitHub Issues'
                        url = 'https://github.com/freefair/okhttp-spring-boot/issues'
                    }
                    scm {
                        connection = 'scm:git:https://github.com/freefair/okhttp-spring-boot.git'
                        developerConnection = 'scm:git:git@github.com:freefair/okhttp-spring-boot.git'
                        url = 'https://github.com/freefair/okhttp-spring-boot'
                    }
                }
            }

            repositories {
                maven {
                    name = "mavenCentralStaging"
                    url = rootProject.layout.buildDirectory.dir('staging-deploy')
                }
            }
        }
    }
}

apply plugin: "maven-publish"

dependencies {
    javadoc project(":okhttp-spring-client")
    javadoc project(":okhttp-spring-boot-autoconfigure")
    javadoc project(":starters:okhttp-spring-boot-starter")

    javadocClasspath "org.springframework.boot:spring-boot-starter-actuator"
    javadocClasspath 'com.squareup.okhttp3:logging-interceptor'
}

tasks.named('javadoc', Javadoc) {
    options.locale "en_US"
    options.linkSource true
    options.links "https://square.github.io/okhttp/5.x/"
    failOnError = false
}

tasks.register('uploadDocumentation', UploadFile) {
    dependsOn ":javadocJar"
    username = "user"
    password = findProperty('freefairDocsPass')
    file = tasks.named("javadocJar", Jar).flatMap {it.archiveFile}
    url = "https://docs.freefair.io/api/$project.version?path=okhttp-spring-boot"
}

tasks.named('wrapper') {
    distributionType = Wrapper.DistributionType.BIN
}

jreleaser {
    project {
        license = 'MIT'
        inceptionYear = '2016'
    }
    signing {
        active = 'NEVER'
    }
    release {
        github {
            repoOwner = "freefair"
            name = "okhttp-spring-boot"
            tagName = "$version"
            skipTag = true
            overwrite = false
            update {
                enabled = true
            }
            releaseNotes {
                enabled = false
            }
            changelog {
                enabled = true
            }
        }
    }
    deploy {
        maven {
            mavenCentral {
                sonatype {
                    active = 'RELEASE'
                    namespace = "io.freefair"
                    url = 'https://central.sonatype.com/api/v1/publisher'
                    sign = false //Already done by Gradle
                    checksums = false //Already done by Gradle
                    stagingRepository('build/staging-deploy')
                }
            }
        }
    }
}
