plugins {
    id 'kotlin-android'
    id 'com.android.library'
    id 'com.google.devtools.ksp'
    id 'org.jetbrains.dokka'
    id 'com.vanniktech.maven.publish'
}

android {
    namespace "org.fdroid.database"
    compileSdk libs.versions.compileSdk.get().toInteger()

    defaultConfig {
        minSdkVersion 21
        targetSdk 34 // relevant for instrumentation tests (targetSdk 21 fails on Android 14)

        consumerProguardFiles "consumer-rules.pro"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments disableAnalytics: 'true'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        androidTest {
            java.srcDirs += "src/dbTest/java"
            // Adds exported schema location as test app assets.
            assets.srcDirs += files("$projectDir/schemas".toString())
        }
        test {
            java.srcDirs += "src/dbTest/java"
            // Adds exported schema location as test app assets.
            assets.srcDirs += files("$projectDir/schemas".toString())
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
    kotlinOptions {
        jvmTarget = '17'
        freeCompilerArgs += "-Xexplicit-api=strict"
        freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
    }
    aaptOptions {
        // needed only for instrumentation tests: assets.openFd()
        noCompress "json"
    }
    packagingOptions {
        exclude 'META-INF/AL2.0'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/LICENSE.md'
        exclude 'META-INF/LICENSE-notice.md'
    }
}

dependencies {
    implementation project(":libs:download")
    implementation project(":libs:index")

    implementation libs.androidx.core.ktx
    implementation libs.androidx.lifecycle.livedata.ktx

    implementation libs.androidx.room.runtime
    implementation libs.androidx.room.ktx
    ksp libs.androidx.room.compiler

    implementation libs.microutils.kotlin.logging
    implementation libs.kotlinx.serialization.json

    testImplementation project(":libs:sharedTest")
    testImplementation libs.junit
    testImplementation libs.mockk
    testImplementation libs.kotlin.test
    testImplementation libs.androidx.test.core.ktx
    testImplementation libs.androidx.test.ext.junit
    testImplementation libs.androidx.core.testing
    testImplementation libs.androidx.room.testing
    testImplementation libs.robolectric
    testImplementation libs.commons.io
    testImplementation libs.logback.classic
    testImplementation libs.kotlinx.coroutines.test
    testImplementation libs.turbine
    testImplementation libs.okhttp

    androidTestImplementation project(":libs:sharedTest")
    androidTestImplementation libs.mockk.android
    androidTestImplementation libs.kotlin.test
    androidTestImplementation libs.androidx.test.ext.junit
    androidTestImplementation libs.androidx.core.testing
    androidTestImplementation libs.androidx.espresso.core
    androidTestImplementation libs.androidx.room.testing
    androidTestImplementation libs.commons.io
}

ksp {
    arg(new RoomSchemaArgProvider(new File(projectDir, "schemas")))
}

signing {
    useGpgCmd()
}

import org.jetbrains.dokka.gradle.DokkaTask
tasks.withType(DokkaTask).configureEach {
    pluginsMapConfiguration.set(
            ["org.jetbrains.dokka.base.DokkaBase": """{
                "customAssets": ["${file("${rootProject.rootDir}/logo-icon.svg")}"],
                "footerMessage": "© 2010-2022 F-Droid Limited and Contributors"
            }"""]
    )
}

class RoomSchemaArgProvider implements CommandLineArgumentProvider {
    @InputDirectory
    @PathSensitive(PathSensitivity.RELATIVE)
    File schemaDir

    RoomSchemaArgProvider(File schemaDir) {
        this.schemaDir = schemaDir
    }

    @Override
    Iterable<String> asArguments() {
        return ["room.schemaLocation=${schemaDir.path}".toString()]
    }
}
