plugins {
  id 'net.consensys.zkevm.kotlin-library-conventions'
  id 'com.gradleup.shadow' version '8.3.5'
}

group = 'build.linea.staterecovery'
archivesBaseName = 'linea-staterecovery-besu-plugin'

dependencies {
  compileOnly("info.picocli:picocli:${libs.versions.picoli.get()}") {
    because 'Required for command line parsing. Provided by Besu at runtime.'
  }
  implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
  api(project(":jvm-libs:generic:vertx-helper"))
  api(project(":jvm-libs:generic:serialization:jackson"))
  api(project(":jvm-libs:linea:metrics:micrometer"))
  api(project(":jvm-libs:linea:clients:eth-logs-searcher"))
  api(project(":jvm-libs:linea:clients:linea-contract-clients"))
  api(project(":jvm-libs:linea:web3j-extensions"))
  api(project(":besu-plugins:state-recovery:appcore:logic"))
  api(project(":besu-plugins:state-recovery:clients:blobscan-client"))
  api(project(":besu-plugins:state-recovery:clients:eth-api"))
}

ext.groupsToIncludeInShadow = [
  "build.linea.*",
  "io.micrometer.*",
  "io.vertx:vertx-web-client",
  "io.vertx:vertx-uri-template",
  "org.jetbrains.kotlin:kotlin-reflect.*",
  "org.jetbrains.kotlinx:kotlinx-datetime.*",
  "tech.pegasys.teku.internal:async",
  "com.michael-bull.kotlin-result:kotlin-result.*",
  "com.fasterxml.jackson.module:jackson-module-kotlin",
]

def canIncludeLib(String lib) {
  def allowed = groupsToIncludeInShadow.find { libAllowed ->
    lib.matches(libAllowed)
  } != null
  allowed
}

tasks.create("testLibsInclusion", Task) {
  //  def lib = "build.linea.internal:kotlin-extensions"
  doLast {
    def lib = "com.michael-bull.kotlin-result:kotlin-result"
    println("Can include $lib: ${canIncludeLib(lib)}")
  }
}

shadowJar {
  if ("${project.version}" != 'unspecified') {
    archiveClassifier.set('')
    archiveVersion.set("${project.version}")
  } else {
    archiveClassifier.set('SNAPSHOT')
  }

  dependencies {
    exclude { dependency ->
      !canIncludeLib("${dependency.moduleGroup}:${dependency.moduleName}")
    }
  }

  manifest {
    attributes(
        'Specification-Title': archiveBaseName,
        'Specification-Version': project.version,
        'Implementation-Title': archiveBaseName,
        'Implementation-Version': project.version,
        )
  }
}
