# Multi-stage Dockerfile for Rustelo application
FROM rust:1.75-slim-bullseye as builder

# Build arguments for feature selection
ARG CARGO_FEATURES="production"
ARG NO_DEFAULT_FEATURES="true"

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

# Install Node.js and npm for frontend builds
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
    && apt-get install -y nodejs

# Install cargo-leptos for building the application
RUN cargo install cargo-leptos --version 0.2.20

# Set working directory
WORKDIR /app

# Copy package.json and install node dependencies first (for better caching)
COPY package.json package-lock.json* ./
RUN npm ci --only=production

# Copy Cargo files for dependency caching
COPY Cargo.toml Cargo.lock ./
COPY server/Cargo.toml ./server/
COPY client/Cargo.toml ./client/
COPY shared/Cargo.toml ./shared/

# Create dummy source files to build dependencies
RUN mkdir -p server/src client/src shared/src && \
    echo "fn main() {}" > server/src/main.rs && \
    echo "pub fn lib() {}" > server/src/lib.rs && \
    echo "pub fn lib() {}" > client/src/lib.rs && \
    echo "pub fn lib() {}" > shared/src/lib.rs

# Build dependencies
RUN cargo build --release --bin server
RUN cargo leptos build --release

# Remove dummy source files
RUN rm -rf server/src client/src shared/src

# Copy the actual source code
COPY . .

# Build the application with specified features
RUN if [ "$NO_DEFAULT_FEATURES" = "true" ]; then \
        cargo leptos build --release --features "$CARGO_FEATURES" --no-default-features; \
    else \
        cargo leptos build --release --features "$CARGO_FEATURES"; \
    fi

# Runtime stage
FROM debian:bullseye-slim

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

# Create app user
RUN useradd -m -u 1000 app

# Set working directory
WORKDIR /app

# Copy built application from builder stage
COPY --from=builder /app/target/release/server /app/server
COPY --from=builder /app/target/site /app/target/site
COPY --from=builder /app/public /app/public

# Copy configuration files
COPY --from=builder /app/config.toml /app/config.toml
COPY --from=builder /app/config.prod.toml /app/config.prod.toml

# Copy templates and other runtime assets
COPY --from=builder /app/templates /app/templates
COPY --from=builder /app/migrations /app/migrations

# Create necessary directories
RUN mkdir -p /app/logs /app/uploads /app/tmp /app/cache /app/data /app/backups

# Change ownership to app user
RUN chown -R app:app /app

# Switch to app user
USER app

# Expose port
EXPOSE 3030

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

# Set environment variables
ENV RUST_LOG=info
ENV ENVIRONMENT=production

# Run the application
CMD ["./server"]
