/*
 * Copyright 2020 Aiven Oy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

plugins {
    // https://docs.gradle.org/current/userguide/java_library_plugin.html
    id "java-library"

    // https://docs.gradle.org/current/userguide/checkstyle_plugin.html
    id "checkstyle"

    // https://docs.gradle.org/current/userguide/idea_plugin.html
    id "idea"

    // https://docs.gradle.org/current/userguide/jacoco_plugin.html
    id "jacoco"

    // https://docs.gradle.org/current/userguide/distribution_plugin.html
    id "distribution"

    // https://docs.gradle.org/current/userguide/publishing_maven.html
    id "maven-publish"

    // https://docs.gradle.org/current/userguide/signing_plugin.html
    id "signing"
}

group = "io.aiven"

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11

    withJavadocJar()
    withSourcesJar()
}

wrapper {
    distributionType = 'ALL'
    doLast {
        def sha256Sum = new String(new URL("${distributionUrl}.sha256").bytes)
        propertiesFile << "distributionSha256Sum=${sha256Sum}\n"
        println "Added checksum to wrapper properties"
    }
}

compileJava {
    options.compilerArgs = ["-Xlint:all", "-Werror"]
}

jacoco {
    toolVersion = "0.8.7"
}

repositories {
    mavenCentral()
    // For kafka-avro-serializer and kafka-connect-avro-converter
    maven {
        url "https://packages.confluent.io/maven"
    }
}

ext {
    kafkaVersion = "1.1.0"
    // Compatible with Kafka version:
    // https://docs.confluent.io/current/installation/versions-interoperability.html
    confluentPlatformVersion = "4.1.4"
    // Align with version used by commons
    avroConverterVersion = "7.2.2"
    aivenConnectCommonsVersion = "0.12.0"

    amazonS3Version = "1.12.729"
    amazonSTSVersion = "1.12.729"
    slf4jVersion = "1.7.36"
    junitVersion = "5.10.2"
    testcontainersVersion = "1.19.8"
    localstackVersion = "0.2.23"
    wireMockVersion = "2.35.0"
    mockitoVersion = "5.12.0"
}

sourceSets {
    integrationTest {
        java {
            srcDirs = ['src/integration-test/java']
        }
        resources {
            srcDirs = ['src/integration-test/resources']
        }

        compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
        runtimeClasspath += output + compileClasspath
    }
}

idea {
    module {
        testSourceDirs += project.sourceSets.integrationTest.java.srcDirs
        testSourceDirs += project.sourceSets.integrationTest.resources.srcDirs
    }
}

configurations {
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntime.extendsFrom testRuntimeClasspath
}

dependencies {
    compileOnly "org.apache.kafka:connect-api:$kafkaVersion"
    compileOnly "org.apache.kafka:connect-runtime:$kafkaVersion"

    implementation "org.slf4j:slf4j-api:$slf4jVersion"
    implementation "com.amazonaws:aws-java-sdk-s3:$amazonS3Version"
    implementation "com.amazonaws:aws-java-sdk-sts:$amazonSTSVersion"
    implementation "io.aiven:commons-for-apache-kafka-connect:$aivenConnectCommonsVersion"

    testImplementation "org.xerial.snappy:snappy-java:1.1.10.5"
    testImplementation "com.github.luben:zstd-jni:1.5.6-3"

    testImplementation "org.apache.kafka:connect-api:$kafkaVersion"
    testImplementation "org.apache.kafka:connect-runtime:$kafkaVersion"
    testImplementation "org.apache.kafka:connect-json:$kafkaVersion"
    testImplementation "org.slf4j:slf4j-simple:$slf4jVersion"

    testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
    testImplementation 'org.assertj:assertj-core:3.26.3'

    testImplementation "io.findify:s3mock_2.11:0.2.6"

    testImplementation "org.mockito:mockito-core:$mockitoVersion"

    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
    testImplementation "org.mockito:mockito-junit-jupiter:$mockitoVersion"

    testRuntimeOnly "ch.qos.logback:logback-classic:1.5.6"

    integrationTestImplementation "cloud.localstack:localstack-utils:$localstackVersion"
    integrationTestImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
    integrationTestImplementation "org.testcontainers:kafka:$testcontainersVersion" // this is not Kafka version
    integrationTestImplementation "org.testcontainers:localstack:$testcontainersVersion"
    integrationTestImplementation "com.github.tomakehurst:wiremock-jre8:$wireMockVersion"

    // TODO: add avro-converter to ConnectRunner via plugin.path instead of on worker classpath
    integrationTestImplementation("io.confluent:kafka-connect-avro-converter:$avroConverterVersion") {
        exclude group: "org.apache.kafka", module: "kafka-clients"
    }

    integrationTestImplementation "org.apache.avro:avro:1.11.3"

    testImplementation ("org.apache.parquet:parquet-tools:1.11.2") {
        exclude group: "org.slf4j", module: "slf4j-api"
    }
    testImplementation("org.apache.hadoop:hadoop-mapreduce-client-core:3.4.0") {
        exclude group: "org.apache.hadoop", module: "hadoop-yarn-client"
        exclude group: "org.apache.hadoop.thirdparty", module: "hadoop-shaded-protobuf_3_7"
        exclude group: "com.google.guava", module: "guava"
        exclude group: "commons-cli", module: "commons-cli"
        exclude group: "org.apache.commons", module: "commons-math3"
        exclude group: "org.apache.httpcomponents", module: "httpclient"
        exclude group: "commons-codec", module: "commons-codec"
        exclude group: "commons-io", module: "commons-io"
        exclude group: "commons-net", module: "commons-net"
        exclude group: "org.eclipse.jetty"
        exclude group: "org.eclipse.jetty.websocket"
        exclude group: "javax.servlet"
        exclude group: "javax.servlet.jsp"
        exclude group: "javax.activation"
        exclude group: "com.sun.jersey"
        exclude group: "log4j"
        exclude group: "org.apache.commons", module: "commons-text"
        exclude group: "org.slf4j", module: "slf4j-api"
        exclude group: "org.apache.hadoop", module: "hadoop-auth"
        exclude group: "org.apache.hadoop", module: "hadoop-yarn-api"
        exclude group: "com.google.re2j"
        exclude group: "com.google.protobuf"
        exclude group: "com.google.code.gson"
        exclude group: "com.jcraft"
        exclude group: "org.apache.curator"
        exclude group: "org.apache.zookeeper"
        exclude group: "org.apache.htrace"
        exclude group: "com.google.code.findbugs"
        exclude group: "org.apache.kerby"
        exclude group: "com.fasterxml.jackson.core"
        exclude group: "com.fasterxml.woodstox", module: "woodstox-core:5.0.3"
        exclude group: "org.apache.avro", module: "avro"
        exclude group: "org.apache.hadoop", module: "hadoop-yarn-common"
        exclude group: "com.google.inject.extensions", module: "guice-servlet"
        exclude group: "io.netty", module: "netty"
    }

    // Make test utils from 'test' available in 'integration-test'
    integrationTestImplementation sourceSets.test.output
    integrationTestImplementation "org.awaitility:awaitility:4.2.1"
}

checkstyle {
    toolVersion "8.29"
    getConfigDirectory().set(rootProject.file("checkstyle/"))
}

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath

    dependsOn distTar
    shouldRunAfter test

    useJUnitPlatform()

    // Run always.
    outputs.upToDateWhen { false }
    // Pass the distribution file path to the tests.
    systemProperty("integration-test.distribution.file.path", distTar.archiveFile.get().asFile.path)
}

test {
    useJUnitPlatform()
}

distributions {
    main {
        contents {
            from jar
            from configurations.runtimeClasspath

            into("/") {
                from projectDir
                include "version.txt", "README*", "LICENSE*", "NOTICE*", "licenses/"
                include "config/"
            }
        }
    }
}

jar {
    manifest {
        attributes(
                'Version': "${project.version}"
        )
    }
}

javadoc {
    options.addBooleanOption('html5', true)
    // disable missing javadoc lint and show only warning and error messages
    options.addStringOption('Xdoclint:all,-missing', '-quiet')
}


processResources {
    filesMatching('s3-connector-for-apache-kafka-version.properties') {
        expand(version: version)
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId = getGroup()
            artifactId = "s3-connector-for-apache-kafka"
            version = getVersion()

            from components.java

            pom {
                name = "Aiven's S3 Sink Connector for Apache Kafka"
                description = "Aiven's S3 Sink Connector for Apache Kafka"
                url = "https://github.com/aiven-open/s3-connector-for-apache-kafka"
                organization {
                    name = "Aiven Oy"
                    url = "https://aiven.io"
                }

                licenses {
                    license {
                        name = "Apache 2.0"
                        url = "http://www.apache.org/licenses/LICENSE-2.0"
                        distribution = "repo"
                    }
                }

                developers {
                    developer {
                        id = 'aiven'
                        name = 'Aiven Opensource'
                        email = 'opensource@aiven.io'
                    }
                }

                scm {
                    connection = 'scm:git:git://github.com:aiven/s3-connector-for-apache-kafka.git'
                    developerConnection = 'scm:git:ssh://github.com:aiven/s3-connector-for-apache-kafka.git'
                    url = 'https://github.com/aiven-open/s3-connector-for-apache-kafka'
                }
            }
        }
    }

    repositories {
        maven {
            name="sonatype"

            def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
            def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            credentials(PasswordCredentials)
        }
    }
}

signing {
    sign publishing.publications.maven
    useGpgCmd()
    // Some issue in the plugin:
    // GPG outputs already armored signatures. The plugin also does armoring for `asc` files.
    // This results in double armored signatures, i.e. garbage.
    // Override the signature type provider to use unarmored output for `asc` files, which works well with GPG.
    signatureTypes = new AbstractSignatureTypeProvider() {
        {
            BinarySignatureType binary = new BinarySignatureType() {
                @Override
                String getExtension() {
                    return "asc";
                }
            }
            register(binary);
            setDefaultType(binary.getExtension());
        }
    }
}
