buildscript {
    dependencies {
        classpath libs.h2
        classpath libs.flyway.core
    }
}

plugins {
    id 'java-library'
    id 'checkstyle'
    alias libs.plugins.spring.boot2.plugin
    alias libs.plugins.flyway.plugin
    alias libs.plugins.jooq.codegen.gradle
    alias libs.plugins.yumi.gradle.licenser.plugin
    alias libs.plugins.jaredsburrows.license.plugin
}

// ----------------------------------------------------------

def dbVersion = libs.versions.h2.version.get()
def dbDriver = 'org.h2.Driver'
def dbUrl = "jdbc:h2:file:${project.projectDir}/build/h2-db/test;NON_KEYWORDS=VALUE;AUTO_SERVER=TRUE"
def dbUsername = 'sa'
def dbPassword = ''
def dbMigration = 'src/test/resources/db/migration'

// ----------------------------------------------------------

dependencies {
    api libs.jooq

    testImplementation platform(libs.spring.boot2.dependencies)

    testImplementation project(":spring-boot-2-jooq-complete")
    testImplementation project(":test-shared-impl")
    testImplementation libs.assertj

    testImplementation libs.javax.validation.api
    testImplementation libs.kryo
    testImplementation 'org.springframework:spring-web'
    testImplementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-jooq'
    testImplementation 'org.springframework.boot:spring-boot-starter-validation'
    testImplementation 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation libs.spring.doc.openapi.ui
    testImplementation libs.spring.doc.openapi.data.rest
    testImplementation libs.bundles.jaxb
    testImplementation libs.h2

    runtimeOnly libs.jooq.codegen
    jooqCodegen libs.jooq.meta

    testCompileOnly libs.lombok
    testAnnotationProcessor libs.lombok
}

bootJar {
    enabled = false
}

// ----------------------------------------------------------

flyway {
    url = dbUrl
    user = dbUsername
    password = dbPassword
    driver = dbDriver
    defaultSchema = 'TEST_DOMAIN_FLYWAY'
    createSchemas = true
    encoding = 'UTF-8'
    locations = ["filesystem:./${dbMigration}"]
}

// ----------------------------------------------------------

jooq {
    configuration {

        jdbc {
            driver = dbDriver
            url = dbUrl
            user = dbUsername
            password = dbPassword
        }

        generator {
            database {
                name = "org.jooq.meta.h2.H2Database"
                includes = ".*"
                inputSchema = 'TEST_DOMAIN'
                recordVersionFields = 'CONCURRENCY_VERSION'
                forceIntegerTypesOnZeroScaleDecimals = true
            }

            generate {
                generatedAnnotation = false
                generatedAnnotationType = 'DETECT_FROM_JDK'
                javaTimeTypes = true
            }
            target {
                packageName = "io.domainlifecycles.test.springboot2"
            }
        }
    }
}

tasks.named("compileJava") {
    dependsOn(tasks.named("jooqCodegen"))
}

tasks.named("jooqCodegen") {
    dependsOn(tasks.named("flywayMigrate"))
    inputs.files(fileTree("src/main/resources/db/migration"))
}

// ----------------------------------------------------------

tasks.withType(Checkstyle) {
    enabled = false
}
