# ---- STAGE 1: Build ----
# Use an OpenJDK image that matches the version you develop with.
# It contains the JDK, but not Maven.
FROM eclipse-temurin:25-jdk-jammy AS builder

# Set the working directory
WORKDIR /app

# ---- Caching Dependencies ----
# First, copy the files that define the build, including the Maven Wrapper
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .

# Make the wrapper executable
RUN chmod +x ./mvnw

# Run a Maven command to download dependencies.
# Since pom.xml and wrapper files rarely change, this layer will be cached by Docker,
# speeding up subsequent builds significantly.
RUN ./mvnw dependency:go-offline

# ---- Building the Application ----
# Now, copy the source code. If only source code changes, the layers above are cached.
COPY src ./src

# Build the application JAR using the Maven Wrapper
RUN ./mvnw clean package -DskipTests -Djooq.codegen.skip=true -Dflyway.skip=true


# ---- STAGE 2: Runtime ----
# Use a lean Eclipse Temurin JRE image for a small and secure final container.
FROM eclipse-temurin:25-jre-jammy

# Set the working directory
WORKDIR /app

# Copy only the built JAR file from the 'builder' stage
COPY --from=builder /app/target/*.jar app.jar

# Expose the application port
EXPOSE 8080

# Command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
