<?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>org.vaadin.stefan</groupId>
        <artifactId>fullcalendar-parent</artifactId>
        <version>7.2.2-SNAPSHOT</version>
    </parent>

    <artifactId>fullcalendar-e2e-test-app</artifactId>
    <version>25.0.0-SNAPSHOT</version>
    <name>Full Calendar E2E Test App</name>
    <description>Dedicated Spring Boot app serving test views for Playwright E2E tests</description>
    <packaging>jar</packaging>

    <properties>
        <vaadin.version>25.1-SNAPSHOT</vaadin.version>
        <spring-boot.version>4.0.4</spring-boot.version>
        <fullcalendar.version>7.2.2-SNAPSHOT</fullcalendar.version>

        <plugin.version.maven-compiler>3.14.1</plugin.version.maven-compiler>
        <plugin.version.maven-surefire>3.5.4</plugin.version.maven-surefire>
        <plugin.version.maven-failsafe>3.5.4</plugin.version.maven-failsafe>

        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-dev</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.vaadin</groupId>
                    <artifactId>copilot</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.vaadin.stefan</groupId>
            <artifactId>fullcalendar2</artifactId>
            <version>${fullcalendar.version}</version>
        </dependency>

        <dependency>
            <groupId>org.vaadin.stefan</groupId>
            <artifactId>fullcalendar2-scheduler</artifactId>
            <version>${fullcalendar.version}</version>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <wait>500</wait>
                    <maxAttempts>240</maxAttempts>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${plugin.version.maven-compiler}</version>
            </plugin>

            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.version}</version>
                <configuration>
                    <frontendHotdeploy>true</frontendHotdeploy>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-frontend</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Integration tests with Playwright - activated using -Pit -->
            <id>it</id>
            <properties>
                <e2e.tests.dir>${project.basedir}/../e2e-tests</e2e.tests.dir>
            </properties>
            <build>
                <plugins>
                    <!-- Ensure production build for IT -->
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>vaadin-maven-plugin</artifactId>
                        <version>${vaadin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-frontend</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Install npm dependencies and run Playwright e2e tests -->
                    <!-- Uses system Node.js (devcontainer: FROM node:20, CI: actions/setup-node) -->
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>3.5.0</version>
                        <executions>
                            <execution>
                                <id>npm-install</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>npm</executable>
                                    <workingDirectory>${e2e.tests.dir}</workingDirectory>
                                    <arguments>
                                        <argument>install</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>install-playwright-browsers</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>npx</executable>
                                    <workingDirectory>${e2e.tests.dir}</workingDirectory>
                                    <arguments>
                                        <argument>playwright</argument>
                                        <argument>install</argument>
                                        <argument>chromium</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                            <execution>
                                <id>run-playwright-tests</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>npx</executable>
                                    <workingDirectory>${e2e.tests.dir}</workingDirectory>
                                    <arguments>
                                        <argument>playwright</argument>
                                        <argument>test</argument>
                                    </arguments>
                                    <environmentVariables>
                                        <BASE_URL>http://localhost:8082</BASE_URL>
                                        <CI>true</CI>
                                    </environmentVariables>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Start Spring Boot server before integration tests -->
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <executions>
                            <execution>
                                <id>start-app</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>start</goal>
                                </goals>
                                <configuration>
                                    <wait>500</wait>
                                    <maxAttempts>240</maxAttempts>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-app</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>stop</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <!-- Configure failsafe for integration test results -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>${plugin.version.maven-failsafe}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <!-- Production mode is activated using -Pproduction -->
            <id>production</id>
            <dependencies>
                <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>vaadin-core</artifactId>
                    <exclusions>
                        <exclusion>
                            <groupId>com.vaadin</groupId>
                            <artifactId>vaadin-dev</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>vaadin-maven-plugin</artifactId>
                        <version>${vaadin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-frontend</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
