/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

plugins {
  id("java")
  id("idea")
  alias(libs.plugins.shadow)
}

dependencies {
  // Force upgrade for outdated transitive libthrift pulled by Hive Metastore
  constraints {
    compileOnly(libs.thrift)
    testImplementation(libs.thrift)
  }

  compileOnly(project(":api"))
  compileOnly(project(":common"))
  compileOnly(project(":core"))

  compileOnly(libs.hive2.metastore) {
    exclude(group = "log4j")
    exclude(group = "org.apache.logging.log4j")
  }
  compileOnly(libs.immutables.value)
  compileOnly(libs.lombok)
  compileOnly(libs.caffeine)
  compileOnly(libs.guava)
  compileOnly(libs.slf4j.api)

  implementation(project(":catalogs:catalog-common")) {
    exclude("*")
  }

  annotationProcessor(libs.immutables.value)
  annotationProcessor(libs.lombok)

  testImplementation(project(":api"))
  testImplementation(project(":common"))
  testImplementation(project(":core"))
  testImplementation(project(":integration-test-common", "testArtifacts"))

  testImplementation(libs.bundles.log4j)
  testImplementation(libs.commons.collections3)
  testImplementation(libs.commons.configuration1)
  testImplementation(libs.datanucleus.api.jdo)
  testImplementation(libs.datanucleus.core)
  testImplementation(libs.datanucleus.jdo)
  testImplementation(libs.datanucleus.rdbms)
  testImplementation(libs.derby)
  testImplementation(libs.hadoop3.auth) {
    exclude("*")
  }
  testImplementation(libs.hadoop3.common) {
    exclude("*")
  }
  testImplementation(libs.hadoop3.mapreduce.client.core) {
    exclude("*")
  }
  // Hadoop 3.x runtime requirements (stripped by exclude("*") above)
  testImplementation(libs.hadoop3.shaded.guava)
  testImplementation(libs.hadoop3.shaded.protobuf)
  testImplementation(libs.commons.configuration2)
  testImplementation(libs.re2j)
  testImplementation(libs.hive2.exec) {
    artifact {
      classifier = "core"
    }
    exclude("com.google.code.findbugs", "jsr305")
    exclude("com.google.protobuf")
    exclude("org.apache.avro")
    exclude("org.apache.ant")
    exclude("org.apache.calcite")
    exclude("org.apache.calcite.avatica")
    exclude("org.apache.curator")
    exclude("org.apache.derby")
    exclude("org.apache.hadoop", "hadoop-yarn-server-resourcemanager")
    exclude("org.apache.hive", "hive-llap-tez")
    exclude("org.apache.hive", "hive-vector-code-gen")
    exclude("org.apache.ivy")
    exclude("org.apache.logging.log4j")
    exclude("org.apache.zookeeper")
    exclude("org.codehaus.groovy", "groovy-all")
    exclude("org.datanucleus", "datanucleus-core")
    exclude("org.eclipse.jetty.aggregate", "jetty-all")
    exclude("org.eclipse.jetty.orbit", "javax.servlet")
    exclude("org.openjdk.jol")
    exclude("org.pentaho")
    exclude("org.slf4j")
  }
  testImplementation(libs.hive2.metastore) {
    exclude("ant")
    exclude("co.cask.tephra")
    exclude("com.github.joshelser")
    exclude("com.google.code.findbugs", "jsr305")
    exclude("com.google.code.findbugs", "sr305")
    exclude("com.tdunning", "json")
    exclude("com.zaxxer", "HikariCP")
    exclude("io.dropwizard.metrics")
    exclude("javax.transaction", "transaction-api")
    exclude("org.apache.ant")
    exclude("org.apache.avro")
    exclude("org.apache.curator")
    exclude("org.apache.derby")
    exclude("org.apache.hadoop", "hadoop-yarn-server-resourcemanager")
    exclude("org.apache.hbase")
    exclude("org.apache.logging.log4j")
    exclude("org.apache.parquet", "parquet-hadoop-bundle")
    exclude("org.apache.zookeeper")
    exclude("org.datanucleus")
    exclude("org.eclipse.jetty.aggregate", "jetty-all")
    exclude("org.eclipse.jetty.orbit", "javax.servlet")
    exclude("org.openjdk.jol")
    exclude("org.slf4j")
  }
  testImplementation(libs.caffeine)
  testImplementation(libs.junit.jupiter.api)
  testImplementation(libs.mockito.core)
  testImplementation(libs.mockito.inline)
  testImplementation(libs.testcontainers)
  testImplementation(libs.woodstox.core)

  testRuntimeOnly(libs.junit.jupiter.engine)
}

val testJar by tasks.registering(Jar::class) {
  archiveClassifier.set("tests")
  from(sourceSets["test"].output)
}

configurations {
  create("testArtifacts")
}

artifacts {
  add("testArtifacts", testJar)
}

// Ensure the shaded Hive metastore lib jars exist before compiling this module,
// since compileOnly(project(":catalogs:hive-metastore{2,3}-libs")) puts those
// jars on the compile classpath and they are produced by the copyDepends tasks.
tasks.named<JavaCompile>("compileJava") {
  dependsOn(
    ":catalogs:hive-metastore2-libs:copyDepends",
    ":catalogs:hive-metastore3-libs:copyDepends"
  )
}
