import org.gradle.internal.jvm.Jvm

buildscript {
  // The Android Gradle plugin is only required when opening the android folder stand-alone.
  // This avoids unnecessary downloads and potential conflicts when the library is included as a
  // module dependency in an application project.
  if (project == rootProject) {
    repositories {
      google()
      mavenCentral()
    }

    dependencies {
      classpath("com.android.tools.build:gradle:8.2.2")
      classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.24")
    }
  }
}

plugins {
  id "com.android.library"
  id "com.facebook.react"
  id "org.jetbrains.kotlin.android"
}

ext {
  jvmVersion = Jvm.current().javaVersion.majorVersion
  jvmMajorVersion = jvmVersion.toInteger()
  if (jvmMajorVersion < 17) {
    println "\n\n\n"
    println "**************************************************************************************************************"
    println "\n\n\n"
    println "ERROR: Notifee requires JDK 17 or newer to build on Android."
    println "  Obsolete Java versions such as 8 and 11 are not supported."
    println "  Incompatible major version detected: '" + jvmVersion + "'"
    println "\n\n\n"
    println "**************************************************************************************************************"
    println "\n\n\n"
    throw new GradleException("Notifee requires JDK 17 or newer to build on Android. Detected Java major version '" + jvmVersion + "'.")
  }
  if (jvmMajorVersion != 17 && jvmMajorVersion != 21) {
    println "\n\n\n"
    println "**************************************************************************************************************"
    println "\n\n\n"
    println "WARNING: Notifee Android builds are validated with JDK 17 and JDK 21."
    println "  Detected Java major version: '" + jvmVersion + "'."
    println "  The build will continue, but compatibility depends on the consumer Gradle, AGP, Kotlin, and React Native toolchain."
    println "  Use JDK 17 or JDK 21 for a stable build baseline."
    println "\n\n\n"
    println "**************************************************************************************************************"
    println "\n\n\n"
  }
}

if (project.hasProperty('reactNativeProjects')) {
  reactNativeProjects.each { dependent ->
    project.evaluationDependsOn(dependent)
  }
} else {
  project.evaluationDependsOn(':app')
}

android {
  namespace = "io.invertase.notifee"
  compileSdk = 35

  defaultConfig {
    minSdk = 24
    targetSdk = 35
    multiDexEnabled = true

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    javaCompileOptions {
      annotationProcessorOptions {
        arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
      }
    }
  }

  lint {
    disable 'GradleCompatible'
    abortOnError = false
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
  }

  sourceSets {
    main {
      kotlin.srcDirs += 'src/main/kotlin'
      java.srcDirs += 'src/main/java'
    }
    androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
  }

  testOptions {
    unitTests.returnDefaultValues = true
  }

  testOptions.unitTests.all {
    testLogging {
      events "failed", "skipped"
      showStackTraces = true
      exceptionFormat = "full"
    }

    maxParallelForks = (System.getenv("CI") == "true") ? 1 : Runtime.runtime.availableProcessors().intdiv(2) ?: 1
    forkEvery = 40
    maxHeapSize = "2048m"
    minHeapSize = "1024m"
    systemProperties['junit.jupiter.execution.parallel.enabled'] = true
    systemProperties['junit.jupiter.execution.parallel.mode.default'] = "concurrent"
  }

  buildTypes {
    release {
      consumerProguardFiles 'proguard-rules.pro'
    }
  }
}

repositories {
  google()
  mavenCentral()
}

dependencies {
  // --- Core dependencies (formerly in android/build.gradle) ---
  api 'androidx.annotation:annotation:1.3.0' // https://developer.android.com/jetpack/androidx/releases/annotation
  api 'androidx.concurrent:concurrent-futures:1.1.0' // https://developer.android.com/jetpack/androidx/releases/concurrent
  api 'androidx.work:work-runtime:2.11.1' // https://developer.android.com/jetpack/androidx/releases/work
  implementation 'com.facebook.fresco:fresco:2.6.0' // https://github.com/facebook/fresco/releases
  implementation("com.google.guava:guava:33.5.0-android") // https://github.com/google/guava
  compileOnly 'org.checkerframework:checker-qual:3.43.0' // required by Guava's nullness annotations
  implementation 'androidx.core:core:1.6.0'

  // NotifeeCoreDatabase_Impl is generated against Room 2.8.4's RoomOpenDelegate API
  // (stable since Room 2.6.0). If bumping this version, verify the generated _Impl class
  // is still binary-compatible by running yarn smoke:android. A future Room 3.0 with
  // breaking changes will require regenerating the core database sources.
  def room_version = '2.8.4' // https://developer.android.com/jetpack/androidx/releases/room
  api "androidx.room:room-runtime:$room_version"
  api 'androidx.sqlite:sqlite:2.6.2' // https://developer.android.com/jetpack/androidx/releases/sqlite
  api 'androidx.sqlite:sqlite-framework:2.6.2'
  annotationProcessor "androidx.room:room-compiler:$room_version"

  api 'org.greenrobot:eventbus:3.3.1' // https://github.com/greenrobot/EventBus/releases

  // ProfileInstaller bootstraps baseline profiles on devices that do not receive
  // cloud-compiled profiles from Play Store (sideloads, debug builds).
  api 'androidx.profileinstaller:profileinstaller:1.4.1'

  // --- Bridge dependencies ---
  implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
  //noinspection GradleDynamicVersion
  implementation 'com.facebook.react:react-android:+'

  // --- Test dependencies ---
  testImplementation 'junit:junit:4.13.2' // https://github.com/junit-team/junit4/releases
  testImplementation 'org.robolectric:robolectric:4.14.1' // https://github.com/robolectric/robolectric/releases
  testImplementation 'org.mockito:mockito-core:5.19.0' // https://github.com/mockito/mockito/releases
  androidTestImplementation 'androidx.test.ext:junit:1.1.3' // https://developer.android.com/jetpack/androidx/releases/test
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // see above
  androidTestImplementation "androidx.room:room-testing:$room_version"
  androidTestImplementation "androidx.test:monitor:1.4.0"
}
