import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies

class AndroidRoomConventionPlugin : Plugin<Project> {
    override fun apply(target: Project): Unit = with(target) {
        with(pluginManager) {
            apply("com.google.devtools.ksp")
        }

        extensions.configure<LibraryExtension> {
            defaultConfig {
                javaCompileOptions {
                    annotationProcessorOptions {
                        arguments["room.schemaLocation"] = "$projectDir/schemas"
                        arguments["room.incremental"] = "true"
                    }
                }
            }
            sourceSets {
                getByName("androidTest").assets.srcDirs("$projectDir/schemas")
            }
        }

        val libs = libs
        dependencies {
            add("implementation", libs.findLibrary("androidx-room-runtime").get())
            add("implementation", libs.findLibrary("androidx-room-ktx").get())
            add("ksp", libs.findLibrary("androidx-room-compiler").get())
            add("androidTestImplementation", libs.findLibrary("androidx-room-testing").get())
        }
    }
}
