plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {

    namespace "com.mindful.android"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion '27.0.12077973'

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    defaultConfig {
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        applicationId "com.mindful.android"
        minSdkVersion defaultMinSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }


    signingConfigs {
        release {
            if (System.getenv("KEYSTORE_FILE") != null) {
                storeFile file(System.getenv("KEYSTORE_FILE"))
                storePassword System.getenv("STORE_PASSWORD")
                keyAlias System.getenv("KEY_ALIAS")
                keyPassword System.getenv("KEY_PASSWORD")
            }
        }
    }

    buildTypes {
        release {
            ndk {
                debugSymbolLevel 'full'
            }
            resValue "string", "app_name", "Mindful"
            signingConfig signingConfigs.release

            // If you see "SigningConfig 'release' is missing required property 'storeFile'",  
            // it means your release keystore is not set up due to missing environment variables.  
            // To fix this locally, either:  
            // 1. Use `signingConfig signingConfigs.debug` (but DO NOT commit this change).  
            // 2. Set the following environment variables to keep signing secure:  
            //    - KEYSTORE_FILE  
            //    - STORE_PASSWORD  
            //    - KEY_ALIAS  
            //    - KEY_PASSWORD  
            // Avoid pushing any local changes to this Gradle file, as it may break CI/CD.  
        }

        debug {
            applicationIdSuffix ".debug"
            resValue "string", "app_name", "Mindful Debug"
            signingConfig System.getenv("KEYSTORE_FILE") != null ? signingConfigs.release : signingConfigs.debug
        }

        profile {
            // app_links library is causing error so commenting it out
            // applicationIdSuffix ".profile"
            resValue "string", "app_name", "Mindful Profile"
            signingConfig System.getenv("KEYSTORE_FILE") != null ? signingConfigs.release : signingConfigs.debug
        }
    }
    buildFeatures {
        viewBinding true
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'androidx.work:work-runtime:2.10.1'
    implementation 'androidx.appcompat:appcompat:1.7.1'
}

