/*
 * 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.
 */

import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
    id "java"
    id "com.diffplug.spotless" version "6.25.0"
    id "org.springframework.boot" version "3.2.2"
    id "jacoco"
    id "com.google.cloud.tools.jib" version "3.4.0"
    id "idea"
}

apply plugin: "io.spring.dependency-management"

group = "com.google.cloud.solutions"
version = "0.1.1-SNAPSHOT"

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    gradlePluginPortal()
}

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

dependencies {

    // Commons library deps
    implementation "com.google.guava:guava:33.0.0-jre"
    implementation "org.apache.commons:commons-lang3:3.13.0"

    // Logging framework
    implementation "com.google.flogger:flogger:${floggerVersion}"
    implementation "com.google.flogger:google-extensions:${floggerVersion}"
    runtimeOnly "com.google.flogger:flogger-system-backend:${floggerVersion}"

    // Google Cloud Service Client Libraries
    implementation "com.google.cloud:google-cloud-storage:${cloudStorageClientVersion}"

    //Spring deps
    implementation "org.springframework.boot:spring-boot-starter-web"
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"

    // Test Deps
    testImplementation "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}"
    testImplementation "com.google.truth:truth:${truthVersion}"
    testImplementation "org.springframework.boot:spring-boot-starter-test"
}

jib {
    from {
        image = "eclipse-temurin:21-jre-alpine@sha256:${termurin21JreImageHash}"
    }
}

tasks.withType(JavaCompile).configureEach {
    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
    options.encoding = "UTF-8"
}

jacoco {
    toolVersion = "0.8.9"
}

test {
    useJUnit()
    dependsOn cleanTest
    finalizedBy jacocoTestReport
    testLogging {
        events "passed", "skipped", "failed", "standardOut", "standardError"
        showStandardStreams = true
        exceptionFormat = TestExceptionFormat.FULL
    }
}

jacocoTestReport {
    reports {
        csv.required = false
        xml.required = true
        html.required = false
    }
}

def licenseLines = new File("ApacheHeader").readLines()
def licenseJava = "/*\n" + licenseLines.collect(ln -> " *" + (ln.matches("^.+")? " " : "") + ln).join("\n") + "\n */\n\n"
def licenseWithForwardSlashes = "//\n" + licenseLines.collect(ln -> "//" + (ln.matches("^.+")? " " : "") + ln).join("\n") + "\n//\n\n"

spotless {
    java {
        target "**/*.java"
        targetExclude "third_party/**"
        googleJavaFormat("1.22.0")
        removeUnusedImports()
        importOrder()
        trimTrailingWhitespace()
        endWithNewline()
        licenseHeader(licenseJava)
    }

    kotlin {
        target "**/*.kt"
        ktlint()
        trimTrailingWhitespace()
        indentWithSpaces(2)
        endWithNewline()
        licenseHeader(licenseJava)
    }

    freshmark {
        target '*.md'
        indentWithSpaces(2)
        trimTrailingWhitespace()
        endWithNewline()
    }

    format "proto", {
        target "**/*.proto"
        licenseHeader(licenseWithForwardSlashes, "syntax")
        indentWithSpaces(2)
        trimTrailingWhitespace()
        endWithNewline()
    }


    format "gradle", {
        target "**/*.gradle"
        indentWithSpaces(2)
        trimTrailingWhitespace()
        endWithNewline()
        licenseHeader(licenseJava, "(import|plugins|rootProject)")
    }

    format "misc", {
        target "**/.gitignore", "*.xml", "src/**/*.xml"
        indentWithSpaces(2)
        trimTrailingWhitespace()
        endWithNewline()
    }
}
