plugins {
    id 'java'
    id 'com.gradleup.shadow' version '8.3.5'
    id 'jacoco'
}

group = 'telegram.files'
version = '0.1.15'

repositories {
    mavenCentral()
}

dependencies {
    def osName = System.getProperty("os.name").toLowerCase()

    implementation 'io.vertx:vertx-core:4.5.11'
    implementation 'io.vertx:vertx-web:4.5.11'
    implementation 'io.vertx:vertx-jdbc-client:4.5.11'
    implementation 'io.vertx:vertx-sql-client-templates:4.5.11'
    implementation 'io.vertx:vertx-health-check:4.5.11'
    implementation 'org.xerial:sqlite-jdbc:3.47.1.0'
    implementation 'io.vertx:vertx-pg-client:4.5.11'
    implementation 'io.vertx:vertx-mysql-client:4.5.11'
    implementation 'com.ongres.scram:client:2.1'
    implementation 'io.agroal:agroal-api:1.16'
    implementation 'io.agroal:agroal-pool:1.16'
    implementation 'cn.hutool:hutool-core:5.8.34'
    implementation 'cn.hutool:hutool-log:5.8.34'
    implementation 'org.jooq:jool:0.9.15'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.3'
    // only for macos
    if (osName.contains("mac")) {
        implementation('io.netty:netty-resolver-dns-native-macos:4.1.115.Final') {
            artifact {
                name = 'netty-resolver-dns-native-macos'
                type = 'jar'
                classifier = System.getProperty("os.arch").contains("aarch64") ? "osx-aarch_64" : "osx-x86_64"
            }
        }
    }

    testImplementation 'io.vertx:vertx-junit5:4.5.11'
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    testImplementation 'org.mockito:mockito-core:5.15.2'
}

test {
    def envFile = file('../.env.test')
    if (envFile.exists()) {
        doFirst {
            envFile.eachLine {
                if (!it.startsWith("#") && !it.isEmpty()) {
                    def (key, value) = it.tokenize('=')
                    environment key, value
                }
                return
            }
        }
    }
    jvmArgs "-Djava.library.path=${System.getenv('TDLIB_PATH')}",
            "-XX:+EnableDynamicAgentLoading",
            "-javaagent:${configurations.testRuntimeClasspath.find { it.name.contains('mockito-core') }}"
    environment "APP_ROOT", "${testClassesDirs.asPath}"
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    dependsOn test
    reports {
        xml.required = true
        html.required = false
    }
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: "org/drinkless/tdlib/**")
        })
    }
}

tasks.build {
    dependsOn shadowJar
}

shadowJar {
    archiveBaseName.set('telegram-files')
    archiveClassifier.set('')
    archiveVersion.set('')
    manifest {
        attributes(
                'Main-Class': 'telegram.files.Start'
        )
    }
}

tasks.named('jar') {
    manifest {
        attributes(
                'Implementation-Title': project.name,
                'Implementation-Version': project.version,
                'Built-By': System.getProperty('user.name'),
                'Build-Time': new Date().format('yyyy-MM-dd HH:mm:ss'),
                'Git-Commit': getGitCommit()
        )
    }
}

static def getGitCommit() {
    try {
        return 'git rev-parse --short HEAD'.execute().text.trim()
    } catch (ignored) {
        return 'unknown'
    }
}
