# 1. Builder Stage
FROM rust:1.92 AS builder

WORKDIR /app

# --- OPTIMIZATION: Dependency Caching ---
# 1. Create a dummy project to cache dependencies
RUN mkdir src
RUN echo "fn main() {}" > src/main.rs

# 2. Copy manifests
COPY Cargo.toml Cargo.lock ./

# 3. Build only dependencies (this layer is cached until Cargo.toml changes)
RUN cargo build --release

# 4. Now remove dummy source and copy your actual source
#    We 'touch' main.rs to ensure Cargo realizes it needs a rebuild
RUN rm -rf src
COPY . .
RUN touch src/main.rs

# 5. Build the actual application
RUN cargo build --release

# 2. Runtime Stage
FROM debian:bookworm-slim

WORKDIR /app

# Install OpenSSL & CA Certificates
RUN apt-get update && \
    apt-get install -y libssl3 ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Copy the binary from builder
# Note: Ensure the binary name matches 'package.name' in Cargo.toml ("server")
COPY --from=builder /app/target/release/server .

# Documentation for ports
EXPOSE 3000

# Run the binary
CMD ["./server"]