FROM node:20-alpine AS frontend-build
WORKDIR /app/frontend
COPY src/frontend/package*.json ./
RUN npm ci
COPY src/frontend/ ./
RUN npm run build

FROM maven:3.9-eclipse-temurin-23 AS backend-build
WORKDIR /app

ARG APP_VERSION=0.0.1-SNAPSHOT

COPY pom.xml ./
COPY src ./src
COPY --from=frontend-build /app/frontend/dist ./src/main/resources/static
RUN mvn versions:set -DnewVersion=${APP_VERSION} -DgenerateBackupPoms=false && \
    mvn clean package -DskipTests

FROM eclipse-temurin:23-jre-alpine
WORKDIR /app

ARG APP_VERSION=0.0.1-SNAPSHOT
ENV APP_VERSION=${APP_VERSION}
LABEL version="${APP_VERSION}"

COPY --from=backend-build /app/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]