import java.util.Properties
import java.io.FileInputStream
import java.io.FileOutputStream

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")
}

// Load key.properties
val keyPropertiesFile = rootProject.file("app/../publish/key.properties")
val keyProperties = Properties()
if (keyPropertiesFile.exists()) {
    keyProperties.load(FileInputStream(keyPropertiesFile))
}

// ============================================
// Auto-increment version code on each build
// ============================================
val versionPropertiesFile = rootProject.file("app/version.properties")
val versionProperties = Properties()

fun calculateNextVersionCode(): Int {
    if (versionPropertiesFile.exists()) {
        versionProperties.load(FileInputStream(versionPropertiesFile))
    }
    val currentCode = versionProperties.getProperty("VERSION_CODE", "0").toInt()
    val newCode = currentCode + 1
    versionProperties.setProperty("VERSION_CODE", newCode.toString())
    versionProperties.store(FileOutputStream(versionPropertiesFile), "Auto-generated version code - DO NOT EDIT MANUALLY")
    return newCode
}

// Get auto-incremented version code
val autoVersionCode = calculateNextVersionCode()

// Version name format: major.minor.patch (from pubspec.yaml) + build number
// Example: 1.0.0.123
fun getAutoVersionName(): String {
    val flutterVersionName = flutter.versionName
    return "$flutterVersionName.$autoVersionCode"
}

dependencies {
    // SplashScreen API for Android 12+ compatibility
    implementation("androidx.core:core-splashscreen:1.0.1")
}

android {
    namespace = "com.durianqueen.app"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

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

    kotlin {
        jvmToolchain(17)
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.durianqueen.app"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        // Auto-incremented version code and name
        versionCode = autoVersionCode
        versionName = getAutoVersionName()
    }

    signingConfigs {
        create("release") {
            if (keyPropertiesFile.exists()) {
                keyAlias = keyProperties["keyAlias"] as String
                keyPassword = keyProperties["keyPassword"] as String
                storeFile = file(keyProperties["storeFile"] as String)
                storePassword = keyProperties["storePassword"] as String
            }
        }
    }

    buildTypes {
        release {
            signingConfig = if (keyPropertiesFile.exists()) {
                signingConfigs.getByName("release")
            } else {
                signingConfigs.getByName("debug")
            }
            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
}

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