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')
def flutterVersionName = localProperties.getProperty('flutter.versionName')

if (flutterVersionName == null || flutterVersionCode == null) {
    def pubspecFile = rootProject.file('../pubspec.yaml')
    if (pubspecFile.exists()) {
        def pubspec = pubspecFile.text
        def versionMatch = pubspec =~ /version:\s*['"]?([^\s'"]+)['"]?/
        if (versionMatch) {
            def version = versionMatch[0][1].trim()
            if (flutterVersionName == null) {
                flutterVersionName = version.contains('+') ? version.split('+')[0].trim() : version
            }
            if (flutterVersionCode == null) {
                flutterVersionCode = version.contains('+') ? version.split('+')[1].trim() : '1'
            }
        }
    }
}

if (flutterVersionCode == null) flutterVersionCode = '1'
if (flutterVersionName == null) flutterVersionName = '1.0'

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
def hasKeystore = false
if (keystorePropertiesFile.exists()) {
    try {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
        hasKeystore = keystoreProperties['storeFile'] != null &&
                     keystoreProperties['keyAlias'] != null &&
                     keystoreProperties['keyPassword'] != null &&
                     keystoreProperties['storePassword'] != null &&
                     file(keystoreProperties['storeFile']).exists()
    } catch (Exception e) {
        println("Warning: Failed to load key.properties: ${e.message}")
    }
}

android {
    namespace "am.sure.mobile"
    compileSdk = 36
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    if (hasKeystore) {
        signingConfigs {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile file(keystoreProperties['storeFile'])
                storePassword keystoreProperties['storePassword']
            }
        }
    }

    defaultConfig {
        applicationId "am.sure.mobile"
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            if (hasKeystore) {
                signingConfig signingConfigs.release
            }
            minifyEnabled false
            shrinkResources false
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
