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

group = 'expo.modules.persistentbackgroundlocation'
version = '0.1.0'

def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
if (expoModulesCorePlugin.exists()) {
  apply from: expoModulesCorePlugin
  applyKotlinExpoModulesCorePlugin()
}

// SDK 56 default targets — every value is overridable from the consumer app's
// root `build.gradle` (via `ext`) so older / newer toolchains keep compiling.
def safeExtGet(prop, fallback) {
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

android {
  namespace "expo.modules.persistentbackgroundlocation"

  compileSdkVersion safeExtGet("compileSdkVersion", 36)

  def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
  if (agpVersion.tokenize('.')[0].toInteger() < 8) {
    compileOptions {
      sourceCompatibility JavaVersion.VERSION_17
      targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
      jvmTarget = JavaVersion.VERSION_17.majorVersion
    }
  }

  defaultConfig {
    // Foreground-service-type rules (Android 14) require targetSdk >= 34; the
    // consumer app's targetSdk wins via `ext`.
    minSdkVersion safeExtGet("minSdkVersion", 24)
    targetSdkVersion safeExtGet("targetSdkVersion", 36)
  }

  lintOptions {
    abortOnError false
  }

  publishing {
    singleVariant("release") {
      withSourcesJar()
    }
  }
}

dependencies {
  implementation project(':expo-modules-core')

  // Kotlin stdlib — version controlled by the consumer app via Expo.
  implementation "org.jetbrains.kotlin:kotlin-stdlib:${safeExtGet('kotlinVersion', '2.0.21')}"

  // Coroutines for off-main-thread persistence / sync.
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0"
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0"

  // FusedLocationProviderClient + ActivityRecognition (battery-optimal path).
  // Gracefully falls back to AOSP LocationManager when Play Services is absent.
  implementation "com.google.android.gms:play-services-location:${safeExtGet('playServicesLocationVersion', '21.3.0')}"

  // Notification compat + ServiceCompat.startForeground(type, ...).
  implementation "androidx.core:core-ktx:${safeExtGet('androidxCoreVersion', '1.13.1')}"
}
