# Build image — stays at 1.19.5 until hexpm publishes 1.20.0 stable image.
# Dev shell uses 1.20.0-rc.4 via flake.nix.
FROM hexpm/elixir:1.19.5-erlang-28.3.1-debian-trixie-20260202-slim AS build

RUN apt-get update -y && apt-get install -y --no-install-recommends \
    build-essential git npm ca-certificates \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

WORKDIR /app

ENV MIX_ENV=prod

RUN mix local.hex --force && mix local.rebar --force

# Umbrella root deps
COPY mix.exs mix.lock ./
COPY apps/centralcloud_core/mix.exs ./apps/centralcloud_core/
COPY apps/centralcloud_my/mix.exs ./apps/centralcloud_my/
COPY apps/centralcloud_ops/mix.exs ./apps/centralcloud_ops/

RUN mix deps.get --only prod && mix deps.compile

COPY config ./config
COPY apps/centralcloud_core ./apps/centralcloud_core
COPY apps/centralcloud_my ./apps/centralcloud_my

RUN mix compile

# Build release for centralcloud_my only
COPY rel ./rel
RUN mix release centralcloud_my

FROM debian:trixie-slim AS app

RUN apt-get update -y && apt-get install -y --no-install-recommends \
    libstdc++6 openssl libncurses6 locales ca-certificates \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

RUN useradd --create-home --shell /bin/sh app

WORKDIR /app
RUN chown app:app /app

USER app

COPY --from=build --chown=app:app /app/_build/prod/rel/centralcloud_my ./

# Convenience symlink for migrate init container
RUN ln -s /app/bin/centralcloud_my /app/bin/migrate || true

ENV HOME=/app
ENV PHX_SERVER=true

CMD ["/app/bin/server"]
