import nu.studer.gradle.jooq.JooqEdition

/* Composite build for jooq object generator (JooqGenerater.java) */

plugins {
    id 'nu.studer.jooq' version '8.2'
    id 'java'
}

repositories {
    mavenCentral()
//    mavenLocal()
    maven {
        url = 'https://plugins.gradle.org/m2/'
    }
}

group = 'dmo.fs.utils'
version = '0.0.2'

ext {
  vertxVersion = '4.5.26'
}

def jooqVersion = '3.21.6'

jooq {
  version = '3.19.18'
  edition = JooqEdition.OSS
}

tasks.register('copyDatabaseConfig', Copy) {
    from('src/main/resources/database_config.json')
    into new File('build', 'resources/main')
    doLast {
        compileJava
    }
}
/*
  Using a java class to generate jooq objects - allows for multiple db's -
  see ./src/main/kotlin/golf/handicap/generated/ directory
*/
tasks.register('jooqGenerate', JavaExec) {
    doFirst {
        defaultTasks 'clean'
        classpath = sourceSets.main.runtimeClasspath
    }
//    environment.remove('DEFAULT_DB')
    environment 'VERTXWEB_ENVIRONMENT', 'prod'
    mainClass = 'dmo.fs.db.generate.utils.JooqGenerate'
    args = ['prod']
    setIgnoreExitValue(true)
    dependsOn copyDatabaseConfig
}

java {
    sourceCompatibility = JavaVersion.VERSION_25
    targetCompatibility = JavaVersion.VERSION_25
}

dependencies {
    implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion"))
    implementation 'io.vertx:vertx-core:${vertxVersion}'
    implementation 'io.vertx:vertx-rx-java3:${vertxVersion}'
    implementation 'io.vertx:vertx-web-api-contract:${vertxVersion}'
    implementation 'io.vertx:vertx-jdbc-client:${vertxVersion}'
    implementation 'io.agroal:agroal-pool:2.5'
    implementation 'org.xerial:sqlite-jdbc:3.47.1.0'
    implementation 'io.vertx:vertx-mysql-client:${vertxVersion}'
    implementation 'io.vertx:vertx-pg-client:${vertxVersion}'
    implementation "org.jooq:jooq-postgres-extensions:${jooqVersion}"
    implementation 'org.jooq:jooq:${jooqVersion}'
    implementation 'org.jooq:jooq-codegen-maven:${jooqVersion}'
    implementation 'org.jooq:jooq-meta:${jooqVersion}'
    implementation 'com.h2database:h2:2.3.232'
    implementation 'org.mariadb.jdbc:mariadb-java-client:3.5.2'
}

tasks.withType(JavaExec).configureEach {
    jvmArgs += ['--enable-native-access=ALL-UNNAMED', '--add-opens', 'java.base/java.lang=ALL-UNNAMED']

    if (JavaVersion.current() >= JavaVersion.VERSION_24) {
        jvmArgs += ['--sun-misc-unsafe-memory-access=allow']
    }
}

