apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"

// ✅ react-native-config MUST be applied AFTER com.android.application plugin
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

// (optional) only if you really need per-variant files
project.ext.envConfigFiles = [
    debug  : ".env",
    release: ".env"
]
/**
 * This is the configuration block to customize your React Native Android app.
 * By default you don't need to apply any configuration, just uncomment the lines you need.
 */
react {
    /* Folders */
    // root = file("../../")
    // reactNativeDir = file("../../node_modules/react-native")
    // codegenDir = file("../../node_modules/@react-native/codegen")
    // cliFile = file("../../node_modules/react-native/cli.js")

    /* Variants */
    // debuggableVariants = ["liteDebug", "prodDebug"]

    /* Bundling */
    // nodeExecutableAndArgs = ["node"]
    // bundleCommand = "ram-bundle"
    // bundleConfig = file(../rn-cli.config.js)
    // bundleAssetName = "MyApplication.android.bundle"
    // entryFile = file("../js/MyApplication.android.js")
    // extraPackagerArgs = []

    /* Hermes Commands */
    // hermesCommand = "$rootDir/my-custom-hermesc/bin/hermesc"
    // hermesFlags = ["-O", "-output-source-map"]

    /* Autolinking */
    autolinkLibrariesWithApp()
}

/**
 * Set this to true to Run Proguard on Release builds to minify the Java bytecode.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore (JSC)
 */
def jscFlavor = 'io.github.react-native-community:jsc-android:2026004.+'

// ✅ make hermesEnabled safe even if gradle.properties doesn't define it
def hermesEnabled = (project.findProperty("hermesEnabled") ?: "true")

android {
    splits {
        abi {
            reset()
            enable true
            universalApk false
            include "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
    }
    ndkVersion rootProject.ext.ndkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    compileSdk rootProject.ext.compileSdkVersion

    buildFeatures {
        buildConfig = true
    }

    namespace "com.reactnativestarter"
    defaultConfig {
        applicationId "com.reactnativestarter"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }

    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

dependencies {
    // The version of react-native is set by the React Native Gradle Plugin
    implementation("com.facebook.react:react-android")

    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}
