<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.netmoor.moorhaven</groupId>
        <artifactId>moorhaven-parent</artifactId>
        <version>0.3.3</version>
    </parent>

    <artifactId>moorhaven-app</artifactId>
    <name>Moorhaven :: App</name>

    <properties>
        <!-- Единственный модуль-сервис: общий конвейер собирает из него образ harbor.home.arpa/apps/moorhaven-app -->
        <jib.skip>false</jib.skip>
        <start-class>com.netmoor.moorhaven.MoorhavenApplication</start-class>
        <!-- Фронтенд собирается внутри Maven (frontend-maven-plugin) и встраивается в jar в ОБЕИХ
             композициях (standalone и cluster): один артефакт = API + UI, отдельного задания CI для
             фронта нет (платформа netmoor, ответ 7). -Dskip.frontend=true — для бэкенд-работы. -->
        <skip.frontend>false</skip.frontend>
        <frontend.dir>${project.basedir}/../frontend</frontend.dir>
        <!-- The SQLite driver ships only in the standalone jar (the profile flips
             this to compile); the cluster build still needs it on the compile
             classpath for profile-independent sources gated by @Profile("sqlite"). -->
        <sqlite-jdbc.scope>provided</sqlite-jdbc.scope>
        <!-- Coverage ratchet (#50): own-module line coverage at measurement time minus
             a 2pp safety margin, see doc/coverage-baseline.md. Ratchet-only: raise when
             coverage improves, never lower. -->
        <jacoco.min>0.23</jacoco.min>
    </properties>

    <dependencies>
        <!-- Internal modules -->
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-backup</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-notification</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-file-storage</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-media-library</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-photo-ai</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-streaming</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-cameras</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-ai-hailo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-torrent-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-ha-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-dlna</artifactId>
        </dependency>
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-plugin-host</artifactId>
        </dependency>

        <!-- Spring Boot starters -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Prometheus scrape endpoint at /actuator/prometheus (exposed via management config). -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- OTLP tracing export (disabled by default via management.tracing.enabled=false). -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-otel</artifactId>
        </dependency>
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-exporter-otlp</artifactId>
            <exclusions>
                <!-- okhttp + kotlin-stdlib + okio weigh 3 MB; the JDK HttpClient sender below does the same job. -->
                <exclusion>
                    <groupId>io.opentelemetry</groupId>
                    <artifactId>opentelemetry-exporter-sender-okhttp</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-exporter-sender-jdk</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-docker-compose</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Liquibase -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-liquibase</artifactId>
        </dependency>

        <!-- Spring Modulith -->
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.modulith</groupId>
            <artifactId>spring-modulith-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- MVC controller slices with production request binding and exception handling. -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webmvc-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Security/architecture rules with FreezingArchRule (SecurityArchitectureTests). -->
        <dependency>
            <groupId>com.tngtech.archunit</groupId>
            <artifactId>archunit-junit5</artifactId>
            <version>${archunit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- Real PostgreSQL for the migration-portability test (Docker-gated). -->
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers-postgresql</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- provided in the cluster build (compile classpath only), compile in
             standalone — see the sqlite-jdbc.scope property. -->
        <dependency>
            <groupId>com.netmoor.moorhaven</groupId>
            <artifactId>moorhaven-sqlite-slim</artifactId>
            <scope>${sqlite-jdbc.scope}</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Тесты приложения идут и при -Dskip.module.tests=true (cluster-verify), см. корневой pom. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <!-- Geodata bundle (root pom: moorhaven.geodata.*): downloaded once into the local Maven cache,
                 checked against its sha256, unpacked to classes/geodata so it ships in the jar.
                 BundledGeoData installs it into the data directory on startup. -->
            <plugin>
                <groupId>com.googlecode.maven-download-plugin</groupId>
                <artifactId>download-maven-plugin</artifactId>
                <version>1.13.0</version>
                <executions>
                    <execution>
                        <id>geodata</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>wget</goal>
                        </goals>
                        <configuration>
                            <uri>${moorhaven.geodata.url}</uri>
                            <sha256>${moorhaven.geodata.sha256}</sha256>
                            <unpack>true</unpack>
                            <outputDirectory>${project.build.outputDirectory}/geodata</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.15.4</version>
                <configuration>
                    <workingDirectory>${frontend.dir}</workingDirectory>
                    <installDirectory>${project.build.directory}/node</installDirectory>
                    <skip>${skip.frontend}</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>install-node-and-npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <!-- Та же мажорная версия, что у разработки и e2e (Node 24): на 22 падают Blob.stream/Intl в тестах. -->
                            <nodeVersion>v24.14.1</nodeVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm-ci</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>ci</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm-build</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                    <!-- Frontend gates in the 'test' phase, so the platform's single `mvn verify`
                         (ci-templates/maven-app) blocks on them and `-DskipTests` skips them like
                         surefire (the plugin skips its test-phase executions on skipTests).
                         `run build` above already type-checks (tsc -b). -->
                    <execution>
                        <id>npm-lint</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <arguments>run lint</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm-check-api</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <arguments>run check:api</arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>npm-test</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>test</phase>
                        <configuration>
                            <!-- Coverage ratchet per directory lives in vite.config.ts. -->
                            <arguments>run test:coverage</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-frontend-dist</id>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <phase>process-resources</phase>
                        <configuration>
                            <skip>${skip.frontend}</skip>
                            <outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${frontend.dir}/dist</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <!-- Build-time-only weight: Lombok (annotation processor, provided scope is
                             still packed into a boot jar) and ArchUnit (pulled in by Modulith; module
                             verification lives in ModularityTests). aspectjweaver + spring-aspects must
                             stay: @EnableJpaAuditing makes spring-data-jpa register
                             AnnotationBeanConfigurerAspect, and startup fails without them. -->
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                        <exclude>
                            <groupId>com.tngtech.archunit</groupId>
                            <artifactId>archunit</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- Standalone (default): in-process stores + Caffeine cache, no Hazelcast. -->
        <profile>
            <id>standalone</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- Ship the SQLite driver (first-run database choice). -->
                <sqlite-jdbc.scope>compile</sqlite-jdbc.scope>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.netmoor.moorhaven</groupId>
                    <artifactId>moorhaven-inprocess</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.hibernate.orm</groupId>
                    <artifactId>hibernate-community-dialects</artifactId>
                </dependency>
            </dependencies>
        </profile>

        <!-- Spring AOT (opt-in, add to the standalone build: -Pstandalone,aot). process-aot turns bean
             registration and JPA/Data metadata into generated code at build time, which is then used at
             runtime by starting with -Dspring.aot.enabled=true (package/jpackage.sh does that).
             The catch: AOT freezes every build-time decision - active profiles, @Conditional*,
             moorhaven.modules.*.enabled - into the jar. So an AOT jar is built per (platform, database)
             and ships only that combination; the default jar keeps the runtime database chooser and
             module switches working. The profiles below are the ones the jar is frozen with.
             process-aot also writes generated CGLIB classes into target/classes, which ModularityTests
             then reads as violations - run `mvn clean` before testing after an -Paot build. -->
        <profile>
            <id>aot</id>
            <properties>
                <aot.profiles>standalone,sqlite</aot.profiles>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>process-aot</id>
                                <goals>
                                    <goal>process-aot</goal>
                                </goals>
                                <configuration>
                                    <profiles>${aot.profiles}</profiles>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- Cluster / microservices: Hazelcast-backed stores, cache and events. -->
        <profile>
            <id>cluster</id>
            <dependencies>
                <dependency>
                    <groupId>com.netmoor.moorhaven</groupId>
                    <artifactId>moorhaven-hazelcast</artifactId>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <configuration>
                            <excludes combine.children="append">
                                <!-- Compile-only here (see the base provided dependency);
                                     the boot plugin would otherwise package provided too. -->
                                <exclude>
                                    <groupId>com.netmoor.moorhaven</groupId>
                                    <artifactId>moorhaven-sqlite-slim</artifactId>
                                </exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
