# Modified 2026-11-12: health checks and persistent uploads in release working directories.
# Build stage
ARG BUILDER_IMAGE="hexpm/elixir:1.17-erlang-26.2.5-debian-bookworm-20251208-slim"
ARG RUNNER_IMAGE="debian:bookworm-20251208-slim"

FROM ${BUILDER_IMAGE} as builder

# Install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git curl \
    && apt-get clean && rm -f /var/lib/apt/lists/*_*

# Prepare build dir
WORKDIR /app

# Install hex + rebar
RUN mix local.hex --force && \
    mix local.rebar --force

# Set build ENV
ENV MIX_ENV="prod"

# Install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config

# Copy compile-time config files
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile

COPY priv priv
COPY lib lib
COPY assets assets

# Compile assets
RUN mix assets.deploy

# Copy runtime config (needed by the release to read DATABASE_URL etc.)
COPY config/runtime.exs config/

# Compile the release
RUN mix compile

# Generate release
RUN mix phx.gen.release
RUN mix release

# Runner stage
FROM ${RUNNER_IMAGE}

RUN apt-get update -y && \
  apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates curl \
  && apt-get clean && rm -f /var/lib/apt/lists/*_*

# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR "/app"
RUN chown nobody /app

# Set runner ENV
ENV MIX_ENV="prod"
ENV PHX_SERVER="true"

# Copy the release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/scopestrength ./

RUN ln -s lib/scopestrength-0.1.0/priv /app/priv \
  && ln -s ../lib/scopestrength-0.1.0/priv /app/bin/priv \
  && mkdir -p /app/priv/static/uploads \
  && chown nobody:root /app/priv/static/uploads

USER nobody

CMD ["/app/bin/server"]
