<?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.hyperfoil.tools</groupId>
        <artifactId>horreum</artifactId>
        <version>0.19.2</version>
    </parent>

    <artifactId>horreum-api</artifactId>
    <name>Horreum API</name>

    <properties>
        <!-- Due to Jenkins plugin we will keep api and client on Java 11 -->
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.release>11</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <inputSpec.original>${project.build.directory}/generated/openapi-internal.yaml</inputSpec.original>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-rest-jackson</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-resteasy-client</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-hibernate-validator</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-smallrye-openapi</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
           <!-- Workaround for https://github.com/OpenAPITools/openapi-generator/issues/15202
                the issue is affecting windows only build -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${version.maven.helper}</version>
                <executions>
                    <execution>
                        <id>fix-path-for-swagger</id>
                        <goals>
                            <goal>regex-property</goal>
                        </goals>
                        <configuration>
                            <failIfNoMatch>false</failIfNoMatch>
                            <name>inputSpec-fixed</name>
                            <value>${inputSpec.original}</value>
                            <regex>\\</regex>
                            <replacement>/</replacement>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>${version.maven.jar}</version>
            </plugin>
            <plugin>
                <artifactId>smallrye-open-api-maven-plugin</artifactId>
                <groupId>io.smallrye</groupId>
                <version>4.2.0</version>
                <executions>
                    <!--Generate External Schema-->
                    <execution>
                        <id>external</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>generate-schema</goal>
                        </goals>
                        <configuration>
                            <configProperties>${basedir}/src/main/resources/application.properties</configProperties>
                            <infoVersion>${major-version}</infoVersion>
                            <scanPackages>
                                io.hyperfoil.tools.horreum.api,
                                io.hyperfoil.tools.horreum.api.alerting,
                                io.hyperfoil.tools.horreum.api.data,
                                io.hyperfoil.tools.horreum.api.grafana,
                                io.hyperfoil.tools.horreum.api.report
                            </scanPackages>
                            <scanExcludePackages>
                                io.hyperfoil.tools.horreum.api.internal
                            </scanExcludePackages>
                            <scanProfiles>
                                external
                            </scanProfiles>

                            <operationIdStrategy>METHOD</operationIdStrategy>
                            <schemaFilename>openapi</schemaFilename>
                        </configuration>
                    </execution>

                    <!-- Generate Internal Schema -->
                    <execution>
                        <id>internal</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>generate-schema</goal>
                        </goals>
                        <configuration>
                            <configProperties>${basedir}/src/main/resources/application.properties</configProperties>
                            <infoVersion>${major-version}</infoVersion>
                            <scanPackages>
                                io.hyperfoil.tools.horreum.api,
                                io.hyperfoil.tools.horreum.api.alerting,
                                io.hyperfoil.tools.horreum.api.data,
                                io.hyperfoil.tools.horreum.api.grafana,
                                io.hyperfoil.tools.horreum.api.report
                            </scanPackages>
                            <scanClasses>.*</scanClasses>
                            <operationIdStrategy>METHOD</operationIdStrategy>
                            <schemaFilename>openapi-internal</schemaFilename>
                        </configuration>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.16.0</version>
                <executions>
                    <execution>
                        <id>typescript</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!-- Workaround for https://github.com/OpenAPITools/openapi-generator/issues/15202 -->
                            <inputSpec>${inputSpec-fixed}</inputSpec>
                            <output>${project.basedir}/../horreum-web/src/generated</output>
                            <generatorName>typescript-fetch</generatorName>
                            <skipValidateSpec>false</skipValidateSpec>
                            <typeMappings>Array=any</typeMappings>
                            <globalProperties>
                                <skipFormModel>false</skipFormModel>
                            </globalProperties>
                            <modelNameMappings>
                                <modelNameMapping>Tag=Label</modelNameMapping>
                            </modelNameMappings>
                            <configOptions>
                                <supportsES6>true</supportsES6>
                                <useSingleRequestParameter>false</useSingleRequestParameter>
                                <typescriptThreePlus>true</typescriptThreePlus>
                                <prefixParameterInterfaces>true</prefixParameterInterfaces>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>../docs/site/content/en/openapi</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>target/generated/</directory>
                                    <includes>
                                        <include>openapi.yaml</include>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>write-project-properties</goal>
                        </goals>
                        <configuration>
                            <outputFile>
                                ${project.build.outputDirectory}/build.properties
                            </outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
