plugins {
    id("io.micronaut.application") version "4.5.4"
    id("com.gradleup.shadow") version "8.3.7"
    id("io.micronaut.aot") version "4.5.4"
}

// group = FROST_APP_GROUP
// version = FROST_APP_VERSION

repositories {
    // 保留 mavenCentral(), 确保在 IDEA 中，通过代码点击进依赖源码时，右上角 [下载源代码] 按钮可以正常使用
    mavenCentral()
}

dependencies {
    // implementation platform(...) 仅传递版本约束，不传递实际依赖。例如: 统一版本管理（BOM）
    // implementation project(...)  传递所有依赖（代码 + 依赖项）

    // 必需依赖
    // implementation platform(project(":frost-iot-dependencies"))
    implementation project(":frost-iot-dependencies")
    implementation project(":common")

    // micronaut 相关依赖
    annotationProcessor("io.micronaut:micronaut-http-validation")
    annotationProcessor("io.micronaut.serde:micronaut-serde-processor")
    implementation("io.micronaut.serde:micronaut-serde-jackson")
    compileOnly("io.micronaut:micronaut-http-client")
    runtimeOnly("ch.qos.logback:logback-classic")
    testImplementation("io.micronaut:micronaut-http-client")

    // 包含 YAML 解析 (micronaut 4.x 起不再默认包含，需要手动引入)
    // see: https://micronaut.io/2023/02/19/micronaut-framework-4-0-and-snakeyaml-transitive-dependency/
    // Micronaut Framework 4.0, to be released in 2023, will not expose SnakeYAML as a transitive dependency.
    // Micronaut Framework uses SnakeYAML to read Application Configuration from files such as application.yml or bootstrap.yml.
    // Micronaut Framework 4.0 will not expose SnakeYAML as a transitive dependency. We don’t pull an unnecessary dependency if you are not using YAML.
    // You do not have to specify a version number since Micronaut BOM (Bill of Materials) specifies a SnakeYAML version.
    runtimeOnly("org.yaml:snakeyaml")

    // log.info 编码配置需要使用 logback API
    // 编码配置代码见: EncodingInitializer
    compileOnly("ch.qos.logback:logback-classic")

    // lombok 依赖注解处理
    annotationProcessor("org.projectlombok:lombok")

    // lombok 依赖处理需要在此之前

    // // Micronaut JPA 相关依赖
    // // docs: https://micronaut.bookhub.tech/action/db/jpa
    // // JPA 使用的注解来源于依赖 jakarta.annotation-api
    // compileOnly("jakarta.annotation:jakarta.annotation-api")
    // // micronaut-jdbc-hikari 封装的对接 hikari 连接池的依赖
    // implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    // // micronaut-hibernate-jpa 是使用 Hibernate JPA 的核心依赖
    // implementation("io.micronaut.sql:micronaut-hibernate-jpa")
    // // micronaut-serde-jackson 带有实体的序列化注解
    // implementation("io.micronaut.serde:micronaut-serde-jackson")

    // Micronaut Data 相关依赖
    // docs: https://micronaut.bookhub.tech/data/introduction

    // Micronaut Data JPA Hibernate 相关依赖
    // docs: https://guides.micronaut.io/latest/micronaut-jpa-hibernate-gradle-java.html
    // Configures Hibernate/JPA EntityManagerFactory beans
    implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
    // Micronaut Data Transaction Hibernate dependency
    implementation("io.micronaut.data:micronaut-data-tx-hibernate")
    // 连接池 (推荐 HikariCP)
    // Configures SQL DataSource instances using Hikari Connection Pool
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    runtimeOnly("org.postgresql:postgresql") // PostgreSQL 驱动

    // Micronaut Data Hibernate Reactive 相关依赖
    // docs: https://micronaut.bookhub.tech/data/hibernateReactive
    // annotationProcessor("io.micronaut.data:micronaut-data-processor")
    // implementation("io.micronaut.data:micronaut-data-hibernate-reactive")
    // implementation("io.micronaut.sql:micronaut-hibernate-reactive")
    // runtimeOnly("io.micronaut.reactor:micronaut-reactor") // 如果使用Reactor

    // jetbrains 编译时注解
    implementation("org.jetbrains:annotations:${libraries.jetbrainsAnnotations}")

    // OpenAPI/Swagger Support
    // see: https://micronaut-projects.github.io/micronaut-openapi/latest/guide/
    // OpenAPI 注解支持
    annotationProcessor("io.micronaut.openapi:micronaut-openapi")
    compileOnly("io.micronaut.openapi:micronaut-openapi-annotations")
    // Micronaut OpenAPI 支持
    // implementation("io.micronaut.openapi:micronaut-openapi")

    // 其他业务依赖
    // ...
}

// OpenAPI/Swagger Support
tasks.withType(JavaCompile).configureEach {
    options.compilerArgs = [
            '-parameters'
    ]
}

// 2025.05.25 执行 publishToMavenCentralPortal 前确保 distZip 已执行
tasks.named('publishToMavenCentralPortal') {
    // 将 distZip 的输出声明为 publishToMavenCentralPortal 的输入
    inputs.files(tasks.named('distZip'))
}

application {
    mainClass = "${group}.core.Application"
}
java {
    // sourceCompatibility = JavaVersion.toVersion("17")
    // targetCompatibility = JavaVersion.toVersion("17")

    // // 指定编译 Java 源代码时使用的 Java 语言版本（即源代码的语法版本）
    // // 影响生成的 .class 文件的字节码版本
    // sourceCompatibility = JavaVersion.VERSION_17 // JavaVersion.toVersion("17")
    //
    // // 指定生成的字节码（.class 文件）的目标 JVM 版本
    // // 通常与 sourceCompatibility 保持一致
    // targetCompatibility = JavaVersion.VERSION_17 // JavaVersion.toVersion("17")
}


graalvmNative.toolchainDetection = false

micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("${group}.core.*")
    }
    aot {
        // Please review carefully the optimizations enabled below
        // Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
        optimizeServiceLoading = false
        convertYamlToJava = false
        precomputeOperations = true
        cacheEnvironment = true
        optimizeClassLoading = true
        deduceEnvironment = true
        optimizeNetty = true
        replaceLogbackXml = true
    }
}


tasks.named("dockerfileNative") {
    jdkVersion = "21"
}
