import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import java.util.Properties

fun loadKeystoreProps(): Properties? {
    val f = rootProject.file("keystore.properties")
    if (!f.exists()) return null
    val p = Properties()
    f.inputStream().use { p.load(it) }
    return p
}

plugins {
    alias(libs.plugins.luach.android.application)
    alias(libs.plugins.compose.compiler)
    alias(libs.plugins.luach.android.hilt)
    alias(libs.plugins.kotlin.serialization)
    alias(libs.plugins.baselineprofile)
}

android {
    namespace = "com.applemand.zmanim"

    defaultConfig {
        applicationId = "com.applemand.zmanim"
        testInstrumentationRunner = "com.applemand.zmanim.LuachTestRunner"
        resourceConfigurations += setOf("en", "iw")
        vectorDrawables.useSupportLibrary = true
    }

    val keystoreProps = loadKeystoreProps()
    signingConfigs {
        if (keystoreProps != null) {
            create("release") {
                storeFile = file(keystoreProps.getProperty("storeFile"))
                storePassword = keystoreProps.getProperty("storePassword")
                keyAlias = keystoreProps.getProperty("keyAlias")
                keyPassword = keystoreProps.getProperty("keyPassword")
            }
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix = ".debug"
            versionNameSuffix = "-debug"
            isDebuggable = true
        }
        release {
            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            signingConfig = signingConfigs.findByName("release")
                ?: signingConfigs.getByName("debug")
        }
    }

    buildFeatures {
        compose = true
        buildConfig = true
    }

    bundle {
        language {
            enableSplit = true
        }
    }

    packaging {
        jniLibs {
            useLegacyPackaging = false
        }
    }
}

androidComponents {
    this as ApplicationAndroidComponentsExtension
    onVariants(selector().withBuildType("release")) { variant ->
        variant.packaging.resources.excludes.add("META-INF/*.version")
    }
}

dependencies {
    implementation(project(":core:common"))
    implementation(project(":core:design"))
    implementation(project(":core:ui"))
    implementation(project(":core:settings"))
    implementation(project(":core:datetime"))
    implementation(project(":core:jewcal"))
    implementation(project(":core:zmanim"))
    implementation(project(":core:location"))
    implementation(project(":core:notifications"))
    implementation(project(":core:calsync"))

    implementation(project(":feature:calendar"))
    implementation(project(":feature:zmanim"))
    implementation(project(":feature:settings"))
    implementation(project(":feature:locationpicker"))
    implementation(project(":feature:dafyomi"))
    implementation(project(":feature:ics"))

    implementation(platform(libs.compose.bom))
    implementation(libs.compose.ui)
    implementation(libs.compose.ui.graphics)
    implementation(libs.compose.ui.tooling.preview)
    implementation(libs.compose.foundation)
    implementation(libs.compose.material3)
    implementation(libs.compose.material3.adaptive)
    implementation(libs.compose.material3.adaptive.layout)
    implementation(libs.compose.material3.adaptive.navigation)
    implementation(libs.compose.material3.windowsizeclass)
    debugImplementation(libs.compose.ui.tooling)

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.lifecycle.runtime.compose)
    implementation(libs.androidx.lifecycle.viewmodel.compose)
    implementation(libs.androidx.navigation.compose)
    implementation(libs.androidx.hilt.navigation.compose)
    implementation(libs.androidx.splashscreen)
    implementation(libs.androidx.startup.runtime)
    implementation(libs.androidx.window)
    implementation(libs.androidx.work.runtime.ktx)
    implementation(libs.androidx.hilt.work)

    implementation(libs.kotlinx.coroutines.android)
    implementation(libs.kotlinx.serialization.json)
    implementation(libs.kotlinx.datetime)

    "baselineProfile"(project(":baselineprofile"))

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

    androidTestImplementation(libs.androidx.test.ext)
    androidTestImplementation(libs.androidx.test.espresso)
    androidTestImplementation(platform(libs.compose.bom))
    androidTestImplementation(libs.compose.ui.test.junit4)
    androidTestImplementation(libs.hilt.testing)
    debugImplementation(libs.compose.ui.test.manifest)
}
