# Multi-stage Dockerfile for cross-compilation
# This image provides Rust + Node.js toolchain for building Rustelo applications

ARG RUST_VERSION={{rust_version}}
ARG NODE_VERSION={{node_version}}

# Build stage with full toolchain
FROM node:${NODE_VERSION}-slim as base

# Install system dependencies for Rust and cross-compilation
RUN apt-get update && apt-get install -y \
    curl \
    build-essential \
    pkg-config \
    libssl-dev \
    libsqlite3-dev \
    musl-tools \
    gcc-musl-dev \
    && rm -rf /var/lib/apt/lists/*

# Install Rust toolchain
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${RUST_VERSION}
ENV PATH="/root/.cargo/bin:${PATH}"

# Add cross-compilation targets
RUN rustup target add x86_64-unknown-linux-gnu
RUN rustup target add aarch64-unknown-linux-gnu
RUN rustup target add x86_64-unknown-linux-musl

# Install additional tools for Rustelo development
RUN cargo install wasm-pack --version {{wasm_pack_version}}
RUN cargo install trunk --version {{trunk_version}}
RUN cargo install just --version {{just_version}}

# Set up workspace
WORKDIR /workspace

# Install global Node.js dependencies commonly used by Rustelo projects
RUN npm install -g \
    pnpm@{{pnpm_version}} \
    @tailwindcss/cli@{{tailwind_version}} \
    sass@{{sass_version}}

# Create volume directories for persistent storage
RUN mkdir -p /workspace/target /workspace/node_modules

# Copy cross-build script
COPY scripts/cross-build.sh /usr/local/bin/cross-build
RUN chmod +x /usr/local/bin/cross-build

# Set default environment variables
ENV CARGO_HOME=/root/.cargo
ENV CARGO_TARGET_DIR=/workspace/target
ENV NODE_ENV=production
ENV RUSTELO_MODE=cross

# Default command
CMD ["cross-build"]