FROM lukemathwalker/cargo-chef:latest-rust-1.97.0-slim-bookworm AS chef
WORKDIR app

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

FROM chef AS builder

# Ensure working C compile setup (not installed by default in arm64 images)
RUN apt update && apt install build-essential -y

COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json

COPY . .
RUN cargo build --release --bin atuin-server

FROM debian:bookworm-20260713-slim AS runtime
LABEL org.opencontainers.image.source="https://github.com/atuinsh/atuin" \
  org.opencontainers.image.url="https://atuin.sh" \
  org.opencontainers.image.licenses="MIT"

RUN useradd -c 'atuin user' atuin && mkdir /config && chown atuin:atuin /config
# ca-certificates for webhooks to work, curl for the healthcheck
RUN apt update && apt install --no-install-recommends ca-certificates curl -y && rm -rf /var/lib/apt/lists/*
WORKDIR app

USER atuin

ENV TZ=Etc/UTC
ENV RUST_LOG=atuin_server=info
ENV ATUIN_CONFIG_DIR=/config

COPY --from=builder /app/target/release/atuin-server /usr/local/bin

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD curl -fsS -o /dev/null "http://localhost:${ATUIN_PORT:-8888}/healthz"

ENTRYPOINT ["/usr/local/bin/atuin-server"]
