FROM rust:latest AS builder

WORKDIR /app

# Install ZMQ and protobuf dependencies
RUN apt-get update && apt-get install -y \
    libzmq3-dev \
    pkg-config \
    protobuf-compiler \
    && rm -rf /var/lib/apt/lists/*

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

# Copy build script and protobuf files (required for build)
COPY build.rs ./
COPY protobuf ./protobuf

# Copy source code
COPY src ./src

# Build application (includes dependencies)
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

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

WORKDIR /app

# Copy binary from builder
COPY --from=builder /app/target/release/relay /app/relay

# Create config directory
RUN mkdir -p /config

# Set environment
ENV RUST_LOG=relay=info

# Expose ports
# 5555: Ingestor work queue (PUB)
# 5556: Ingestor response (ROUTER)
# 5558: Market data publication (XPUB)
# 5559: Client requests (ROUTER)
EXPOSE 5555 5556 5558 5559

CMD ["/app/relay"]
