FROM eclipse-temurin:25-jdk-alpine AS build
RUN apk add --no-cache maven
WORKDIR /app

# Copy pom.xml and download dependencies (for caching)
COPY pom.xml .
RUN mvn dependency:go-offline

# Copy source and templates
COPY src ./src

# Build and run tests with overridden properties
ARG PUZZLE_ROWS=8
ARG PUZZLE_COLS=9
RUN mvn clean test package -DPUZZLE_ROWS=${PUZZLE_ROWS} -DPUZZLE_COLS=${PUZZLE_COLS}

FROM eclipse-temurin:25-jdk-alpine

RUN apk add --no-cache curl tzdata

# Install supercronic for cron scheduling
RUN curl -fsSL -o /usr/local/bin/supercronic \
  https://github.com/aptible/supercronic/releases/download/v0.2.30/supercronic-linux-amd64 \
 && chmod +x /usr/local/bin/supercronic

WORKDIR /app

# Copy the built jar and other necessary files from the build stage
COPY --from=build /app/target/tools-all.jar /app/target/tools-all.jar
COPY nl_score_hints.csv /app/nl_score_hints.csv
COPY docker-entrypoint.sh /app/docker-entrypoint.sh
COPY crontab /app/crontab

# Create output directory
RUN mkdir -p /data/puzzles

ENV TZ=Europe/Amsterdam
ENV OUT_DIR=/data/puzzles
ENV PUZZLES_PER_DAY=3
ENV THEME_FILTER=true
ENV THEME_MIN_SCORE=0.6

VOLUME ["/data"]

RUN chmod +x /app/docker-entrypoint.sh

ENTRYPOINT ["/app/docker-entrypoint.sh"]
