/*
 * Copyright 2024 Google LLC
 *
 * 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
 *
 *     https://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 {
    id "java"
    id "java-library"
    id "idea"
    id "jacoco"
    id "com.diffplug.spotless" version "6.25.0"
    id "com.github.johnrengelman.shadow" version "8.1.1"
}

repositories {
    mavenCentral()
    gradlePluginPortal()
}

group "com.google.cloud.solutions.dataflow"
version "1.0"

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
        vendor = JvmVendorSpec.ADOPTIUM
    }
}

dependencies {
    // Apache Beam dependencies.
    implementation "org.apache.beam:beam-sdks-java-core:${beamVersion}"
    implementation "org.apache.beam:beam-sdks-java-extensions-avro:${beamVersion}"
    runtimeOnly "org.apache.beam:beam-runners-google-cloud-dataflow-java:${beamVersion}"

    // Google Cloud and API dependencies.
    implementation "com.google.api:gax:2.53.0"
    implementation "com.google.cloud:google-cloud-spanner:6.74.0"
    implementation "com.google.cloud:google-cloud-core:2.43.0"
    implementation "org.apache.beam:beam-sdks-java-io-google-cloud-platform:${beamVersion}"
    testImplementation "org.apache.beam:beam-sdks-java-extensions-google-cloud-platform-core:${beamVersion}"

    // Auto-value dependencies.
    implementation "com.google.auto.value:auto-value:${autoValueVersion}"
    annotationProcessor "com.google.auto.value:auto-value:${autoValueVersion}"
    implementation "com.google.auto.value:auto-value-annotations:${autoValueVersion}"

    // Common dependencies.
    compileOnly "org.checkerframework:checker-qual:3.42.0"
    implementation "javax.annotation:javax.annotation-api:1.3.2"
    implementation "com.google.guava:guava:33.3.0-jre"

    // Test dependencies.
    testImplementation "junit:junit:4.13.2"
    testImplementation "com.google.truth:truth:1.4.4"
    testImplementation "org.hamcrest:hamcrest:3.0"
    testImplementation "org.mockito:mockito-core:5.13.0"
    testImplementation "org.slf4j:slf4j-api:${slf4jVersion}"
    testImplementation "org.slf4j:slf4j-simple:${slf4jVersion}"

    // Test containers dependencies.
    testImplementation "org.testcontainers:testcontainers:${testContainersVersion}"
    testImplementation "org.testcontainers:gcloud:${testContainersVersion}"

    // Apache Beam test dependencies.
    testImplementation "org.apache.beam:beam-it-truthmatchers:${beamVersion}"
    testImplementation "org.apache.beam:beam-runners-direct-java:${beamVersion}"
    testImplementation "org.apache.beam:beam-it-google-cloud-platform:${beamVersion}"
    testImplementation "org.apache.beam:beam-sdks-java-extensions-google-cloud-platform-core:${beamVersion}"
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.deprecation = true
    options.compilerArgs.add("--enable-preview")
}

shadowJar {
    zip64 = true
    mergeServiceFiles()
    archiveBaseName = "dataflow-gcs-avro-to-spanner-scd"
}

test {
    dependsOn cleanTest
    testLogging.showStandardStreams = true
    finalizedBy jacocoTestReport
    maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
    jvmArgs("--enable-preview")
}

jacoco {
    toolVersion = "0.8.12"
}

jacocoTestReport {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            fileTree(dir: it, exclude: ["**/*AutoValue_*.class"])
        }))
    }
}

spotless {
    format "dockerfile", {
        target "**/*.dockerfile", "**/Dockerfile"
        endWithNewline()
    }

    format "gradle", {
        target "**/*.gradle"
        indentWithSpaces(2)
        trimTrailingWhitespace()
        endWithNewline()
    }

    java {
        googleJavaFormat("1.24.0").reflowLongStrings().reorderImports(true)
        removeUnusedImports()
        importOrder()
        trimTrailingWhitespace()
        endWithNewline()
    }
}
