# Stage 1: compile the re-frame UI (ui/) to JS. shadow-cljs needs Node and a JVM. It writes
# to ../resources/public/js (see ui/shadow-cljs.edn), i.e. /build/resources/public/js here.
# trixie, not bookworm: its default-jre is Java 21, which shadow-cljs's Closure Compiler
# requires (bookworm's is 17 -> UnsupportedClassVersionError).
FROM node:22-trixie-slim AS ui

RUN apt-get update \
    && apt-get install -y --no-install-recommends default-jre-headless \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build/ui
COPY ui/package.json ui/package-lock.json ./
RUN npm ci
# Resolve the ClojureScript deps in their own layer so source edits don't re-download them.
COPY ui/shadow-cljs.edn ./
RUN npx shadow-cljs classpath > /dev/null
COPY ui/src ./src
RUN npx shadow-cljs release app

# Stage 2: the server.
FROM clojure:openjdk-11-tools-deps

RUN sed -i \
    -e 's|deb.debian.org/debian|archive.debian.org/debian|g' \
    -e '/security.debian.org/d' \
    /etc/apt/sources.list \
    && apt-get update && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Aliases whose deps get pre-fetched into the image; the coverage compose override adds `cov`.
ARG DEPS_ALIASES=run

COPY deps.edn ./
RUN clojure -P -X:${DEPS_ALIASES} \
    && echo "[build] cached jars: $(find /root/.m2 -name '*.jar' | wc -l)"

COPY . .
COPY --from=ui /build/resources/public/js resources/public/js

EXPOSE 3000

CMD ["clojure", "-X:run"]
