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

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


java {
    withJavadocJar()
    withSourcesJar()
}

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

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 {
    compileOnly libs.slf4j.api
    api libs.jooq
    implementation libs.classgraph

    api project(":types")
    implementation project(":reflect")
    implementation project(":access")
    api project(":builder")
    implementation project(":mirror")
    api project(":persistence")

    testImplementation project(":test-shared-impl")
    testImplementation project(":type-utils")
    testImplementation project(":assertions")
    testImplementation project(":bean-validations")
    testImplementation project(":validation-extender")


    testImplementation libs.assertj
    testImplementation libs.slf4j.api
    testImplementation libs.logback.classic
    testImplementation libs.kryo
    testImplementation libs.mockito.core
    testImplementation libs.h2

    //jakarta config
    testImplementation libs.hibernate.validator
    testImplementation libs.glassfish.jakarta.el
    testImplementation libs.jakarta.el.api

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

    testImplementation libs.lombok
    testAnnotationProcessor libs.lombok
}

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"
            }
        }
    }
}

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

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

tasks.named('sourcesJar') {
    dependsOn(tasks.named('jooqCodegen'))
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

jar {
    exclude('io/domainlifecycles/test/**')
}

// Although test classes are not scanned by checkstyle, as configured in the root-level build.gradle file,
// checkstyle will throw errors and hints as soon as you add a JavaDoc in test file methods, which is the case in some
// of the more complex test files in this module. That's why the checkstyleTest task is explicitly disabled.
checkstyleTest.enabled = false
