plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
    id("com.google.gms.google-services")
}

// Load keystore properties from key.properties file
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    // Define the namespace for your Android application
    namespace = "com.marco.aiot"
    // Set the compile SDK version based on Flutter's configuration
    compileSdk = flutter.compileSdkVersion
    // Set the NDK version based on Flutter's configuration
    ndkVersion = flutter.ndkVersion

    // Configure Java compatibility options
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
        // ✅ Enable core library desugaring for Java 8+ APIs
        coreLibraryDesugaringEnabled true
    }

    // Configure Kotlin options for JVM target
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    // Default configuration for your application
    defaultConfig {
        // Specify your unique Application ID. This identifies your app on Google Play.
        applicationId = "com.marco.aiot"
        // Set minimum and target SDK versions based on Flutter's configuration
        minSdk = 23
        targetSdk = flutter.targetSdkVersion
        // Set version code and name based on Flutter's configuration (from pubspec.yaml)
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    // Define signing configurations for different build types
    signingConfigs {
        release {
            // Reference the key alias from key.properties
            keyAlias keystoreProperties['keyAlias']
            // Reference the key password from key.properties
            keyPassword keystoreProperties['keyPassword']
            // Reference the keystore file path from key.properties
            storeFile file(keystoreProperties['storeFile'])
            // Reference the keystore password from key.properties
            storePassword keystoreProperties['storePassword']
        }
    }

    // Define different build types (e.g., debug, release)
    buildTypes {
        release {
            // Apply the 'release' signing configuration defined above to the release build
            signingConfig signingConfigs.release
            // Enable code minification to reduce app size
            minifyEnabled true
            // Enable resource shrinking to remove unused resources
            shrinkResources true
            // Other release specific configurations can be added here, e.g., ProGuard rules
        }
    }
}

// Configure Flutter specific settings, pointing to the root of your Flutter project
flutter {
    source = "../.."
}

// ✅ Add required dependencies for desugaring
dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.1.4'
}

