# Build stage
FROM rust:alpine AS builder

# Install build dependencies
RUN apk add --no-cache musl-dev

WORKDIR /usr/src/dhl

# 1. Copy only the dependency manifests
COPY Cargo.toml Cargo.lock ./

# 2. Create a dummy source file to build dependencies
RUN mkdir src && echo "fn main() {}" > src/main.rs && \
    cargo build --release && \
    rm -rf src

# 3. Now copy the real source code
COPY src ./src

# 4. Build the actual application
# We touch the main file to ensure cargo rebuilds it
RUN touch src/main.rs && cargo build --release

# Final stage
FROM alpine:latest

# Set environment variable to signal the app it's running in Docker
ENV DOCKER_CONTAINER=true

# Install runtime dependencies
RUN apk add --no-cache ca-certificates

WORKDIR /app

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

# Volume for configuration and response files
VOLUME ["/app"]

# Default port
EXPOSE 3000

CMD ["dhl"]
