buildscript {
    ext.kotlin_version = project.hasProperty("kotlin_version") ? project.property("kotlin_version") : '2.4.20'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:9.4.1'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.4.21'
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
    }
}

plugins {
    id "org.jlleitschuh.gradle.ktlint" version "14.2.0" apply false
}

ext {
    junitVersion = project.hasProperty('junitVersion') ? project.property('junitVersion') : '4.13.2'
    androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? project.property('androidxAppCompatVersion') : '1.8.0'
    androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? project.property('androidxJunitVersion') : '1.3.0'
    androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? project.property('androidxEspressoCoreVersion') : '3.7.0'
}

apply plugin: 'com.android.library'
if (project.extensions.findByName('kotlin') == null) {
    apply plugin: 'kotlin-android'
}
apply plugin: 'kotlin-parcelize'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
// Generates the kotlinx.serialization serializers for the @Serializable result
// models. Required for toJSObject() to work at runtime.
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'

import groovy.json.JsonSlurper

def getPluginVersion() {
    try {
        def packageJsonFile = file('../package.json')
        if (packageJsonFile.exists()) {
            def packageJson = new JsonSlurper().parseText(packageJsonFile.text)
            if (packageJson.version) {
                return packageJson.version
            }
        }
    } catch (Exception e) {
        throw new GradleException("Failed to read plugin version from package.json", e)
    }

    throw new GradleException("Plugin version not found in ../package.json")
}

def pluginVersion = getPluginVersion()

android {
    namespace = "io.capkit.device"
    compileSdk = Math.max(36, project.hasProperty('compileSdkVersion') ? project.property('compileSdkVersion') as int : 36)

    // AGP 8.0+ disables BuildConfig by default for libraries.
    // We need to enable it to inject the plugin version.
    buildFeatures {
        buildConfig = true
    }

    defaultConfig {
        minSdkVersion Math.max(24, project.hasProperty('minSdkVersion') ? project.property('minSdkVersion') as int : 24)
        targetSdkVersion Math.max(36, project.hasProperty('targetSdkVersion') ? project.property('targetSdkVersion') as int : 36)
        versionCode 1

        // Dynamic versioning (feature enabled)
        versionName = pluginVersion

        // Injects the version into the BuildConfig class ONLY if feature is enabled
        buildConfigField "String", "PLUGIN_VERSION", "\"${pluginVersion}\""

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled = false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    lint {
        abortOnError = false
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_21
        targetCompatibility = JavaVersion.VERSION_21
    }
}

repositories {
    google()
    mavenCentral()
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.12.0"

    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
}
