import org.openapitools.generator.gradle.plugin.tasks.GenerateTask
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
    id("org.openapi.generator") version "7.12.0"
    id("org.springframework.boot") version "3.4.4"
    id("io.spring.dependency-management") version "1.1.7"
    java
}

repositories {
    mavenCentral()
}

tasks.named("compileJava") {
    dependsOn("generateJavaApi")
    dependsOn("generateJavaChatApi")
    dependsOn("generateFrontendApi")
}

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

tasks.named<BootJar>("bootJar") {
    enabled = false
}

tasks.jar {
    enabled = true
}


sourceSets {
    main {
        java {
            srcDir("${projectDir}/build/generated-sources/model/src/main/java")
            exclude("org/openapitools/configuration/SpringDocConfiguration.java")
        }
    }
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("io.swagger.core.v3:swagger-annotations:2.2.37")
    implementation("io.swagger.core.v3:swagger-models:2.2.37")
    implementation("io.swagger.core.v3:swagger-core:2.2.37")

    implementation("org.openapitools:jackson-databind-nullable:0.2.6")
    implementation("org.springframework.hateoas:spring-hateoas:2.2.0")
}

tasks.register<GenerateTask>("generateJavaApi") {
    inputs.file("${projectDir}/src/main/resources/api.yml")
    inputs.file("${projectDir}/src/main/resources/config_backend.json")
    inputs.dir("${projectDir}/src/main/resources/schemas")
    generatorName.set("spring")
    library.set("spring-boot")
    inputSpec.set("${projectDir}/src/main/resources/api.yml")
    configFile.set("${projectDir}/src/main/resources/config_backend.json")
    outputDir.set("${projectDir}/build/generated-sources/model")
    typeMappings.put("OffsetDateTime", "Instant")
    importMappings.put("java.time.OffsetDateTime", "java.time.Instant")
    modelNameSuffix.set("Dto")

    // Make generator use Java 8 time types and map date-time -> Instant
    additionalProperties.set(mapOf("dateLibrary" to "java8"))
    typeMappings.set(mapOf("date-time" to "Instant"))
    importMappings.set(mapOf("Instant" to "java.time.Instant"))
    generateModelTests.set(false)
    generateModelDocumentation.set(false)
    generateApiTests.set(false)
    generateApiDocumentation.set(false)
    generateAliasAsModel.set(true)
    modelPackage.set("com.alttd.altitudeweb.model")
    generateApiTests.set(false)
}

tasks.register<GenerateTask>("generateJavaChatApi") {
    inputs.file("${projectDir}/src/main/resources/chat-api.yml")
    inputs.file("${projectDir}/src/main/resources/config_backend.json")
    inputs.dir("${projectDir}/src/main/resources/schemas")
    generatorName.set("spring")
    library.set("spring-boot")
    inputSpec.set("${projectDir}/src/main/resources/chat-api.yml")
    configFile.set("${projectDir}/src/main/resources/config_backend.json")
    outputDir.set("${projectDir}/build/generated-sources/model")
    typeMappings.put("OffsetDateTime", "Instant")
    importMappings.put("java.time.OffsetDateTime", "java.time.Instant")
    modelNameSuffix.set("Dto")

    // Make generator use Java 8 time types and map date-time -> Instant
    additionalProperties.set(mapOf("dateLibrary" to "java8"))
    typeMappings.set(mapOf("date-time" to "Instant"))
    importMappings.set(mapOf("Instant" to "java.time.Instant"))
    generateModelTests.set(false)
    generateModelDocumentation.set(false)
    generateApiTests.set(false)
    generateApiDocumentation.set(false)
    generateAliasAsModel.set(true)
    modelPackage.set("com.alttd.altitudeweb.model")
    generateApiTests.set(false)
}

tasks.register<GenerateTask>("generateFrontendApi") {
    inputs.file("${projectDir}/src/main/resources/api.yml")
    inputs.file("${projectDir}/src/main/resources/config_backend.json")
    inputs.dir("${projectDir}/src/main/resources/schemas")
    generatorName.set("typescript-angular")
    inputSpec.set("$rootDir/open_api/src/main/resources/api.yml")
    outputDir.set("${projectDir}/../frontend/src/api")
    configFile.set("$rootDir/open_api/src/main/resources/config_frontend.json")
    generateApiTests.set(false)
}
