/*
 * 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 {
  alias(libs.plugins.openapi.generator)
  alias(libs.plugins.diffplug.spotless)
  id 'java-library'
}

import com.diffplug.gradle.spotless.JavaExtension


description = 'Solrj - Solr Java Client'

dependencies {
  implementation libs.fasterxml.jackson.core.databind
  implementation libs.fasterxml.jackson.core.annotations
  implementation libs.fasterxml.jackson.core.core
  api project(":solr:api")

  implementation libs.slf4j.api
  runtimeOnly libs.slf4j.jcloverslf4j

  api libs.eclipse.jetty.http2.client
  implementation libs.eclipse.jetty.http2.httpclienttransport
  implementation libs.eclipse.jetty.http
  implementation libs.eclipse.jetty.client
  implementation libs.eclipse.jetty.util
  implementation libs.eclipse.jetty.io
  runtimeOnly libs.eclipse.jetty.alpnjavaclient

  implementation(libs.apache.httpcomponents.httpmime, {
    exclude group: "commons-codec", module: "commons-codec"
    exclude group: "commons-logging", module: "commons-logging"
  })
  implementation libs.apache.httpcomponents.httpclient
  implementation libs.apache.httpcomponents.httpcore

  compileOnly libs.stephenc.jcip.annotations

  testImplementation project(':solr:test-framework')
  testImplementation project(':solr:core')
  testImplementation project(':solr:solrj')

  // ideally ZK centric tests move to solrj-zookeeper but sometimes we depend on ZK here anyway
  testImplementation project(':solr:solrj-zookeeper')
  testImplementation(libs.apache.zookeeper.zookeeper, {
    exclude group: "org.apache.yetus", module: "audience-annotations"
  })
  permitTestUnusedDeclared libs.apache.zookeeper.zookeeper
  testImplementation libs.apache.zookeeper.jute

  testImplementation libs.apache.lucene.core
  testImplementation libs.apache.lucene.testframework

  testImplementation libs.carrotsearch.randomizedtesting.runner
  testImplementation libs.junit.junit
  testImplementation libs.hamcrest.hamcrest

  testImplementation libs.commonsio.commonsio
  testImplementation libs.fasterxml.jackson.dataformat.cbor

  testImplementation libs.jakarta.servlet.api

  testImplementation libs.eclipse.jetty.server
  testImplementation libs.eclipse.jetty.ee10.servlet
  testImplementation libs.eclipse.jetty.ee10.webapp
  testRuntimeOnly(libs.eclipse.jetty.alpnjavaserver, {
    exclude group: "org.eclipse.jetty.alpn", module: "alpn-api"
  })
  testImplementation libs.eclipse.jetty.session
  testImplementation(libs.mockito.core, {
    exclude group: "net.bytebuddy", module: "byte-buddy-agent"
  })
  testRuntimeOnly(libs.mockito.subclass, {
    exclude group: "net.bytebuddy", module: "byte-buddy-agent"
  })
  testRuntimeOnly(libs.apache.log4j.slf4j2impl, {
    exclude group: "org.apache.logging.log4j", module: "log4j-api"
  })
  testImplementation libs.apache.commons.lang3
  testImplementation libs.dropwizard.metrics.core
  testImplementation libs.fasterxml.jackson.core.core
}

/**
 * Java Code Generation for Solr API
 */

evaluationDependsOn(":solr:api")

configurations {
  openApiSpecFile {
    canBeConsumed = false
  }
}

ext {
  generatedCodeDir = "${buildDir}/generated/"
  javaTemplateDir = "${projectDir}/src/resources/java-template"
}

dependencies {
  openApiSpecFile(project(path: ":solr:api", configuration: "openapiSpec"))
}

/**
 * Code Generation task
 */
openApiGenerate {
  generatorName = "java"
  inputSpec = project(":solr:api").openApiSpecFile

  // Add 'debugModels: ""' or 'debugOperations: ""' to get the JSON input to mustache templating for those components
  globalProperties.set([apis: "", models: "false"])
  templateDir = project.javaTemplateDir
  apiPackage = "org.apache.solr.client.solrj.request"
  outputDir = project.generatedCodeDir
  generateApiTests = false
  generateModelTests = false
  generateApiDocumentation = false
  generateModelDocumentation = false
  cleanupOutput = true
  additionalProperties = ["modelPackage": "org.apache.solr.client.api.model"]
}

tasks.openApiGenerate.dependsOn configurations.openApiSpecFile

def generatedFiles = files("${project.generatedCodeDir}/src/main/java") {
  builtBy tasks.openApiGenerate
}

/**
 * Setup Spotless (Code formatting) for the generated java files
 */
def generatedExt = new JavaExtension(spotless)
project.spotlessJavaSetup.execute(generatedExt)
generatedExt.target(generatedFiles)
def generatedSpotlessTask = generatedExt.createIndependentApplyTask("generatedSpotless")
generatedSpotlessTask.group("build")
generatedSpotlessTask.description("Apply formatting for generated code")

tasks.openApiGenerate.finalizedBy generatedSpotlessTask

/**
 * Add the Generated code to the SolrJ Source paths
 */

sourceSets {
  main {
    java {
      srcDir generatedFiles
    }
  }
}
