FROM rust:1.90-bookworm AS chef
WORKDIR /app
RUN cargo install cargo-chef

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

FROM chef AS builder
ENV SQLX_OFFLINE=true
COPY --from=planner /app/recipe.json /app/recipe.json
RUN cargo chef cook --release --recipe-path /app/recipe.json
COPY . .
RUN cargo build --release --bin data-persistence-service-server

FROM debian:bookworm-slim AS runtime
WORKDIR /app
RUN groupadd --system --gid 1001 appuser && \
    useradd --system --uid 1001 --gid 1001 appuser
USER appuser
COPY --from=builder /app/target/release/data-persistence-service-server /usr/local/bin/data-persistence-service-server
COPY ./migrations ./migrations
ENV HOST=0.0.0.0
ENV PORT=3000
EXPOSE 3000
ENTRYPOINT ["/usr/local/bin/data-persistence-service-server"]
