# RustyTorch++ Production Container
# Multi-stage build for minimal runtime image

# =============================================================================
# Stage 1: Build environment
# =============================================================================
FROM rust:1.83-bookworm AS builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build

# Copy workspace configuration first for better caching
COPY Cargo.toml Cargo.lock ./

# Copy all crate manifests (for dependency resolution)
COPY crates/ crates/
COPY demos/ demos/

# Build dependencies first (cached layer)
# Create dummy source files to build dependencies
RUN find crates -name "*.rs" -type f -delete && \
    find demos -name "*.rs" -type f -delete && \
    for dir in $(find crates demos -name "Cargo.toml" -exec dirname {} \;); do \
        mkdir -p "$dir/src" && \
        echo "fn main() {}" > "$dir/src/main.rs" 2>/dev/null || true && \
        echo "pub fn lib() {}" > "$dir/src/lib.rs" 2>/dev/null || true; \
    done && \
    cargo build --release -p rtx-serving-api 2>/dev/null || true && \
    rm -rf crates demos

# Copy actual source code
COPY crates/ crates/
COPY demos/ demos/

# Build the serving API binary
RUN cargo build --release -p rtx-serving-api && \
    strip /build/target/release/rtx-serving-api

# =============================================================================
# Stage 2: Runtime environment
# =============================================================================
FROM debian:bookworm-slim AS runtime

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    && rm -rf /var/lib/apt/lists/* \
    && groupadd -r rtx && useradd -r -g rtx rtx

WORKDIR /app

# Copy binary from builder
COPY --from=builder /build/target/release/rtx-serving-api /app/rtx-serving-api

# Create directories for configuration and models
RUN mkdir -p /app/config /app/models /app/cache && \
    chown -R rtx:rtx /app

# Switch to non-root user
USER rtx

# Environment variables
ENV RTX_LOG_LEVEL=info \
    RTX_HOST=0.0.0.0 \
    RTX_PORT=8080 \
    RTX_MODEL_PATH=/app/models \
    RTX_CACHE_PATH=/app/cache \
    RUST_BACKTRACE=1

# Expose HTTP port
EXPOSE 8080

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

# Run the server
ENTRYPOINT ["/app/rtx-serving-api"]
CMD ["--host", "0.0.0.0", "--port", "8080"]
