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

android {
    namespace = "com.sicherhaven.eventify"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
    }

    defaultConfig {
        applicationId = "com.sicherhaven.eventify"
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = 17
        versionName = "1.6.1(p)"
    }

    // ---------- SIGNING CONFIG ----------
    signingConfigs {
        create("release") {
            // path relative to android/app/
            storeFile = file("upload-keystore-new.jks")

            // Read from gradle.properties or environment variables.
            @Suppress("UNCHECKED_CAST")
            val storePwd = (project.findProperty("KEYSTORE_PASSWORD") ?: System.getenv("KEYSTORE_PASSWORD")) as String?
            val alias = (project.findProperty("KEY_ALIAS") ?: System.getenv("KEY_ALIAS")) as String?
            val keyPwd = (project.findProperty("KEY_PASSWORD") ?: System.getenv("KEY_PASSWORD")) as String?

            // If any are null/empty the build will fail; keep empty strings to avoid null issues,
            // but ensure you provided values in gradle.properties or env.
            storePassword = storePwd ?: ""
            keyAlias = alias ?: ""
            keyPassword = keyPwd ?: ""
        }
    }

    buildTypes {
        release {
            // Use the release signing config created above
            signingConfig = signingConfigs.getByName("release")

            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

flutter {
    source = "../.."
}
