plugins {
	id "java"
	id "idea"
	id "jacoco"
	id "application"
	id "io.freefair.lombok" version "8.14.2"
	id "org.danilopianini.git-sensitive-semantic-versioning-gradle-plugin" version "5.1.8"
	id "com.diffplug.spotless" version "7.2.1"
}

group = "com.autonomouslogic.esiproxy"

ext {
	mainClassName = "com.autonomouslogic.eveesiproxy.EveEsiProxy"
	envProperties = new Properties()
	if (file("local.env").exists()) {
		file("local.env").withInputStream { envProperties.load(it) }
	}
}

repositories {
	mavenCentral()
}

java {
	withJavadocJar()
	withSourcesJar()
	sourceCompatibility = JavaVersion.VERSION_21
	targetCompatibility = JavaVersion.VERSION_21
}

dependencies {
	annotationProcessor "com.google.dagger:dagger-compiler:2.57.2"
//	implementation "io.reactivex.rxjava3:rxjava:3.1.8"
	implementation "com.google.dagger:dagger:2.57.2"

	// Logging
	implementation "org.apache.logging.log4j:log4j-api:2.25.2"
	implementation platform("org.apache.logging.log4j:log4j-bom:2.25.2")
	runtimeOnly "org.apache.logging.log4j:log4j-core:2.25.2"
//	runtimeOnly "org.slf4j:jul-to-slf4j:2.0.17"
	runtimeOnly "org.apache.logging.log4j:log4j-slf4j2-impl:2.25.2"
	runtimeOnly "org.apache.logging.log4j:log4j-jul:2.25.2"

	// Jackson
	implementation "com.fasterxml.jackson.core:jackson-annotations:2.20"
	implementation "com.fasterxml.jackson.core:jackson-core:2.20.0"
	implementation "com.fasterxml.jackson.core:jackson-databind:2.20.0"
	implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.20.0"

	// Helidon
	implementation "io.helidon.webserver:helidon-webserver:4.3.1"
	implementation "io.helidon.logging:helidon-logging-slf4j:4.3.1"

	// Misc
	implementation "com.squareup.okhttp3:okhttp:4.12.0"
//	implementation "org.yaml:snakeyaml:2.3"
	implementation "com.autonomouslogic.commons:commons-java:1.9.8"
	implementation "commons-io:commons-io:2.20.0"
	implementation "com.google.guava:guava:33.5.0-jre"
	implementation "com.github.scribejava:scribejava-core:8.3.3"
	implementation "commons-codec:commons-codec:1.19.0"
	implementation "org.apache.commons:commons-lang3:3.19.0"
	implementation "org.thymeleaf:thymeleaf:3.1.3.RELEASE"
	implementation "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.4.0"
	implementation "io.reactivex.rxjava3:rxjava:3.1.12"

	// Tests
	testAnnotationProcessor "com.google.dagger:dagger-compiler:2.57.2"
	testImplementation "com.google.dagger:dagger:2.57.2"
	testImplementation "org.junit.jupiter:junit-jupiter:5.14.0"
	testImplementation "org.junit.jupiter:junit-jupiter-params:5.14.0"
	testImplementation "org.junit-pioneer:junit-pioneer:2.3.0"
	testImplementation "org.mockito:mockito-junit-jupiter:5.20.0"
	testImplementation "com.squareup.okhttp3:mockwebserver:4.12.0"
	testImplementation "com.tngtech.archunit:archunit:1.4.1"
}

task versionFile()  {
	doLast {
		new File(projectDir, "src/main/resources/version").text = "$version\n"
	}
}

test {
	useJUnitPlatform()
}

jacocoTestReport {
	executionData(test)
	reports {
		xml.required = true
		html.required = true
	}
}

spotless {
	java {
		target "src/**/*.java"
		palantirJavaFormat()
		leadingSpacesToTabs()
	}

	groovyGradle {
		leadingSpacesToTabs()
	}
}

gitSemVer {
	buildMetadataSeparator.set("-")
}

test {
	environment "LOG_LEVEL", "TRACE"
	environment "CONFIG_DIR", "/tmp"
	jvmArgs "-client", "-XX:+UseG1GC", "-Xmx8g"

	useJUnitPlatform()
	testLogging {
		events = [ "FAILED", "PASSED", "SKIPPED" ]
		showExceptions = true
		exceptionFormat = "FULL"
		showExceptions = true
		showCauses = true
		showStackTraces = true
	}
	// https://junit-pioneer.org/docs/environment-variables/#warnings-for-reflective-access
	jvmArgs("--add-opens", "java.base/java.util=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED")
}

application {
	mainClass = mainClassName
	applicationDefaultJvmArgs = [
			"-server",
			"-XX:+UseZGC",
			"-XX:+PrintCommandLineFlags",
			"-XX:MinHeapSize=" + 10 * 1024 * 1024,
			"-XX:MaxRAMPercentage=80.0",
			"-XX:+UseCompressedOops"
	]
}

run {
	environment "LOG_LEVEL", "TRACE"
	jvmArgs "-client", "-XX:+UseZGC", "-Xmx2g"
	environment envProperties
}

wrapper {
	distributionType = "ALL"
}
