plugins {
    id 'java'
    id 'kotlin'
    id 'kotlin-spring'
    id 'nu.studer.jooq' version '4.1'
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id "org.flywaydb.flyway" version "6.2.3"
    id "org.jetbrains.kotlin.jvm"
    id "org.jetbrains.kotlin.plugin.spring"
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.boot.experimental:spring-boot-bom-r2dbc:0.1.0.M3")
    }
}

dependencies {
    implementation project(':voter-application-core')
    implementation 'org.flywaydb:flyway-core:6.2.4'
    implementation 'io.r2dbc:r2dbc-h2'
    implementation 'org.jooq:jooq'
    implementation 'org.jooq:jooq-meta'
    implementation 'org.jooq:jooq-codegen'
    implementation("org.springframework.boot.experimental:spring-boot-starter-data-r2dbc")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
    implementation ('org.springframework.boot:spring-boot-starter-jooq') {
        exclude group: "jakarta.xml.bind", module: "jakarta.xml.bind-api"
    }
    implementation ('org.springframework.boot:spring-boot-starter-data-jpa:2.2.3.RELEASE') {
        exclude group: "jakarta.xml.bind", module: "jakarta.xml.bind-api"
    }
    jooqRuntime 'com.h2database:h2:1.4.200'
    testImplementation 'org.flywaydb.flyway-test-extensions:flyway-spring-test:5.2.1'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation("org.springframework.boot.experimental:spring-boot-test-autoconfigure-r2dbc")
    testImplementation("io.projectreactor:reactor-test")
}

ext {
    h2SchemaDbName = "VOTER"
    h2SchemaUser = "username"
    h2SchemaPassword = "password"
    h2Schema = "VOTER"
}

jooq {
    version = '3.12.3'
    edition = 'OSS'
    voter(sourceSets.main) {
        jdbc {
            driver = 'org.h2.Driver'
            url = "jdbc:h2:~/${h2SchemaDbName}"
            user = h2SchemaUser
            password = h2SchemaPassword
        }
        generator {
            database {
                name = 'org.jooq.meta.h2.H2Database'
                inputSchema = h2Schema
                includes = '.*'
                excludes = ''
                outputSchema = ''
                outputSchemaToDefault = false
            }
            // Properties documentation: https://www.jooq.org/doc/3.11/manual/code-generation/codegen-advanced/
            generate {
                daos = true
                relations = true
                deprecated = true
                records = true
                immutablePojos = true
                fluentSetters = true
            }
            target {
                packageName = 'com.hexarchbootdemo.adapter.output.persistence.h2.generated-sources.jooq'
                directory = 'src/main/java'
            }
        }
    }
}

flyway {
    url = "jdbc:h2:~/${h2SchemaDbName}"
    user = h2SchemaUser
    password = h2SchemaPassword
    schemas = [h2Schema]
    locations = ["filesystem:$project.projectDir/src/main/resources/db/migration"]
}
jar {
    enabled = true
}

compileKotlin.dependsOn generateVoterJooqSchemaSource
generateVoterJooqSchemaSource.dependsOn flywayMigrate
test.dependsOn flywayClean
