plugins {
    id 'java'
    id 'application'
    id 'edu.sc.seis.launch4j' version '2.5.4'
}

// JDK 版本检查
def requiredJavaVersion = 20
def currentJavaVersion = JavaVersion.current().majorVersion.toInteger()
if (currentJavaVersion != requiredJavaVersion) {
    throw new GradleException("构建需要 JDK ${requiredJavaVersion}，但当前是 JDK ${currentJavaVersion}。请更换 JDK 环境。")
}

group = 'com.axis.innovators.box'
version = '0.0.1'

repositories {
    mavenLocal()
    maven { url "https://maven.aliyun.com/repository/public" }
    mavenCentral()
    jcenter()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    implementation 'org.commonmark:commonmark:0.24.0'
    implementation 'org.commonjava.googlecode.markdown4j:markdown4j:2.2-cj-1.1'
    implementation 'com.google.code.gson:gson:2.8.9'

    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'

    implementation 'org.ow2.asm:asm:7.1'
    implementation 'org.ow2.asm:asm-commons:7.1'
    implementation 'org.ow2.asm:asm-analysis:7.1'
    implementation 'org.ow2.asm:asm-util:7.0'
    implementation 'org.ow2.asm:asm-tree:7.1'
}

// 分离依赖项到 libs 目录
task copyDependencies(type: Copy) {
    from configurations.runtimeClasspath
    into "$buildDir/libs/libs"
}

// 执行我生成jar
jar {
    manifest {
        attributes 'Main-Class': 'com.axis.innovators.box.Main',
                'Class-Path': configurations.runtimeClasspath.files.collect { "libs/$it.name" }.join(' ')
    }
    dependsOn copyDependencies
}

// 测试配置
test {
    useJUnitPlatform()
    systemProperty "java.system.class.loader", "com.axis.innovators.box.plugins.BoxClassLoader"

    // 确保测试能看到依赖
    classpath = files(sourceSets.test.output) +
            files("$buildDir/libs/libs") +
            configurations.testRuntimeClasspath
}

// 处理开源文档文件
sourceSets {
    main {
        resources {
            exclude '**/*.md'
        }
    }
    openSourceDocs {
        resources {
            srcDir 'src/main/resources'
            include '**/*.md'
        }
    }
}

tasks.withType(JavaExec).configureEach {
    jvmArgs = [
            '-Djava.system.class.loader=com.axis.innovators.box.plugins.BoxClassLoader'
    ]
}

// 单独打包文档
task packageOpenSourceDocs(type: Jar) {
    archiveClassifier = 'docs'
    from sourceSets.openSourceDocs.resources
    destinationDirectory = file("$buildDir/libs/docs")
}

// 完整的 application 配置
application {
    mainClass = 'com.axis.innovators.box.Main'

    // 确保运行时参数生效
    applicationDefaultJvmArgs = [
            "-Djava.system.class.loader=com.axis.innovators.box.plugins.BoxClassLoader",
            "-Dloader.library.path=$buildDir/libs/libs"
    ]
}

// 创建可运行分发
tasks.register('release') {
    dependsOn build, packageOpenSourceDocs

    doLast {
        copy {
            from "$buildDir/libs"
            into "$buildDir/dist"
            include '*.jar'
            include 'docs/**'
            include 'libs/**'
        }
        println "Release package ready at: $buildDir/dist"
    }
}

// 默认构建任务
build.dependsOn release