<?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>

    <groupId>io.indextables</groupId>
    <artifactId>tantivy4java</artifactId>
    <version>0.32.7</version>
    <packaging>jar</packaging>

    <name>Tantivy4Java Experimental</name>
    <description>Java bindings for Tantivy and Quickwit with experimental L2 disk cache</description>
    <url>https://github.com/indextables/tantivy4java</url>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <id>tantivy4java</id>
            <name>Tantivy4Java Contributors</name>
            <url>https://github.com/indextables/tantivy4java</url>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:git://github.com/indextables/tantivy4java.git</connection>
        <developerConnection>scm:git:ssh://github.com:indextables/tantivy4java.git</developerConnection>
        <url>https://github.com/indextables/tantivy4java/tree/main</url>
    </scm>

    <distributionManagement>
        <repository>
            <id>central</id>
            <url>https://central.sonatype.com/api/v1/publisher</url>
        </repository>
    </distributionManagement>

    <properties>
        <!-- <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>
        <junit.version>5.10.0</junit.version>
        <rust.target.dir>${project.build.directory}/rust</rust.target.dir>
        <!-- Empty by default; the jdk17plus profile sets this for Java 17+ -->
        <surefire.argLine></surefire.argLine>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>
        
        <!-- AWS SDK for S3 testing -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>2.21.29</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Azure Storage Blob -->
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.25.0</version>
            <scope>test</scope>
        </dependency>

        <!-- Azure Identity for OAuth token authentication -->
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.12.0</version>
            <scope>test</scope>
        </dependency>

        <!-- Azure Core - explicit version to ensure compatibility -->
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-core</artifactId>
            <version>1.48.0</version>
            <scope>test</scope>
        </dependency>

        <!-- Testcontainers for Azurite emulator -->
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>1.19.0</version>
            <scope>test</scope>
        </dependency>

        <!-- GCP Cloud Storage -->
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-storage</artifactId>
            <version>2.29.1</version>
            <scope>test</scope>
        </dependency>
        
        <!-- GCP NIO for LocalStorageHelper (pure Java, no Docker) -->
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-nio</artifactId>
            <version>0.127.12</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Mockito for mocking Azure and other services -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Apache Iceberg Java SDK for creating test tables in REST catalog -->
        <dependency>
            <groupId>org.apache.iceberg</groupId>
            <artifactId>iceberg-core</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.iceberg</groupId>
            <artifactId>iceberg-api</artifactId>
            <version>1.7.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Adobe S3Mock for S3 testing -->
        <dependency>
            <groupId>io.findify</groupId>
            <artifactId>s3mock_2.13</artifactId>
            <version>0.2.6</version>
            <scope>test</scope>
        </dependency>
        
        <!-- Scala library (required by S3Mock) -->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.13.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <!-- Enable debug symbols for Java compilation -->
                    <debug>true</debug>
                    <debuglevel>lines,vars,source</debuglevel>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <!-- Ensure sequential test class execution to prevent S3Mock port conflicts -->
                    <parallel>none</parallel>
                    <perCoreThreadCount>false</perCoreThreadCount>
                    <threadCount>1</threadCount>
                    <!-- Fork a new JVM for each test suite (test class) for complete isolation -->
                    <reuseForks>false</reuseForks>
                    <forkCount>1</forkCount>
                    <!-- Set test execution order to be predictable -->
                    <runOrder>filesystem</runOrder>
                    <!-- Exclude cloud/Docker integration tests by default.
                         Run with: mvn test -Pintegration-tests -->
                    <excludes>
                        <exclude>**/Real*Test.java</exclude>
                        <exclude>**/AzureIntegrationTest.java</exclude>
                        <exclude>**/AzureOAuthTokenTest.java</exclude>
                    </excludes>
                    <!-- Enable native access for JNI operations (Java 17+ only; see jdk17plus profile) -->
                    <argLine>${surefire.argLine}</argLine>
                </configuration>
            </plugin>

            <!-- Native library build plugin -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <!-- Run Rust unit tests before building (skip doctests) -->
                    <!-- Build release binary -->
                    <execution>
                        <id>build-rust</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>cargo</executable>
                            <arguments>
                                <argument>build</argument>
                                <argument>--release</argument>
                                <argument>--target-dir</argument>
                                <argument>${rust.target.dir}</argument>
                            </arguments>
                            <workingDirectory>${project.basedir}/native</workingDirectory>
                            <environmentVariables>
                                <RUST_BACKTRACE>1</RUST_BACKTRACE>
                            </environmentVariables>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Copy native library to resources -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.3.1</version>
                <executions>
                    <execution>
                        <id>copy-native-lib</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.outputDirectory}/native</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${rust.target.dir}/release</directory>
                                    <includes>
                                        <include>libtantivy4java.so</include>
                                        <include>libtantivy4java.dylib</include>
                                        <include>tantivy4java.dll</include>
                                        <include>tantivy4java-merge</include>
                                        <include>tantivy4java-merge.exe</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- JAR packaging - creates both main JAR and platform-specific JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <!-- Create platform-specific JAR with classifier -->
                    <execution>
                        <id>platform-jar</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>${platform.classifier}</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- Build profiles -->
    <profiles>
        <!-- Platform detection profiles - auto-activate based on OS -->
        <!-- macOS ARM64 (Apple Silicon) -->
        <profile>
            <id>platform-darwin-aarch64</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>aarch64</arch>
                </os>
                <property>
                    <name>!platform.classifier</name>
                </property>
            </activation>
            <properties>
                <platform.classifier>darwin-aarch64</platform.classifier>
                <rust.target>aarch64-apple-darwin</rust.target>
            </properties>
        </profile>

        <!-- macOS x86_64 (Intel) -->
        <profile>
            <id>platform-darwin-x86_64</id>
            <activation>
                <os>
                    <family>mac</family>
                    <arch>x86_64</arch>
                </os>
                <property>
                    <name>!platform.classifier</name>
                </property>
            </activation>
            <properties>
                <platform.classifier>darwin-x86_64</platform.classifier>
                <rust.target>x86_64-apple-darwin</rust.target>
            </properties>
        </profile>

        <!-- Linux x86_64 -->
        <profile>
            <id>platform-linux-x86_64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>linux</name>
                    <arch>amd64</arch>
                </os>
                <property>
                    <name>!platform.classifier</name>
                </property>
            </activation>
            <properties>
                <platform.classifier>linux-x86_64</platform.classifier>
                <rust.target>x86_64-unknown-linux-gnu</rust.target>
            </properties>
        </profile>

        <!-- Linux ARM64 -->
        <profile>
            <id>platform-linux-aarch64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>linux</name>
                    <arch>aarch64</arch>
                </os>
                <property>
                    <name>!platform.classifier</name>
                </property>
            </activation>
            <properties>
                <platform.classifier>linux-aarch64</platform.classifier>
                <rust.target>aarch64-unknown-linux-gnu</rust.target>
            </properties>
        </profile>

        <!-- Windows x86_64 -->
        <profile>
            <id>platform-windows-x86_64</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>amd64</arch>
                </os>
                <property>
                    <name>!platform.classifier</name>
                </property>
            </activation>
            <properties>
                <platform.classifier>windows-x86_64</platform.classifier>
                <rust.target>x86_64-pc-windows-msvc</rust.target>
            </properties>
        </profile>

        <!-- Linux optimized build profile - automatically activates on Linux -->
        <profile>
            <id>linux-optimized</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>linux</name>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>build-rust</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>cargo</executable>
                                    <arguments>
                                        <argument>build</argument>
                                        <argument>--release</argument>
                                        <argument>--target-dir</argument>
                                        <argument>${rust.target.dir}</argument>
                                    </arguments>
                                    <workingDirectory>${project.basedir}/native</workingDirectory>
                                    <environmentVariables>
                                        <!-- Optimize for x86_64 Linux -->
                                        <RUSTFLAGS>-C target-cpu=x86-64</RUSTFLAGS>
                                        <RUST_BACKTRACE>1</RUST_BACKTRACE>
                                    </environmentVariables>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        
        <!-- Debug build profile - activate with -Pdebug -->
        <profile>
            <id>debug</id>
            <properties>
                <rust.target.dir>target</rust.target.dir>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>build-rust-debug</id>
                                <phase>compile</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>cargo</executable>
                                    <arguments>
                                        <argument>build</argument>
                                        <!-- Use debug build instead of release -->
                                        <argument>--target-dir</argument>
                                        <argument>${rust.target.dir}</argument>
                                    </arguments>
                                    <workingDirectory>${project.basedir}/native</workingDirectory>
                                    <environmentVariables>
                                        <!-- Enable maximum debug information for panic tracking -->
                                        <RUST_BACKTRACE>full</RUST_BACKTRACE>
                                        <TANTIVY4JAVA_DEBUG>1</TANTIVY4JAVA_DEBUG>
                                        <!-- Maximum debug info, no optimization, debug assertions -->
                                        <RUSTFLAGS>-C debuginfo=2 -C opt-level=0 -C debug-assertions=on -C overflow-checks=on -C panic=unwind</RUSTFLAGS>
                                    </environmentVariables>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- Maven Central release profile - activate with -Prelease -->
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Generate source JAR -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>3.3.0</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Generate Javadoc JAR -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>3.6.3</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <source>11</source>
                            <detectJavaApiLink>false</detectJavaApiLink>
                            <doclint>none</doclint>
                        </configuration>
                    </plugin>

                    <!-- GPG signing -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>3.1.0</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                                <configuration>
                                    <!-- Prevent gpg from using pinentry programs -->
                                    <gpgArguments>
                                        <arg>--pinentry-mode</arg>
                                        <arg>loopback</arg>
                                    </gpgArguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Central Publishing Plugin -->
                    <plugin>
                        <groupId>org.sonatype.central</groupId>
                        <artifactId>central-publishing-maven-plugin</artifactId>
                        <version>0.4.0</version>
                        <extensions>true</extensions>
                        <configuration>
                            <publishingServerId>central</publishingServerId>
                            <tokenAuth>true</tokenAuth>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- Integration tests profile - includes cloud/Docker tests excluded by default.
             Activate with: mvn test -Pintegration-tests -->
        <profile>
            <id>integration-tests</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes combine.self="override" />
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- Enable native access flag only on Java 17+ (flag does not exist on Java 11) -->
        <profile>
            <id>jdk17plus-native-access</id>
            <activation>
                <jdk>[17,)</jdk>
            </activation>
            <properties>
                <surefire.argLine>--enable-native-access=ALL-UNNAMED</surefire.argLine>
            </properties>
        </profile>
    </profiles>
</project>
