plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    namespace 'com.thefeed.android'
    compileSdk 35

    defaultConfig {
        applicationId 'com.thefeed.android'
        minSdk 24
        targetSdk 35
        // versionCode auto-increments from git commit count so each build can be installed over the previous one
        versionCode = { ->
            try {
                "git rev-list --count HEAD".execute([], rootDir).text.trim().toInteger()
            } catch (ignored) {
                1
            }
        }()
        versionName = { ->
            try {
                def tag = "git describe --tags --always --dirty".execute([], rootDir).text.trim()
                tag ?: "dev"
            } catch (ignored) {
                "dev"
            }
        }()

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    signingConfigs {
        // Persistent project keystore — ensures all builds (debug & release) share the same signature
        // so you can install updates without uninstalling first.
        // For CI: override with env vars KEYSTORE_BASE64, KEYSTORE_PASSWORD, KEY_ALIAS, KEY_PASSWORD
        release {
            def ksFile = file("keystore.jks")
            if (ksFile.exists()) {
                storeFile ksFile
                storePassword System.getenv("KEYSTORE_PASSWORD") ?: "thefeed123"
                keyAlias System.getenv("KEY_ALIAS") ?: "thefeed"
                keyPassword System.getenv("KEY_PASSWORD") ?: "thefeed123"
            } else {
                // Fallback to debug keystore for consistent local/CI builds without the project keystore
                def debugKs = new File(System.getProperty("user.home"), ".android/debug.keystore")
                storeFile debugKs
                storePassword "android"
                keyAlias "androiddebugkey"
                keyPassword "android"
            }
        }
    }

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

    splits {
        abi {
            enable true
            reset()
            include 'arm64-v8a', 'armeabi-v7a'
            universalApk false
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.15.0'
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.activity:activity-ktx:1.10.1'
}
