# Synor Compute Node Dockerfile
# Heterogeneous compute orchestration for CPU/GPU/TPU/NPU/LPU

# =============================================================================
# Stage 1: Build Environment
# =============================================================================
FROM rust:1.85-bookworm AS builder

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

# Create app directory
WORKDIR /app

# Copy manifests first (for better caching)
COPY Cargo.toml Cargo.lock ./
COPY crates/ crates/
COPY apps/ apps/
COPY contracts/ contracts/
COPY sdk/ sdk/

# Build release binary for compute node
RUN cargo build --release -p synor-compute

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

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

# Create non-root user for security
RUN useradd --create-home --shell /bin/bash synor

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

# Note: synor-compute is a library, not binary
# The container serves as a compute worker runtime

# Create a simple entrypoint script
RUN echo '#!/bin/bash\n\
echo "Synor Compute Node v0.1.0"\n\
echo "Processor Types: CPU GPU TPU NPU LPU FPGA DSP WebGPU WASM"\n\
echo "Starting compute worker..."\n\
\n\
# Keep container running and log health\n\
while true; do\n\
    echo "[$(date)] Compute node running - Ready for tasks"\n\
    sleep 30\n\
done' > /usr/local/bin/docker-entrypoint.sh && \
    chmod +x /usr/local/bin/docker-entrypoint.sh

# Switch to non-root user
USER synor

# Set working directory
WORKDIR /home/synor

# Expose ports
# Compute API
EXPOSE 17200
# Worker communication
EXPOSE 17201
# Metrics
EXPOSE 17202

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

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

# Default command
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
