# StemeDB API Docker Build
#
# Multi-stage build for the stemedb-api binary.
# Produces a minimal Debian-based image with just the compiled binary.

# Stage 1: Build the Rust binary
# Use latest Rust for compatibility with newer crates
FROM rust:bookworm AS builder

WORKDIR /app

# Copy manifests first for better layer caching
COPY Cargo.toml Cargo.lock ./

# Copy workspace members
COPY crates/ crates/
COPY applications/ applications/
COPY sdk/ sdk/

# Build release binary (only stemedb-api)
RUN cargo build --release -p stemedb-api

# Stage 2: Runtime image
FROM debian:bookworm-slim

# Install runtime dependencies
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
    && rm -rf /var/lib/apt/lists/*

# Copy the binary from builder
COPY --from=builder /app/target/release/stemedb-api /usr/local/bin/stemedb-api

# Create data directories
RUN mkdir -p /data/wal /data/db

# Set environment defaults
ENV STEMEDB_WAL_DIR=/data/wal \
    STEMEDB_DB_DIR=/data/db \
    STEMEDB_BIND_ADDR=0.0.0.0:18180 \
    RUST_LOG=stemedb_api=info

# Expose the API port
EXPOSE 18180

# Health check
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:18180/v1/health || exit 1

# Run the API server
CMD ["stemedb-api"]
