# syntax=docker/dockerfile:1

# --- planner: capture the dependency graph for caching ---
FROM rust:1-slim-bookworm AS chef
RUN cargo install cargo-chef --locked
WORKDIR /app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

# --- builder: compile deps (cached), then the workspace ---
FROM chef AS builder
RUN apt-get update && apt-get install -y --no-install-recommends pkg-config && rm -rf /var/lib/apt/lists/*
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release -p server -p worker

# --- runtime: distroless + cc, server binary only ---
FROM gcr.io/distroless/cc-debian12 AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/lgtm-backend /usr/local/bin/server
COPY --from=builder /app/target/release/worker /usr/local/bin/worker
COPY migrations /app/migrations
# Build metadata surfaced by GET /healthz. Set by CI build-args (Spindles passes the
# VERSION-file version + TANGLED_SHA); defaults keep local builds sane. Only in the cheap
# runtime stage, so a new commit doesn't bust the Rust build cache.
ARG BUILD_VERSION=dev
ARG BUILD_COMMIT=unknown
ENV LGTM_BUILD_VERSION=$BUILD_VERSION \
    LGTM_BUILD_COMMIT=$BUILD_COMMIT
EXPOSE 8080
# Default: `server --role all` (migrate → serve HTTP + worker). Override the CMD for other
# roles WITHOUT an entrypoint override, e.g. seed the demo store:
#   docker run <image> --role seed
ENTRYPOINT ["/usr/local/bin/server"]
CMD ["--role", "all"]
