<?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>xyz.tcheeric</groupId>
        <artifactId>cashu-ledger</artifactId>
        <version>0.7.1</version>
    </parent>

    <artifactId>cashu-ledger-web</artifactId>
    <name>Cashu Ledger Web</name>
    <description>Spring Boot web application for cashu-ledger</description>

    <properties>
        <!-- Set -DskipJsTests=true to skip the node-based JS unit suite. -->
        <skipJsTests>false</skipJsTests>
    </properties>

    <dependencies>
        <dependency>
            <groupId>xyz.tcheeric</groupId>
            <artifactId>cashu-ledger-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- Trace schema/ports used directly by the web read API (also transitive via core) -->
        <dependency>
            <groupId>xyz.tcheeric</groupId>
            <artifactId>cashu-ledger-trace-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Prometheus scrape endpoint for trace metrics -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <!-- OpenAPI/Swagger UI for the REST API -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>${springdoc.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- nostrdb-jni for persistent event caching (optional in core, explicit here) -->
        <dependency>
            <groupId>xyz.tcheeric</groupId>
            <artifactId>nostrdb-jni</artifactId>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- MockHttpServletRequest, for testing servlet filters without a container. -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!--
              Boots the real application context, so the security filter chain can be tested as
              it is actually configured. The chain previously said anyRequest().permitAll() with
              every access decision inside a handler (issue #8); spring-test alone cannot check
              that, because there was no context to assemble the chain in.
            -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
            </plugin>
            <!-- Runs the browser-side JS unit suite (crypto envelope + nsec handling)
                 under Node's built-in test runner during the test phase. -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>js-unit-tests</id>
                        <phase>test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>node</executable>
                            <!-- Run FROM the test directory with no path argument.
                                 Passing a directory to node's test runner scans it on
                                 Node 20, but on Node 22 the path is resolved as a module
                                 and the run dies with MODULE_NOT_FOUND — so this passed
                                 locally and failed on a runner that had moved to 22.

                                 A glob argument is not the fix either: exec-maven-plugin
                                 does not go through a shell, so the pattern is passed
                                 literally and node reports "Could not find '...*.test.mjs'".
                                 Discovery from the working directory is what both Node
                                 versions agree on, and it keeps picking up new test files
                                 the way naming them one by one would not. -->
                            <workingDirectory>${project.basedir}/src/test/js</workingDirectory>
                            <arguments>
                                <argument>--test</argument>
                            </arguments>
                            <skip>${skipJsTests}</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <configuration>
                    <from>
                        <image>eclipse-temurin:21-jre</image>
                    </from>
                    <to>
                        <image>docker.398ja.xyz/cashu-ledger-web</image>
                        <tags>
                            <tag>${project.version}</tag>
                            <tag>latest</tag>
                        </tags>
                    </to>
                    <container>
                        <ports>
                            <port>6060</port>
                        </ports>
                        <jvmFlags>
                            <jvmFlag>-XX:+UseContainerSupport</jvmFlag>
                            <jvmFlag>-XX:InitialRAMPercentage=50.0</jvmFlag>
                            <jvmFlag>-XX:MaxRAMPercentage=75.0</jvmFlag>
                            <jvmFlag>-XX:+ExitOnOutOfMemoryError</jvmFlag>
                        </jvmFlags>
                        <environment>
                            <LEDGER_WEB_STORAGE_PATH>/app/data/ndb</LEDGER_WEB_STORAGE_PATH>
                        </environment>
                        <volumes>
                            <volume>/app/data</volume>
                        </volumes>
                        <workingDirectory>/app</workingDirectory>
                        <creationTime>USE_CURRENT_TIMESTAMP</creationTime>
                    </container>
                    <extraDirectories>
                        <paths>
                            <path>
                                <from>src/main/jib</from>
                                <into>/app</into>
                            </path>
                        </paths>
                        <permissions>
                            <permission>
                                <file>/app/data</file>
                                <mode>755</mode>
                            </permission>
                        </permissions>
                    </extraDirectories>
                </configuration>
                <executions>
                    <execution>
                        <phase>deploy</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
