// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "35.0.0"
        minSdkVersion = 24
        compileSdkVersion = 36
        targetSdkVersion = 36
        // Updated NDK to support 16k page size devices
        ndkVersion = "27.0.12077973"
        kotlinVersion = "2.0.0"
        firebaseMessagingVersion = "23.4.0"
        firebaseBomVersion = "32.7.3"
    }
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath('com.android.tools.build:gradle:8.11.2')
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath 'com.google.gms:google-services:4.4.0'
        // Removed firebase-crashlytics (no usages)—add back 3.x if still applied in modules.
        // Removed rust-android-gradle plugin; keep only if you re-enable Rust integration.
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven {
            url("$rootDir/../../node_modules/react-native/android")
        }
        maven {
            url("$rootDir/../../node_modules/jsc-android/dist")
        }
        maven { url 'https://jitpack.io' }
    }
    configurations.configureEach {
        resolutionStrategy.dependencySubstitution {
            substitute(platform(module('com.gemalto.jp2:jp2-android'))) using module('com.github.Tgo1014:JP2ForAndroid:1.0.4')
            substitute module('io.fotoapparat:fotoapparat') using module('com.github.fotoapparat:fotoapparat:2.7.0')
        }
        resolutionStrategy.force 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
    }
    configurations.all {
        resolutionStrategy {
            force 'com.google.firebase:firebase-iid:21.1.0'
        }
    }
}

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                def manifestFile = project.file('src/main/AndroidManifest.xml')
                def buildGradleFile = project.file('build.gradle')

                if (buildGradleFile.exists()) {
                    def buildGradleText = buildGradleFile.text
                    def namespaceMatcher = buildGradleText =~ /namespace\s*['"](.+?)['"]/
                    if (namespaceMatcher.find()) {
                        namespace = namespaceMatcher[0][1]
                        println "Set namespace for ${project.name} to ${namespace} from build.gradle"
                        return
                    }
                }

                if (manifestFile.exists()) {
                    def manifestText = manifestFile.text
                    def packageMatcher = manifestText =~ /package="([\w\.]+)"/
                    if (packageMatcher.find()) {
                        def packageName = packageMatcher[0][1]
                        namespace = packageName
                        println "Set namespace for ${project.name} to ${namespace} from AndroidManifest.xml"
                    } else {
                        namespace = project.group
                        println "No 'package' attribute found. Set namespace for ${project.name} to ${namespace}"
                    }
                } else {
                    namespace = project.group
                    println "AndroidManifest.xml not found. Set namespace for ${project.name} to ${namespace}"
                }
            }
        }
    }
}

apply plugin: "com.facebook.react.rootproject"
