# Synor Storage Node Dockerfile
# Multi-stage build for optimized production image

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

WORKDIR /build

# Install dependencies for libp2p and rocksdb
RUN apt-get update && apt-get install -y \
    cmake \
    libclang-dev \
    libssl-dev \
    pkg-config \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Copy workspace files
COPY Cargo.toml Cargo.lock ./
COPY crates/ ./crates/

# Build the storage node binary
RUN cargo build --release -p synor-storage --bin synor-storage-node

# Stage 2: Runtime
FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Create non-root user
RUN useradd -m -u 1000 synor

# Create data directories
RUN mkdir -p /data/storage /config && chown -R synor:synor /data /config

WORKDIR /app

# Copy the built binary
COPY --from=builder /build/target/release/synor-storage-node /app/synor-storage-node

# Copy configuration template
COPY docker/storage-node/config.toml /config/config.toml

# Make binary executable
RUN chmod +x /app/synor-storage-node && chown synor:synor /app/synor-storage-node

USER synor

# Storage node ports
# 4001: P2P (libp2p)
# 5001: API (internal)
# 8080: Gateway (HTTP)
EXPOSE 4001 5001 8080

# Environment defaults
ENV RUST_LOG=info
ENV DATA_DIR=/data/storage
ENV GATEWAY_ENABLED=true
ENV GATEWAY_ADDR=0.0.0.0:8080

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

# Data volume
VOLUME ["/data/storage"]

# Entry point
ENTRYPOINT ["/app/synor-storage-node"]
CMD ["--config", "/config/config.toml", "--data-dir", "/data/storage"]
