FROM maven:3.9.9-eclipse-temurin-21 AS build
WORKDIR /backend

# Maven Resolver treats 429/503 responses as transient, but the default retry
# budget is short for a concurrent multi-architecture build.  Keep the retry
# policy in the build stage so a temporary Maven Central rate limit does not
# fail the image, without affecting the runtime container.
ENV MAVEN_ARGS="-Daether.connector.http.retryHandler.count=6 \
-Daether.connector.http.retryHandler.interval=10000 \
-Daether.connector.http.retryHandler.intervalMax=300000 \
-Daether.connector.http.retryHandler.serviceUnavailable=429,503"

COPY console/backend/pom.xml ./
COPY console/backend/commons/pom.xml commons/pom.xml
COPY console/backend/hub/pom.xml hub/pom.xml
COPY console/backend/toolkit/pom.xml toolkit/pom.xml

RUN mvn -pl hub -am dependency:go-offline

COPY console/backend/ .
RUN mvn -DskipTests package -pl hub -am

FROM eclipse-temurin:21-jre
WORKDIR /app

RUN apt-get update && \
    apt-get install -y --no-install-recommends curl locales tzdata && \
    locale-gen zh_CN.UTF-8 && \
    ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

ENV TZ=Asia/Shanghai
ENV LANG=zh_CN.UTF-8
ENV LANGUAGE=zh_CN:zh
ENV LC_ALL=zh_CN.UTF-8

COPY --from=build /backend/hub/target/hub-server.jar /app/app.jar
EXPOSE 8080

# Optimized JVM parameters for reduced memory usage:
ENTRYPOINT ["java", \
    "-XX:+UseContainerSupport", \
    "-XX:MaxRAMPercentage=75.0", \
    "-XX:+UseG1GC", \
    "-XX:+UseStringDeduplication", \
    "-jar", "/app/app.jar"]
