# Multi-Stage Dockerfile for {{project_name}}
#
# Auto-generated by `metabots init`
# Customize as needed, or regenerate with `metabots docker generate`
#
# Supports isomorphic deployment (robot, remote, dev targets)
# Build args control which nodes are included

# ============================================================================
# Base: Common dependencies
# ============================================================================
FROM rust:1.75 as base

WORKDIR /workspace

# Copy monorepo structure (if in monorepo)
{{#if is_monorepo}}
COPY Cargo.toml Cargo.lock ./
COPY packages/ packages/
{{/if}}

# ============================================================================
# Development: Includes build tools for hot reload
# ============================================================================
FROM base as development

# Install development tools
RUN cargo install cargo-watch

# Copy project manifests
COPY {{project_path}}/Cargo.toml {{project_path}}/
COPY {{project_path}}/build.rs {{project_path}}/
COPY {{project_path}}/metabots.json {{project_path}}/

# Create dummy source to satisfy cargo
# (Real source will be mounted as volumes)
RUN mkdir -p {{project_path}}/src && \
    echo "fn main() {}" > {{project_path}}/src/main.rs

# Pre-build dependencies for faster hot reload
WORKDIR /workspace/{{project_path}}
RUN cargo build 2>/dev/null || true

# Development entrypoint
CMD ["cargo", "run", "--", "run"]

# ============================================================================
# Builder: Compile production binary with run target selection
# ============================================================================
FROM base as builder

# Accept run target as build arg (robot, remote, or dev)
ARG RUN_TARGET=dev

# Copy entire project for build
COPY . .

# Build release binary with target-specific features
WORKDIR /workspace/{{project_path}}
RUN echo "Building for run target: ${RUN_TARGET}" && \
    cargo build --release --no-default-features --features "target-${RUN_TARGET}"

# ============================================================================
# Production: Robot target (minimal, hardware access)
# ============================================================================
FROM debian:bookworm-slim as production-robot

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

# Copy ONLY the binary
COPY --from=builder /workspace/target/release/{{binary_name}} /usr/local/bin/{{binary_name}}

# Create cache directory for cloud-synced updates
RUN mkdir -p /var/cache/metabots/{models,configs}

# Production entrypoint
CMD ["{{binary_name}}", "run"]

# Health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=10s \
  CMD {{binary_name}} list || exit 1

# ============================================================================
# Production: Remote target (ML dependencies + optional GPU)
# ============================================================================
FROM debian:bookworm-slim as production-remote

# Accept GPU configuration as build args
ARG ENABLE_GPU=false
ARG GPU_VENDOR=nvidia

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

# Install GPU-specific dependencies if enabled
RUN if [ "$ENABLE_GPU" = "true" ]; then \
        echo "Installing GPU support for: $GPU_VENDOR"; \
        if [ "$GPU_VENDOR" = "nvidia" ]; then \
            apt-get update && \
            apt-get install -y --no-install-recommends \
                cuda-cudart-12-0 \
                libcublas-12-0 \
                libcudnn8 \
            && rm -rf /var/lib/apt/lists/*; \
        elif [ "$GPU_VENDOR" = "amd" ]; then \
            apt-get update && \
            apt-get install -y rocm-runtime && \
            rm -rf /var/lib/apt/lists/*; \
        fi; \
    fi

# Copy ONLY the binary
COPY --from=builder /workspace/target/release/{{binary_name}} /usr/local/bin/{{binary_name}}

# Create directories for models, data, and logs
RUN mkdir -p \
    /var/cache/metabots/models \
    /var/cache/metabots/data \
    /var/log/{{project_name}}

# Set environment variables
ENV CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES:-0} \
    MODEL_CACHE_PATH=/var/cache/metabots/models \
    DATA_PATH=/var/cache/metabots/data

# Production entrypoint
CMD ["{{binary_name}}", "run"]

# Health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=10s \
  CMD {{binary_name}} list || exit 1

# ============================================================================
# Production: Dev target (all nodes for testing)
# ============================================================================
FROM debian:bookworm-slim as production-dev

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

# Copy ONLY the binary
COPY --from=builder /workspace/target/release/{{binary_name}} /usr/local/bin/{{binary_name}}

# Create cache directory
RUN mkdir -p /var/cache/metabots/{models,configs}

# Production entrypoint
CMD ["{{binary_name}}", "run"]

# Health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=10s \
  CMD {{binary_name}} list || exit 1
