plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.9.25'
    id 'org.jetbrains.kotlin.plugin.spring' version '1.9.25'
    id 'org.springframework.boot' version '3.4.2'
    id 'io.spring.dependency-management' version '1.1.7'
    id 'org.jetbrains.kotlin.plugin.jpa' version '1.9.25'
    id 'org.openapi.generator' version '7.11.0'
    id 'org.jlleitschuh.gradle.ktlint' version '13.1.0'
}

group = 'com.betriebsratkanzlei'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

ext {
    openHtmlVersion = '1.0.10'
}

dependencies {
    implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-reflect'
    implementation 'org.liquibase:liquibase-core'
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.6'
    implementation 'org.springdoc:springdoc-openapi-ui:1.8.0'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    // https://docs.spring.io/spring-security/reference/servlet/oauth2/index.html#oauth2-resource-server-access-token-jwt
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation "com.openhtmltopdf:openhtmltopdf-core:$openHtmlVersion"
    implementation "com.openhtmltopdf:openhtmltopdf-pdfbox:$openHtmlVersion"
    implementation "com.openhtmltopdf:openhtmltopdf-java2d:$openHtmlVersion"
    implementation "com.openhtmltopdf:openhtmltopdf-slf4j:$openHtmlVersion"
    implementation "com.openhtmltopdf:openhtmltopdf-svg-support:$openHtmlVersion"
    runtimeOnly 'com.h2database:h2'
    implementation 'org.postgresql:postgresql'
    implementation 'org.springframework.boot:spring-boot-testcontainers'
    implementation 'org.testcontainers:postgresql'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

kotlin {
    compilerOptions {
        freeCompilerArgs.addAll '-Xjsr305=strict'
    }
}

allOpen {
    annotation 'jakarta.persistence.Entity'
    annotation 'jakarta.persistence.MappedSuperclass'
    annotation 'jakarta.persistence.Embeddable'
}

ktlint {
    version = "1.5.0"
    android = false
    ignoreFailures = false
    filter {
        exclude("**/generated/**")
        exclude { element -> element.file.path.contains("generated/") }
    }
}

def generatedSourcesServerLegalconsenthubDir = "$buildDir/generated/server".toString()

sourceSets {
    main {
        kotlin.srcDirs += generatedSourcesServerLegalconsenthubDir + '/src/main/kotlin'
    }
}

tasks.named('test') {
    useJUnitPlatform()
}

task generate_legalconsenthub_server(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
    generatorName = 'kotlin-spring'
    inputSpec = "$rootDir/api/legalconsenthub.yml".toString()
    outputDir = generatedSourcesServerLegalconsenthubDir
    apiPackage = 'com.betriebsratkanzlei.legalconsenthub_api.api'
    modelPackage = 'com.betriebsratkanzlei.legalconsenthub_api.model'
    groupId = 'com.betriebsratkanzlei'
    id = 'legalconsenthub_api'
    configOptions = [useTags           : 'true',
                     enumPropertyNaming: 'original',
                     interfaceOnly     : 'true',
                     useSpringBoot3    : 'true']
    typeMappings = [DateTime: "LocalDateTime"]
    importMappings = [LocalDateTime: "java.time.LocalDateTime"]
}

compileKotlin.dependsOn(tasks.generate_legalconsenthub_server)
