import org.springframework.boot.gradle.tasks.bundling.BootBuildImage

plugins {
    application
    id("com.knecon.fforesight.java-conventions")
    id("org.springframework.boot") version "3.1.5"
    id("io.spring.dependency-management") version "1.1.3"
    id("org.sonarqube") version "4.3.0.3225"
    id("io.freefair.lombok") version "8.4"
//    id("org.graalvm.buildtools.native") version "0.9.23"
}

description = "layoutparser-service-server"

configurations {
    all {
        exclude(group = "commons-logging", module = "commons-logging")
        exclude(group = "org.springframework.boot", module = "spring-boot-starter-log4j2")
        exclude(group = "com.iqser.red.commons", module = "logging-commons")
    }
}


val jacksonVersion = "2.15.2"
val pdfBoxVersion = "3.0.0"
val springBootStarterVersion = "3.1.5"

dependencies {
    implementation(project(":layoutparser-service-processor"))
    implementation(project(":layoutparser-service-internal-api"))

    implementation("com.iqser.red.commons:storage-commons:2.45.0")
    implementation("com.knecon.fforesight:tenant-commons:0.21.0")
    implementation("com.knecon.fforesight:tracing-commons:0.5.0")
    implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootStarterVersion}")
    implementation("org.springframework.boot:spring-boot-starter-amqp:${springBootStarterVersion}")
    implementation("com.amazonaws:aws-java-sdk-s3:1.12.536")
    implementation("org.springframework.cloud:spring-cloud-starter-openfeign:4.0.4")
    implementation("net.logstash.logback:logstash-logback-encoder:7.4")

    // for integration testing only
    testImplementation(project(":viewer-doc-processor"))
    testImplementation(project(":layoutparser-service-internal-api"))

    testImplementation("org.springframework.boot:spring-boot-starter-amqp:${springBootStarterVersion}")
    testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootStarterVersion}")
    testImplementation("com.iqser.red.service:persistence-service-shared-api-v1:2.144.0")
    testImplementation("com.iqser.red.commons:jackson-commons:1.3.0")
    testImplementation("com.fasterxml.jackson.module:jackson-module-afterburner:${jacksonVersion}")
    testImplementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${jacksonVersion}")
    testImplementation("org.apache.pdfbox:pdfbox:${pdfBoxVersion}")
    testImplementation("org.apache.pdfbox:pdfbox-tools:${pdfBoxVersion}")
}

/*
//TODO: implement native build using GRAALVM, currently the problem is, logback needs runtime configuration using reflections or something
//kinda weird behavior, since the buildArgs below seem to have no effect at all
//https://stackoverflow.com/questions/72770461/graalvm-native-image-can-not-compile-logback-dependencies

We traced the issue to storage-commons and there to azure blob storage, this is today an open bug with azure spring
https://github.com/Azure/azure-sdk-for-java/issues/36001

//AOT is springs ahead of time compilation, it is a code analysis which generates new code, therefore checkstyle and pmd can be disabled on them.
tasks.named<Checkstyle>("checkstyleAot") {
    enabled = false
}
tasks.named<Checkstyle>("checkstyleAotTest") {
    enabled = false
}
tasks.named<Pmd>("pmdAot") {
    enabled = false
}
tasks.named<Pmd>("pmdAotTest") {
    enabled = false
}
*/

sonarqube {
    properties {
        providers.gradleProperty("sonarToken").getOrNull()?.let { property("sonar.login", it) }
        property("sonar.host.url", "https://sonarqube.knecon.com")
    }
}

tasks.named<BootBuildImage>("bootBuildImage") {

    environment.put("BPE_DELIM_JAVA_TOOL_OPTIONS", " ")
    environment.put("BPE_APPEND_JAVA_TOOL_OPTIONS", "-Dfile.encoding=UTF-8")

    imageName.set("nexus.knecon.com:5001/ff/${project.name}:${project.version}")
    if (project.hasProperty("buildbootDockerHostNetwork")) {
        network.set("host")
    }
    docker {
        if (project.hasProperty("buildbootDockerHostNetwork")) {
            bindHostToBuilder.set(true)
        }
        verboseLogging.set(true)

        publishRegistry {
            username.set(providers.gradleProperty("mavenUser").getOrNull())
            password.set(providers.gradleProperty("mavenPassword").getOrNull())
            email.set(providers.gradleProperty("mavenEmail").getOrNull())
            url.set("https://nexus.knecon.com:5001/")
        }
    }
}
