/*
 * 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.swagger3.core)
  alias(libs.plugins.openapi.generator)
}

apply plugin: 'java-library'

description = 'API - Interfaces and classes used to represent Solrs APIs'

ext {
    jsClientDir = "${buildDir}/generated/js"
    pythonClientDir = "${buildDir}/generated/python"
    openApiSpecDir = "${buildDir}/generated/openapi"
    openApiSpecFile = "${project.openApiSpecDir}/solr-openapi-${version}.json"
}

configurations {
    openapiSpec {
        canBeConsumed = true
        canBeResolved = false
    }
    jsClient {
        canBeConsumed = true
        canBeResolved = false
    }
    pythonClient {
        canBeConsumed = true
        canBeResolved = false
    }

    swaggerBuild.extendsFrom implementation
}

resolve {
  classpath = sourceSets.main.runtimeClasspath
  buildClasspath = sourceSets.main.runtimeClasspath.plus(configurations.getByName("swaggerBuild"))
  resourcePackages = ["org.apache.solr.client.api.util", "org.apache.solr.client.api.endpoint"]
  outputDir = file(project.openApiSpecDir)
  outputFileName = "solr-openapi-${version}"
  prettyPrint = true
// Ignore resources not annotated with 'Operation', useful for omitting endpoints from OAS
  readAllResources = false
}

dependencies {
    api platform(project(":platform"))
    swaggerDeps platform(project(":platform"))
    api platform(libs.fasterxml.jackson.bom)

    runtimeOnly libs.slf4j.api

    implementation libs.jakarta.ws.rsapi
    api libs.fasterxml.jackson.core.annotations
    api libs.swagger3.annotations.jakarta
    implementation libs.semver4j.semver4j

    testImplementation project(':solr:test-framework')
    testImplementation project(':solr:api')
    testImplementation libs.junit.junit
    testImplementation libs.hamcrest.hamcrest
    testImplementation libs.apache.lucene.testframework
    testImplementation libs.fasterxml.jackson.core.databind

    swaggerBuild libs.swagger3.jaxrs2.jakarta
}

// Non-Java client generation tasks below:
task buildJSClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  generatorName.set("javascript")
  inputSpec.set("$openApiSpecFile")
  outputDir.set("$jsClientDir")
  packageName.set("solr")
  generateApiTests.set(false)
  generateModelTests.set(false)
}

task buildPythonClient(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  generatorName.set("python")
  inputSpec.set("$openApiSpecFile")
  outputDir.set("$pythonClientDir")
  packageName.set("solr")
  generateApiTests.set(false)
  generateModelTests.set(false)
}

tasks.withType(org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  dependsOn(resolve)
}

artifacts {
    // Ensure the OAS is available to other modules who want to generate code (i.e. solrj)
    openapiSpec file(openApiSpecFile), {
        builtBy resolve
    }

    // Makes generated clients available to other build modules
    jsClient file(project.jsClientDir), {
        builtBy buildJSClient
    }
    pythonClient file(project.pythonClientDir), {
        builtBy buildPythonClient
    }
}
