import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.proto

plugins {
    alias(libs.plugins.luach.android.library)
    alias(libs.plugins.luach.android.hilt)
    alias(libs.plugins.protobuf)
}

android {
    namespace = "com.applemand.zmanim.core.settings"

    // protobuf-gradle-plugin places generated java/kotlin under build/generated/source/proto/<variant>/{java,kotlin}
    // Register per-variant so the release compile doesn't see debug outputs (which causes
    // Gradle "implicit dependency" validation failures in recent Gradle versions).
    sourceSets {
        getByName("debug") {
            java.srcDirs(
                "build/generated/source/proto/debug/java",
                "build/generated/source/proto/debug/kotlin",
            )
        }
        getByName("release") {
            java.srcDirs(
                "build/generated/source/proto/release/java",
                "build/generated/source/proto/release/kotlin",
            )
        }
    }
}

protobuf {
    protoc { artifact = libs.protobuf.protoc.get().toString() }
    generateProtoTasks {
        all().forEach { task ->
            task.builtins {
                create("java") { option("lite") }
                create("kotlin") { option("lite") }
            }
        }
    }
}

// KSP for main-variant sources must run after the matching proto generation so
// the generated UserSettings class is on KSP's classpath. Scope narrowly to
// avoid pulling in test-variant proto tasks, which would form a cycle.
afterEvaluate {
    tasks.named("kspDebugKotlin").configure { dependsOn("generateDebugProto") }
    tasks.named("kspReleaseKotlin").configure { dependsOn("generateReleaseProto") }
}

dependencies {
    implementation(project(":core:common"))
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.datastore)
    api(libs.protobuf.kotlin.lite)
    implementation(libs.kotlinx.coroutines.android)

    testImplementation(libs.junit4)
    testImplementation(libs.truth)
    testImplementation(libs.kotlinx.coroutines.test)
    testImplementation(libs.turbine)
}
