plugins {
    id 'com.diffplug.spotless' version '6.25.0' apply false
    id 'org.kordamp.gradle.jandex' version '2.1.0' apply false
    id 'org.jreleaser' version '1.19.0'
    id 'net.researchgate.release' version '3.1.0'
}

wrapper {
    gradleVersion = '8.10.1'
}

String groupId = 'org.gitlab4j'
group = groupId

def repositoryPath = 'build/staging-deploy'
subprojects {
    apply plugin: 'java-library'
    apply plugin: 'com.diffplug.spotless'
    apply plugin: 'maven-publish'
    apply plugin: 'org.kordamp.gradle.jandex'

    group = groupId

    java {
        withJavadocJar()
        withSourcesJar()

        compileJava.options.encoding = "UTF-8"
        toolchain {
            languageVersion = JavaLanguageVersion.of(11)
        }
    }

    spotless {
        java {
            palantirJavaFormat()
            importOrder 'java', 'javax', 'jakarta', 'org', 'com', ''
            removeUnusedImports()
        }
    }

    repositories {
        // mavenLocal()
        mavenCentral()
    }

    tasks.named('test') {
        useJUnitPlatform()
    }

    tasks.named("javadoc") {
        inputs.files(tasks.getByPath("jandex").outputs.files)
    }

    publishing {
        repositories {
            maven {
                url = rootProject.file(repositoryPath)
            }
        }
    }
}

jreleaser {
    signing {
        active = 'RELEASE'
        armored = true
    }
    deploy {
        maven {
            mavenCentral {
                gitlab4j {
                    active = 'RELEASE'
                    namespace = 'org.gitlab4j'
                    url = 'https://central.sonatype.com/api/v1/publisher'
                    stagingRepository(repositoryPath)
                    retryDelay = 30
                    maxRetries = 150
                }
            }
        }
    }
    release {
        github {
            enabled = true
            repoOwner = "$githubRepositoryOwner"
            name = "$githubRepositoryName"
            tagName = '{{projectVersion}}'
            overwrite = true
            skipTag = true
            releaseNotes {
                enabled = true
            }
            changelog {
                enabled = false
            }
            milestone {
                close = true
            }
            issues {
                enabled = true
                comment = 'This issue has been resolved in `{{tagName}}` ([Release Notes]({{releaseNotesUrl}}))'
                applyMilestone = 'ALWAYS'

                label {
                    name = 'released'
                    color = '#FF0000'
                    description = 'Issue has been released'
                }
            }
        }
    }
}

release {
    buildTasks = [
        'checkLastVersionValue',
        'clean',
        'publish',
        'jreleaserDeploy'
    ]
    git {
        requireBranch.set('main')
    }
}

def checkLastVersionValueTask = tasks.register('checkLastVersionValue') {
    doLast {
        if(version.endsWith('SNAPSHOT')) {
            throw new GradleException("version '$version' ends with SNAPSHOT, this is not a release build!")
        }
        if(lastVersion != version) {
            throw new GradleException("lastVersion '$lastVersion' does not match version '$version', fix it in the 'gradle.properties' file.")
        }
    }
}

def updateLastVersionValueTask = tasks.register('updateLastVersionValue') {
    doLast {
        def propertiesFile = file('gradle.properties')
        def content = propertiesFile.text
        def newVersion = project.findProperty('release.releaseVersion') ?: version.replace('-SNAPSHOT', '')
        content = content.replaceAll("lastVersion=[0-9a-z\\.\\-]+", "lastVersion=" + newVersion)
        propertiesFile.text = content

        def readmeFile = file('README.md')
        def readme = readmeFile.text
        readme = readme.replace(lastVersion, newVersion)
        readmeFile.text = readme
    }
}

model {
    tasks.unSnapshotVersion {
        dependsOn updateLastVersionValueTask
    }
}
