# Guildhall production image — Elixir/Phoenix umbrella release.
# Multi-stage: builder produces a mix release; runtime is a slim debian
# carrying only the OTP release + runtime libs.
#
# Build context: the guildhall umbrella root.
# Target registry: git.guildhouse.dev/tking/guildhall:<tag>

# ---------- Stage 1: builder ---------------------------------------------
FROM git.guildhouse.dev/guildhouse/substrate/elixir-builder:1.17.3 AS builder

ENV MIX_ENV=prod \
    LANG=C.UTF-8 \
    LC_ALL=C.UTF-8

WORKDIR /app

COPY mix.exs mix.lock ./
COPY config/config.exs config/prod.exs config/runtime.exs config/
COPY apps/guildhall_chronicle/mix.exs apps/guildhall_chronicle/
COPY apps/guildhall_graph_bridge/mix.exs apps/guildhall_graph_bridge/
COPY apps/guildhall_ops_db/mix.exs apps/guildhall_ops_db/
COPY apps/guildhall_orchestrator/mix.exs apps/guildhall_orchestrator/
COPY apps/guildhall_web/mix.exs apps/guildhall_web/

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

COPY apps/ apps/

COPY apps/guildhall_web/assets apps/guildhall_web/assets
RUN cd apps/guildhall_web && \
    mix assets.setup && \
    mix assets.deploy

RUN mix compile --warnings-as-errors && \
    mix release --overwrite

# ---------- Stage 2: runtime (Wolfi — golden elixir-runtime) -------------
FROM git.guildhouse.dev/guildhouse/substrate/elixir-runtime:latest AS runtime

WORKDIR /app

COPY --from=builder --chown=substrate:substrate /app/_build/prod/rel/guildhall /app

USER 1000

ENV HOME=/app \
    PHX_SERVER=true \
    PORT=4000

EXPOSE 4000

HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
    CMD curl -fsS http://localhost:4000/health || exit 1

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/app/bin/guildhall", "start"]
