# SummerCMS Docker Build
# Multi-stage build: JDK for building, JRE for runtime

# Stage 1: Build
FROM eclipse-temurin:21-jdk AS builder

# Install Mill build tool
# Using ./mill launcher pattern - download specific version
WORKDIR /app

# Copy mill launcher and version first for caching
COPY .mill-version ./
COPY mill ./

# Copy build files
COPY build.mill ./

# Copy source code
COPY summercms ./summercms

# Build fat JAR using local mill launcher
RUN chmod +x ./mill && ./mill summercms.assembly

# Stage 2: Runtime
FROM eclipse-temurin:21-jre

WORKDIR /app

# Install curl for health check
RUN apt-get update && \
    apt-get install -y --no-install-recommends curl && \
    rm -rf /var/lib/apt/lists/*

# Copy the fat JAR from builder
COPY --from=builder /app/out/summercms/assembly.dest/out.jar /app/summercms.jar

# Health check using /health endpoint
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

EXPOSE 8080

# Environment variables for configuration override
# These can be overridden at runtime with -e flags
ENV DB_HOST=localhost
ENV DB_PORT=5432
ENV DB_NAME=summercms
ENV DB_USER=summercms

# Run the application
ENTRYPOINT ["java", "-jar", "/app/summercms.jar"]
