# ---------- Build stage ----------
FROM --platform=$BUILDPLATFORM maven:3.9-eclipse-temurin-11 AS build
WORKDIR /workspace

# Cache-friendly: copy POMs first
COPY pom.xml .
COPY play-admin/pom.xml play-admin/
COPY play-common/pom.xml play-common/
COPY play-generator/pom.xml play-generator/

# Warm deps cache
RUN --mount=type=cache,target=/root/.m2 mvn -B -DskipTests dependency:go-offline

# Add sources
COPY play-admin/src play-admin/src/
COPY play-common/src play-common/src/
COPY play-generator/src play-generator/src/

# Build only app module (and its deps)
RUN --mount=type=cache,target=/root/.m2 mvn -pl play-admin -am -B -DskipTests -T 1C clean package

# ---------- Runtime stage (multi-arch) ----------
FROM eclipse-temurin:11-jre AS runtime
# non-root
RUN groupadd -g 1001 appgroup && useradd -u 1001 -g appgroup -m -s /usr/sbin/nologin appuser
WORKDIR /app

COPY --from=build /workspace/play-admin/target/*.jar /app/app.jar
RUN chown -R appuser:appgroup /app
USER appuser

ENV JAVA_OPTS="-Xms2g -Xmx2g" \
    SPRING_PROFILES_ACTIVE="test" \
    TZ="Asia/Shanghai" \
    LANG="C.UTF-8"

EXPOSE 7002
ENTRYPOINT ["sh","-c","exec java $JAVA_OPTS -jar /app/app.jar --spring.profiles.active=${SPRING_PROFILES_ACTIVE}"]
