import groovy.json.JsonSlurper

plugins {
  id 'com.android.library'
  id 'org.jetbrains.kotlin.android'
  id 'org.jetbrains.kotlin.plugin.compose' version '2.1.20'
}

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

group = 'com.clerk.expo'
version = '1.0.0'

// Set by the @clerk/expo-native-components config plugin to the installed @clerk/expo version.
def clerkExpoVersion = rootProject.findProperty("clerkExpo.hostSdkVersion")?.toString() ?:
  new JsonSlurper().parse(new File(projectDir, "../package.json")).version.toString()

// Dependency versions - centralized for easier updates
// See: https://docs.gradle.org/current/userguide/version_catalogs.html for app-level version catalogs
ext {
  kotlinxCoroutinesVersion = "1.7.3"
  clerkAndroidApiVersion = "1.5.0"
  clerkAndroidUiVersion = "1.5.0"
  okhttpVersion = "5.4.0"
  okhttpUrlConnectionVersion = "5.0.0-alpha.16"
  composeVersion = "1.7.0"
  activityComposeVersion = "1.9.0"
  lifecycleVersion = "2.8.0"
  savedStateVersion = "1.4.0"
}

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

android {
  namespace "expo.modules.clerk"

  compileSdk safeExtGet("compileSdkVersion", 36)

  defaultConfig {
    minSdk safeExtGet("minSdkVersion", 24)
    targetSdk safeExtGet("targetSdkVersion", 36)
    versionCode 1
    versionName "1.0.0"
    buildConfigField "String", "CLERK_EXPO_VERSION", "\"${clerkExpoVersion}\""
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  kotlinOptions {
    jvmTarget = "17"
    // TODO: Remove once clerk-android pins its transitive deps to a Kotlin version compatible with Expo.
    // clerk-android transitive deps (e.g. kotlinx-serialization-core:1.10.0) ship Kotlin 2.3.x metadata,
    // but Expo SDK 54/55 uses Kotlin 2.1.x. This flag lets the 2.1.x compiler read 2.3.x metadata safely.
    freeCompilerArgs += ['-Xskip-metadata-version-check']
  }

  buildFeatures {
    buildConfig = true
    compose = true
  }

  packaging {
    resources {
      excludes += ['META-INF/versions/9/OSGI-INF/MANIFEST.MF']
    }
  }

  sourceSets {
    main {
      java.srcDirs = ['src/main/java']
    }
  }
}

// clerk-android is compiled with Kotlin 2.3.x, but Expo/RN projects typically use an older
// Kotlin version (e.g. 2.1.x). This causes "incompatible version of Kotlin" metadata errors
// at :app:compileDebugKotlin. Applying -Xskip-metadata-version-check allows the older
// compiler to read 2.3.x metadata without failing.
try {
  rootProject.allprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
      compilerOptions {
        freeCompilerArgs.add('-Xskip-metadata-version-check')
      }
    }
  }
} catch (Exception ignored) {
  // Future Gradle versions with Isolated Projects may block cross-project configuration.
  // In that case, users should add '@clerk/expo-native-components' to their app.json plugins array instead,
  // which applies the flag via the config plugin's withClerkAndroid.
}

dependencies {
  testImplementation "junit:junit:4.13.2"

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

  // Coroutines for async operations
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinxCoroutinesVersion"

  // Clerk Android SDK with prebuilt UI
  // Exclude kotlin-stdlib to prevent 2.3.0 from polluting the project
  // Exclude transitive OkHttp modules so the compatible versions below control resolution.
  implementation("com.clerk:clerk-android-ui:$clerkAndroidUiVersion") {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk7'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
    exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    exclude group: 'com.squareup.okhttp3', module: 'okhttp-urlconnection'
  }
  // clerk-android-telemetry has a transitive dep on the last released
  // clerk-android-api. Pinning the api explicitly here keeps consumers
  // compiling against the same version we ship the UI from.
  implementation("com.clerk:clerk-android-api:$clerkAndroidApiVersion") {
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk7'
    exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
    exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    exclude group: 'com.squareup.okhttp3', module: 'okhttp-urlconnection'
  }

  implementation('com.squareup.okhttp3:okhttp') {
    version { strictly(okhttpVersion) }
  }
  implementation('com.squareup.okhttp3:logging-interceptor') {
    version { strictly(okhttpVersion) }
  }
  implementation('com.squareup.okhttp3:okhttp-urlconnection') {
    version { strictly(okhttpUrlConnectionVersion) }
  }

  // Jetpack Compose for wrapping Clerk views
  implementation "androidx.compose.ui:ui:$composeVersion"
  implementation "androidx.compose.material3:material3:1.3.0"
  implementation "androidx.activity:activity-compose:$activityComposeVersion"
  implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycleVersion"
  implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycleVersion"
  implementation "androidx.savedstate:savedstate-compose:$savedStateVersion"

  testImplementation "junit:junit:4.13.2"
}
