# Stage 1: Build the rust binary
FROM rust:1.88.0-bookworm AS builder

# Install supplementary dependencies that might be needed
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    protobuf-compiler \
    cmake \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

# Option: Copy in root workspace files and source crates
COPY . .

ARG FURUMI_VERSION=dev

# Build only the server for release
RUN FURUMI_VERSION=${FURUMI_VERSION} cargo build --release --bin furumi-server

# Stage 2: Create the minimal runtime image
FROM debian:bookworm-slim

# Install system dependencies needed at runtime (like OpenSSL if dynamically linked)
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -ms /bin/bash appuser
WORKDIR /home/appuser

# Copy the binary from the builder stage
COPY --from=builder /usr/src/app/target/release/furumi-server /usr/local/bin/furumi-server

USER appuser

# Expose ports: 50051 (gRPC) and 9090 (Metrics)
EXPOSE 50051
EXPOSE 9090

# Command to run the server
ENTRYPOINT ["furumi-server"]
