import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id 'org.jetbrains.kotlin.jvm' version '2.2.10' apply false
    id 'org.jetbrains.kotlin.plugin.serialization' version '2.2.10' apply false
    id 'org.jetbrains.kotlin.plugin.compose' version '2.2.10' apply false
    id "io.gitlab.arturbosch.detekt" version "1.23.7"
    id "org.jetbrains.kotlinx.kover" version "0.8.3"
    id "org.sonarqube" version "5.1.0.4882"
}

ext {
    kotlin_version = '2.2.10'
    kotlinx_coroutines_version = '1.9.0'
    kotlinx_serialization_version = '1.7.3'
    kotlinx_datetime_version = '0.6.2'
    junit_version = '5.11.0'
    sqlite_jdbc_version = '3.47.1.0'

    exposed_version = '0.57.0'
    ktor_version = '3.0.3'
    hoplite_version = '2.8.2'
    sqlite_jdbc_version = '3.47.1.0'
}

allprojects {
    group = 'com.correx'
    version = '0.1.0'

    repositories {
        mavenCentral()
        google()
    }
}

subprojects {
    pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {

        apply plugin: "io.gitlab.arturbosch.detekt"
        apply plugin: "org.jetbrains.kotlinx.kover"

        // Unique jar base names from the full project path (core:inference -> core-inference).
        // Sibling modules share leaf names (core:inference vs infrastructure:inference, core:tools
        // vs infrastructure:tools), which collide in the flat application-plugin lib dir and drop
        // classes at runtime. Derive per-path names so every jar lands distinctly.
        tasks.withType(Jar).configureEach {
            archiveBaseName = project.path.substring(1).replace(':', '-')
        }

        detekt {
            toolVersion = "1.23.7"
            config.setFrom("$rootDir/detekt.yml")
            buildUponDefaultConfig = true
            ignoreFailures = false
        }

        kover {
            reports {
                verify {
                    rule {
                        minBound(70)
                    }
                }
                filters {
                    excludes {
                        classes("*Generated*")
                    }
                }
            }
        }

        tasks.named("check") {
            dependsOn("test")
            dependsOn("detekt")
            dependsOn("koverVerify")
        }
    }

    plugins.withId('org.jetbrains.kotlin.jvm') {

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

        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
            implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version"
            implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinx_serialization_version"
            implementation "org.xerial:sqlite-jdbc:$sqlite_jdbc_version"
            implementation "org.jetbrains.kotlinx:kotlinx-datetime:$kotlinx_datetime_version"
            testImplementation "org.junit.jupiter:junit-jupiter:$junit_version"
        }

        test {
            useJUnitPlatform()
            testLogging {
                events "passed", "skipped", "failed", "standardOut", "standardError"
                showStandardStreams = true
                exceptionFormat = 'full'
            }
            afterSuite { desc, result ->
                if (!desc.parent) { // Only log for the final root suite
                    def duration = (result.endTime - result.startTime) / 1000

                    println """
                    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                    tests:   ${result.testCount}
                    passed:  ${result.successfulTestCount}
                    failed:  ${result.failedTestCount}
                    skipped: ${result.skippedTestCount}
                    
                    result:  ${result.resultType}
                    time:    ${duration}s
                    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
                    """
                }
            }
        }

        tasks.withType(Detekt).configureEach {
            reports {
                html.required.set(true)
                xml.required.set(true)
                txt.required.set(false)
            }
        }

        tasks.withType(KotlinCompile).configureEach {
            compilerOptions {
                jvmTarget = JvmTarget.JVM_21
                freeCompilerArgs.add("-Xcontext-receivers")
            }
        }
    }
}