plugins {
  id 'net.consensys.zkevm.kotlin-application-conventions'
}

dependencies {
  implementation project(':transaction-exclusion-api:core')
  implementation project(':transaction-exclusion-api:persistence:rejectedtransaction')
  implementation project(':jvm-libs:generic:extensions:futures')
  implementation project(':jvm-libs:generic:extensions:kotlin')
  implementation project(':jvm-libs:generic:json-rpc')
  implementation project(':jvm-libs:generic:persistence:db')
  implementation project(':jvm-libs:generic:vertx-helper')
  implementation project(':jvm-libs:linea:core:long-running-service')
  implementation project(':jvm-libs:linea:core:metrics')
  implementation project(':jvm-libs:linea:metrics:micrometer')

  implementation "com.github.ben-manes.caffeine:caffeine:${libs.versions.caffeine.get()}"
  implementation "io.vertx:vertx-core"
  implementation "io.vertx:vertx-web"
  implementation "io.vertx:vertx-health-check"
  implementation "io.vertx:vertx-lang-kotlin"
  implementation "io.vertx:vertx-config"
  implementation "io.vertx:vertx-micrometer-metrics"
  implementation "info.picocli:picocli:${libs.versions.picoli.get()}"
  implementation "com.sksamuel.hoplite:hoplite-core:${libs.versions.hoplite.get()}"
  implementation "com.sksamuel.hoplite:hoplite-toml:${libs.versions.hoplite.get()}"
  implementation "io.micrometer:micrometer-registry-prometheus:${libs.versions.micrometer.get()}"
  implementation "com.fasterxml.jackson.core:jackson-annotations:${libs.versions.jackson.get()}"
  implementation "com.fasterxml.jackson.core:jackson-databind:${libs.versions.jackson.get()}"
  implementation "com.fasterxml.jackson.module:jackson-module-kotlin:${libs.versions.jackson.get()}"

  implementation(project(":jvm-libs:linea:besu-libs"))

  api("io.netty:netty-transport-native-epoll:${libs.versions.netty.get()}:linux-x86_64") {
    because "It enables native transport for Linux."
    // Note that its version should match netty version used in Vertx
  }
  api("io.netty:netty-transport-native-kqueue:${libs.versions.netty.get()}:osx-x86_64") {
    because "It enables native transport for Mac OSX."
    // Note that its version should match netty version used in Vertx
  }

  testImplementation "io.vertx:vertx-junit5"
  testImplementation "io.rest-assured:rest-assured:${libs.versions.restassured.get()}"
  testImplementation "io.rest-assured:json-schema-validator:${libs.versions.restassured.get()}"
  testImplementation "net.javacrumbs.json-unit:json-unit-assertj:${libs.versions.jsonUnit.get()}"
  testImplementation testFixtures(project(":jvm-libs:generic:extensions:kotlin"))
  testImplementation testFixtures(project(":jvm-libs:generic:persistence:db"))
  testImplementation testFixtures(project(":transaction-exclusion-api:core"))
}

application {
  mainClass = 'net.consensys.linea.transactionexclusion.app.TransactionExclusionAppMain'
}

jar {
  archiveBaseName = 'transaction-exclusion-api'
  manifest {
    attributes(
        'Class-Path': configurations.runtimeClasspath.collect { it.getName() }.findAll {it.endsWith('jar') }.join(' '),
        'Main-Class': 'net.consensys.linea.transactionexclusion.app.TransactionExclusionAppMain',
        'Multi-Release': 'true'
        )
  }
}

distributions {
  main {
    distributionBaseName = 'transaction-exclusion-api'
  }
}

run {
  workingDir = rootProject.projectDir
  jvmArgs = [
    "-Dvertx.configurationFile=config/transaction-exclusion-api/vertx.json",
    "-Dlog4j2.configurationFile=config/transaction-exclusion-api/log4j2-dev.xml"
  ] + System.properties.entrySet()
  .findAll { it.key.startsWith("config") }
  .collect { "-D${it.key}=${it.value}" }
  args = [
    "config/transaction-exclusion-api/transaction-exclusion-app-docker.config.toml",
    "config/transaction-exclusion-api/transaction-exclusion-app-local-dev.config.overrides.toml"
  ]
}

sourceSets {
  integrationTest {
    kotlin {
      compileClasspath += main.output
      runtimeClasspath += main.output
    }
    compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath + sourceSets.test.compileClasspath
    runtimeClasspath += sourceSets.main.output + sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath
  }
}

task integrationTest(type: Test) { test ->
  systemProperty "vertx.configurationFile", "vertx-options.json"

  description = "Runs integration tests."
  group = "verification"
  useJUnitPlatform()

  classpath = sourceSets.integrationTest.runtimeClasspath
  testClassesDirs = sourceSets.integrationTest.output.classesDirs

  dependsOn(":localStackPostgresDbOnlyComposeUp")
}

task integrationTestAllNeeded {
  description = "Runs integration tests of transaction-exclusion-api:app and that of all dependencies"
  group = "verification"

  def projectDependencies = configurations.implementation.getAllDependencies().withType(ProjectDependency)
  def dependentProjects = projectDependencies.dependencyProject
  dependentProjects.each { dependency ->
    if (dependency.project.tasks.findByName("integrationTest")) {
      def integrationTestTask = "${dependency.path}:integrationTest"
      dependsOn integrationTestTask
    }
  }
  dependsOn "integrationTest"
}
