buildscript {
    ext.kotlin_version = '1.7.10'

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.3.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)
def packageVersion = packageJson.version

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 34)
    namespace 'com.bigcrunch.ads.react'

    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 21)
        targetSdkVersion safeExtGet('targetSdkVersion', 34)
        versionCode 1
        versionName packageVersion
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = '11'
        freeCompilerArgs += ['-Xskip-metadata-version-check']
    }

    sourceSets {
        main {
            java.srcDirs += 'bigcrunch-ads'
            // When installed from npm, native SDK sources are bundled in bigcrunch-ads/
            // (copied by prepack). In local dev, that directory is empty — include the
            // monorepo's native SDK sources directly via canonical path (needed because
            // node_modules symlink breaks relative path resolution).
            def bundledDir = file('bigcrunch-ads')
            if (fileTree(dir: bundledDir, includes: ['**/*.kt']).empty) {
                def nativeSrc = new File(projectDir.canonicalFile, '../../android/bigcrunch-ads/src/main/java')
                if (nativeSrc.exists()) {
                    java.srcDirs += nativeSrc.canonicalPath
                }
            }
        }
    }
}

repositories {
    google()
    mavenCentral()
    maven { url "https://jitpack.io" }
}

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

    // React Native dependency
    implementation 'com.facebook.react:react-native:+'

    // Google Mobile Ads SDK (Ad Manager)
    implementation 'com.google.android.gms:play-services-ads:22.6.0'

    // Networking
    implementation 'com.squareup.okhttp3:okhttp:4.11.0'

    // JSON parsing (Moshi)
    implementation 'com.squareup.moshi:moshi:1.14.0'
    implementation 'com.squareup.moshi:moshi-kotlin:1.14.0'
    kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.14.0'

    // Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
}