import com.android.build.gradle.internal.lint.AndroidLintTask
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import org.sonarqube.gradle.SonarTask

plugins {
    alias(libs.plugins.versions)
    alias(libs.plugins.sonarqube)
    alias(libs.plugins.kotlin.kover)
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.android.library) apply false
    alias(libs.plugins.compose) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.kotlin.multiplatform) apply false
    alias(libs.plugins.kotlin.jvm) apply false
    alias(libs.plugins.kotlin.android) apply false
}

sonar {
    properties {
        property("sonar.projectKey", "YAEP")
        property("sonar.projectName", "YAEP")
        property("sonar.organization", "default")

        property(
            "sonar.coverage.jacoco.xmlReportPaths",
            "${layout.buildDirectory.asFile.get()}/reports/kover/report.xml"
        )
    }
}

tasks.withType<SonarTask>().configureEach {
    dependsOn(
        subprojects
            .flatMap { it.tasks.withType<AndroidLintTask>() }
            .filter { it.variantName != "release" }
    )
    dependsOn("koverXmlReport")
}

tasks.withType<KotlinJvmCompile>().configureEach {
    compilerOptions {
        jvmTarget.set(JvmTarget.fromTarget(libs.versions.jdk.get()))
    }
}

tasks.withType<DependencyUpdatesTask> {
    rejectVersionIf {
        isNonStable(candidate.version) && !isNonStable(currentVersion)
    }
}

fun isNonStable(version: String): Boolean {
    return version.contains("dev", ignoreCase = true)
            || version.contains("alpha", ignoreCase = true)
            || version.contains("beta", ignoreCase = true)
            || version.contains("RC", ignoreCase = true)
}

subprojects {
    tasks.withType<Test> {
        testLogging {
            events("passed", "skipped", "failed")
        }
    }
}

dependencies {
    kover(projects.domain)
    kover(projects.commonUI)
    kover(projects.desktop)
    kover(projects.android)
}