# Multi-stage base images for Towerops Phoenix app
# Pre-bakes all the setup from k8s/Dockerfile so app deploys are faster
#
# This image is designed to be rebuilt regularly to get latest security updates

ARG ELIXIR_VERSION=1.19.4
ARG OTP_VERSION=28.3
ARG DEBIAN_VERSION=trixie-20251229-slim

#
# BUILDER BASE IMAGE
# Pre-installs build dependencies, hex, and rebar so every deploy is faster
#
FROM docker.io/hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION} AS builder

LABEL maintainer="towerops"
LABEL description="Elixir builder base with pre-installed build tools"

# Set parallel compilation
ENV MIX_OS_DEPS_COMPILE_PARTITION_COUNT=6

# Install build dependencies (matches k8s/Dockerfile builder stage exactly)
# This step takes ~30 seconds on every deploy, so we bake it in
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
       build-essential \
       git \
    && rm -rf /var/lib/apt/lists/*

# Install hex + rebar (matches k8s/Dockerfile exactly)
# This step takes ~10 seconds on every deploy, so we bake it in
RUN mix local.hex --force \
    && mix local.rebar --force

# Set build ENV
ENV MIX_ENV="prod"

WORKDIR /app

#
# RUNTIME BASE IMAGE
# Pre-installs runtime dependencies and configures locale
#
FROM docker.io/debian:${DEBIAN_VERSION} AS runtime

LABEL maintainer="towerops"
LABEL description="Debian 13 runtime base with pre-installed system packages and locale"

# Install runtime dependencies (matches k8s/Dockerfile runner stage exactly)
# This step takes ~30-60 seconds on every deploy, so we bake it in
RUN apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
       libstdc++6 \
       openssl \
       libncurses6 \
       locales \
       ca-certificates \
       iputils-ping \
    && rm -rf /var/lib/apt/lists/*

# Configure locale (matches k8s/Dockerfile exactly)
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

# Set up /app directory (matches k8s/Dockerfile exactly)
WORKDIR "/app"
RUN chown nobody /app
