<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>io.apicurio</groupId>
    <artifactId>apicurio-registry</artifactId>
    <version>3.1.7</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>apicurio-registry-v2-java-sdk</artifactId>

  <name>apicurio-registry-v2-java-sdk</name>

  <properties>
    <projectRoot>${project.basedir}/..</projectRoot>
  </properties>
  <dependencies>
    <dependency>
      <groupId>io.apicurio</groupId>
      <artifactId>apicurio-registry-java-sdk-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.kiota</groupId>
      <artifactId>microsoft-kiota-abstractions</artifactId>
      <version>${kiota.libs.version}</version>
    </dependency>
    <!-- HTTP Adapters - both optional, at least one must be provided by consumer -->
    <dependency>
      <groupId>io.kiota</groupId>
      <artifactId>kiota-http-jdk</artifactId>
      <version>${kiota.community.version}</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>io.kiota</groupId>
      <artifactId>kiota-http-vertx</artifactId>
      <version>${kiota.community.version}</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>io.kiota</groupId>
      <artifactId>kiota-serialization-jackson</artifactId>
      <version>${kiota.community.version}</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.kiota</groupId>
      <artifactId>microsoft-kiota-serialization-text</artifactId>
      <version>${kiota.libs.version}</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.kiota</groupId>
      <artifactId>microsoft-kiota-serialization-form</artifactId>
      <version>${kiota.libs.version}</version>
    </dependency>
    <dependency>
      <groupId>com.microsoft.kiota</groupId>
      <artifactId>microsoft-kiota-serialization-multipart</artifactId>
      <version>${kiota.libs.version}</version>
    </dependency>
    <dependency>
      <groupId>jakarta.annotation</groupId>
      <artifactId>jakarta.annotation-api</artifactId>
    </dependency>
    <!-- Vert.x OAuth2 - optional, only needed when using Vert.x adapter with OAuth2 -->
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-auth-oauth2</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <!-- GMavenPlus plugin for Groovy script execution -->
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>${version.gmavenplus-plugin}</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>${groovy.version}</version>
            <scope>runtime</scope>
          </dependency>
          <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-datatype-json-org.version}</version>
          </dependency>
        </dependencies>
        <executions>
          <!-- Filter OpenAPI spec before Kiota code generation -->
          <execution>
            <id>filter-openapi-spec</id>
            <phase>initialize</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <scripts>
                <script><![CDATA[
                  import com.fasterxml.jackson.databind.ObjectMapper
                  import com.fasterxml.jackson.databind.SerializationFeature

                  def inputFile = new File(project.basedir, '../common/src/main/resources/META-INF/openapi-v2.json')
                  def outputFile = new File(project.build.directory, 'openapi-v2-filtered.json')

                  // Paths to process
                  def pathsToFilter = [
                      '/groups/{groupId}/artifacts',
                      '/groups/{groupId}/artifacts/{artifactId}',
                      '/groups/{groupId}/artifacts/{artifactId}/versions'
                  ] as Set

                  // Content types to remove
                  def contentTypesToRemove = ['*/*', 'application/create.extended+json'] as Set

                  log.info("Filtering OpenAPI spec from: ${inputFile}")
                  log.info("Output will be written to: ${outputFile}")

                  // Use Jackson to preserve order
                  def mapper = new ObjectMapper()
                  def json = mapper.readValue(inputFile, LinkedHashMap.class)

                  // Process only the specified paths
                  json.paths.each { pathKey, pathItem ->
                      if (pathsToFilter.contains(pathKey)) {
                          log.info("Processing path: ${pathKey}")

                          // Check if POST operation exists
                          if (pathItem.post?.requestBody?.content) {
                              def content = pathItem.post.requestBody.content

                              // Remove unwanted content types
                              contentTypesToRemove.each { contentType ->
                                  if (content.containsKey(contentType)) {
                                      content.remove(contentType)
                                      log.info("  Removed content-type: ${contentType}")
                                  }
                              }

                              def remainingKeys = content.keySet() as List
                              log.info("  Remaining content-types: ${remainingKeys}")
                          }
                      }
                  }

                  // Ensure output directory exists
                  outputFile.parentFile.mkdirs()

                  // Write with pretty printing while preserving order
                  mapper.enable(SerializationFeature.INDENT_OUTPUT)
                  mapper.writeValue(outputFile, json)

                  log.info("Filtered OpenAPI spec written successfully")
                ]]></script>
              </scripts>
            </configuration>
          </execution>
          <!-- Post-process Kiota-generated files to support legacy date formats -->
          <execution>
            <id>post-process-kiota</id>
            <phase>process-sources</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <scripts>
                <script><![CDATA[
                  // Post-process Kiota-generated files to support legacy date formats
                  // See: https://github.com/Apicurio/apicurio-registry/issues/6799

                  def generatedDir = new File(project.build.directory, 'generated-sources/kiota')
                  if (!generatedDir.exists()) {
                    log.info('Generated sources directory not found, skipping Kiota post-processing')
                    return
                  }

                  log.info('Post-processing Kiota-generated files in ' + generatedDir)

                  def oldPattern = 'n.getOffsetDateTimeValue()'
                  def newPattern = 'io.apicurio.registry.client.common.util.DateTimeUtil.getOffsetDateTimeValue(n)'

                  def modifiedCount = 0

                  generatedDir.eachFileRecurse { file ->
                    if (file.name.endsWith('.java')) {
                      def content = file.text

                      // Check if file contains the pattern to replace
                      if (content.contains(oldPattern)) {
                        // Replace the pattern with fully qualified method call
                        content = content.replace(oldPattern, newPattern)

                        // Write modified content back to file
                        file.text = content
                        modifiedCount++
                        log.info('  Modified: ' + file.name)
                      }
                    }
                  }

                  log.info('Post-processing complete. Modified ' + modifiedCount + ' files.')
                ]]></script>
              </scripts>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>io.kiota</groupId>
        <artifactId>kiota-maven-plugin</artifactId>
        <version>${kiota.community.version}</version>
        <executions>
          <execution>
            <id>v2</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <kiotaVersion>${kiota.version}</kiotaVersion>
              <kiotaTimeout>${kiota.timeout}</kiotaTimeout>
              <baseURL>${kiota.base.url}</baseURL>
              <file>${project.build.directory}/openapi-v2-filtered.json</file>
              <namespace>io.apicurio.registry.rest.client.v2</namespace>
              <clientClass>RegistryClient</clientClass>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>${version.build.helper.maven.plugin}</version>
        <executions>
          <execution>
            <id>add-source</id>
            <goals>
              <goal>add-source</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <sources>
                <source>${project.build.directory}/generated-sources/kiota/</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
