
plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    id 'org.jetbrains.kotlin.plugin.serialization'
    id 'com.android.library'
    id "com.google.devtools.ksp" version "$version_kotlin-$version_ksp"
}


group rootProject.group
version rootProject.version

sourceCompatibility = '17'
targetCompatibility = '17'

group rootProject.group
version rootProject.version

android {
    compileSdk rootProject.ext.version_android_compile_sdk
    sourceSets.main.manifest.srcFile('src/androidMain/AndroidManifest.xml')
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion rootProject.ext.version_android_target_sdk
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
        debug {
            java.srcDirs += ["build/generated/ksp/android/androidDebug/kotlin"]
        }
        release {
            java.srcDirs += ["build/generated/ksp/android/androidRelease/kotlin"]
        }
    }

    compileOptions {
        sourceCompatibility = '17'
        targetCompatibility = '17'
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
        kotlinOptions {
            jvmTarget = "17"
        }
    }

    buildTypes {
        release {
            consumerProguardFiles 'proguard-rules.pro'
        }
    }
    namespace 'com.ustadmobile.core.db'
}


kotlin {
    androidTarget {
        publishLibraryVariants("release", "debug")
    }

    jvm {
        compilations.main.kotlinOptions {
            // Setup the Kotlin compiler options for the 'main' compilation:
            jvmTarget = "$version_kotlin_jvmTarget"
        }

        compilations.test.kotlinOptions {
            // Setup the Kotlin compiler options for the 'main' compilation:
            jvmTarget = "$version_kotlin_jvmTarget"
        }
    }

    js(IR) {
        browser {
            testTask {
                useKarma {
                    useChromeHeadless() //change to useChrome to run the actual browser
                }
            }
        }
    }

    sourceSets {
        commonMain {
            kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")

            dependencies {
                implementation kotlin('stdlib-common')
                implementation libs.kotlinx.serialization
                implementation libs.coroutines
                implementation project(":lib-util")
                implementation libs.ktor.client.core

                api libs.door.runtime
                implementation libs.napier
                implementation libs.kodein.di
                compileOnly libs.door.room.annotations
 
            }
        }

        commonJvmMain {
            dependsOn commonMain
        }


        androidMain {
            dependsOn commonJvmMain

            dependencies {
                api libs.androidx.room.runtime
                api libs.androidx.room.paging
                api libs.androidx.room.ktx
                api libs.androidx.lifecycle.livedata.ktx
                api libs.androidx.paging.runtime

                implementation libs.nanohttpd
                implementation libs.nanohttpd.nanolets
            }
        }

        androidTest {
            dependencies {
                implementation libs.androidx.room.testing
                implementation libs.androidx.test.runner
                implementation libs.junit
            }
        }

        jvmMain {
            dependsOn commonJvmMain
            kotlin.srcDir("build/generated/ksp/jvm/jvmMain/kotlin")
            dependencies {
                implementation libs.ktor.server.core
                implementation libs.kodein.di.framework.ktor.server.jvm
            }
        }

        jsMain {
            kotlin.srcDir("build/generated/ksp/js/jsMain/kotlin")
        }

    }

}


// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
    compileClasspath
}

ksp {
    arg("doordb_postgres_url", rootProject.ext.buildConfigProperties.getProperty("door.postgresUrl", ""))
    arg("doordb_postgres_user", rootProject.ext.buildConfigProperties.getProperty("door.postgresUser", ""))
    arg("doordb_postgres_password", rootProject.ext.buildConfigProperties.getProperty("door.postgresPass", ""))
    arg("room.schemaLocation", "$projectDir/schemas")
}

dependencies {
    kspJvm libs.door.compiler
    kspJvm libs.door.shallowcopy
    kspJs libs.door.compiler
    kspJs libs.door.shallowcopy
    kspAndroid libs.door.compiler
    kspAndroid libs.androidx.room.compiler
    kspAndroid libs.door.shallowcopy
}
