# ---- Builder Stage ----
# Use an official Leiningen image
FROM clojure:lein-2.11.2 AS builder

WORKDIR /app

# Copy the project definition first
COPY project.clj .

# Fetch dependencies. This creates a cached layer.
RUN lein deps

# Copy the rest of the source code
COPY . .

# Create the standalone "uberjar"
RUN lein uberjar

# ---- Final Stage ----
# Use a minimal JRE image for the runtime
FROM eclipse-temurin:17-jre-slim

WORKDIR /app

# Create a non-root user
RUN addgroup --system app && adduser --system --group app

# Copy the uberjar from the builder stage
# The path depends on your project.clj `target-path` and `uberjar-name`
COPY --from=builder /app/target/uberjar/*-standalone.jar ./app.jar

# Set ownership and user
RUN chown app:app ./app.jar
USER app

# Expose the application port
EXPOSE 3000

# Run the application
CMD ["java", "-jar", "app.jar"]
