//file:noinspection UnnecessaryQualifiedReference
plugins {
    id 'java-gradle-plugin'
    alias libs.plugins.kotlin.jvm
    alias libs.plugins.ktfmt
    alias libs.plugins.build.config
    alias libs.plugins.maven.publish
    alias libs.plugins.detekt
    alias libs.plugins.kotlinx.binaryCompatibilityValidator
    alias libs.plugins.android.lint
}

ktfmt {
    googleStyle()
    trailingCommaManagementStrategy.set(com.ncorti.ktfmt.gradle.TrailingCommaManagementStrategy.COMPLETE)
    removeUnusedImports.set(true)
}

mavenPublishing {
    pom {
        name = "App Platform Gradle Plugin"
    }
}

gradlePlugin {
    plugins {
        appPlatformPlugin {
            id = "software.amazon.app.platform"
            displayName = "App Platform Gradle Plugin"
            implementationClass = "software.amazon.app.platform.gradle.AppPlatformPlugin"
            description = "The Gradle plugin to make the integration of the App Platform easy."
        }
    }
}

buildConfig {
    buildConfigField(String, 'KOTLIN_INJECT_VERSION', libs.versions.kotlin.inject.asProvider().get())
    buildConfigField(String, 'KOTLIN_INJECT_ANVIL_VERSION', libs.versions.kotlin.inject.anvil.get())
    buildConfigField(String, 'APP_PLATFORM_GROUP', property('GROUP'))
    buildConfigField(String, 'APP_PLATFORM_VERSION', property('VERSION_NAME'))
    buildConfigField(String, 'MOLECULE_VERSION', libs.versions.molecule.get())
    buildConfigField(String, 'ANDROID_COMPOSE_VERSION', libs.versions.android.compose.version.get())
}

dependencies {
    implementation libs.kotlin.gradle.plugin.api

    // The Compose plugin is needed for Molecule and not Compose Multiplatform.
    implementation libs.kotlin.compose.gradle.plugin
    implementation libs.compose.gradle.plugin

    // This is needed to reference KspExperimental for experimental features.
    compileOnly libs.ksp.api
    implementation libs.ksp.gradle.plugin

    // compileOnly to not set a minimum version for any consumers of this Gradle plugin and
    // because AGP is purely optional. Usage of AGP APIs is gated by checks when the plugin
    // is applied.
    compileOnly libs.android.gradle.plugin.api

    // compileOnly, because not every consumer of this Gradle plugin will use KMP. All usages
    // are guarded by checks when the plugin is applied.
    compileOnly libs.kotlin.multiplatform.gradle.plugin

    // compileOnly to not force this dependency.
    compileOnly libs.metro.gradle.plugin

    lintChecks libs.androidx.lint.gradle
}

java {
    sourceCompatibility = libs.versions.jvm.compatibility.get()
    targetCompatibility = libs.versions.jvm.compatibility.get()
}

kotlin {
    compilerOptions {
        jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.fromTarget(libs.versions.jvm.compatibility.get()))
        allWarningsAsErrors.set(true)
    }

    explicitApi()
}

tasks.withType(ValidatePlugins).configureEach {
    it.enableStricterValidation = true
}

tasks.withType(io.gitlab.arturbosch.detekt.Detekt).configureEach {
    it.jvmTarget = libs.versions.jvm.compatibility.get()
    it.setSource(layout.files("src"))
}

//noinspection UnnecessaryQualifiedReference
tasks.withType(io.gitlab.arturbosch.detekt.DetektCreateBaselineTask).configureEach {
    it.jvmTarget = libs.versions.jvm.compatibility.get()
    it.setSource(layout.files("src"))
}

detekt {
    config.from(file('../gradle/detekt-config.yml'))
    buildUponDefaultConfig = true
}

tasks.register('release') {
    dependsOn('build', 'check', 'ktfmtCheck', 'detekt', 'apiCheck')
}
