def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY');
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
    apply plugin: 'com.android.application'
}
else {
    apply plugin: 'com.android.library'
}

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

android {
    namespace("com.ghostship.android")
    buildFeatures {
        buildConfig true
    }
    
    // Signing configuration
    signingConfigs {
        release {
            if (keystorePropertiesFile.exists()) {
                storeFile file(keystoreProperties['GHOSTSHIP_STORE_FILE'])
                storePassword keystoreProperties['GHOSTSHIP_STORE_PASSWORD']
                keyAlias keystoreProperties['GHOSTSHIP_KEY_ALIAS']
                keyPassword keystoreProperties['GHOSTSHIP_KEY_PASSWORD']
            }
        }
    }
    ndkVersion "29.0.14206865"
    compileSdkVersion 36
    defaultConfig {
        if (buildAsApplication) {
            applicationId "com.ghostship.android"
        }
        minSdkVersion 24
        targetSdkVersion 36
        versionCode 1
        versionName "1.0.1"
        externalNativeBuild {
            cmake {
                 arguments "-DSDL_SHARED=ON", "-DANDROID_STL=c++_static", "-DHAVE_LD_VERSION_SCRIPT=OFF",'-DUSE_OPENGLES=ON'
                 abiFilters 'arm64-v8a', 'armeabi-v7a'
            }
        }
        
        // Optimize APK size
        vectorDrawables {
            useSupportLibrary true
        }
    }

    // ✅ Universal APK: disables split APKs per ABI
    splits {
        abi {
            enable false
       }
    }

    buildTypes {
        debug {
            debuggable true
            jniDebuggable true
            minifyEnabled false
            shrinkResources false
        }
        release {
            debuggable false
            jniDebuggable false
            // Disable minification temporarily to isolate the issue
            minifyEnabled false
            shrinkResources false
            
            // Keep debug symbols for crash analysis
            ndk {
                debugSymbolLevel 'SYMBOL_TABLE'
            }
            
            // Sign the release build
            if (keystorePropertiesFile.exists()) {
                signingConfig signingConfigs.release
            }
        }
    }
    applicationVariants.all { variant ->
        tasks["merge${variant.name.capitalize()}Assets"]
            .dependsOn("externalNativeBuild${variant.name.capitalize()}")
    }
    if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
        sourceSets.main {
            jniLibs.srcDir 'libs'
        }
        externalNativeBuild {
            cmake {
                 path '../../CMakeLists.txt'
                 version "3.31.5"
            }
        }
    }
    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }
    
    // Additional optimizations
    packagingOptions {
        // Exclude unnecessary files to reduce APK size
        excludes += [
            'META-INF/DEPENDENCIES',
            'META-INF/LICENSE',
            'META-INF/LICENSE.txt',
            'META-INF/NOTICE',
            'META-INF/NOTICE.txt',
            'META-INF/*.kotlin_module'
        ]
        // Pick first occurrence of duplicate files
        pickFirsts += ['**/libc++_shared.so', '**/libjsc.so']
    }
    

    
    if (buildAsLibrary) {
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith(".aar")) {
                    def fileName = "org.libsdl.app.aar";
                    output.outputFile = new File(outputFile.parent, fileName);
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.core:core:1.7.0' // Use the latest version
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.documentfile:documentfile:1.0.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
}

task wrapper(type: Wrapper) {
    gradleVersion = '8.9'
}

task prepareKotlinBuildScriptModel {
}




