# Optimized Docker image for Woodpecker CI
# Reduces size from 2.47GB to ~800MB using multi-stage build
#
# Build: docker build -t your-registry/typedialog-ci:latest -f .woodpecker/Dockerfile.optimized .
# Push: docker push your-registry/typedialog-ci:latest

# Stage 1: Builder - compile Rust tools
FROM rust:1.92-slim AS builder

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

# Install Rust tools
RUN cargo install \
    cargo-audit \
    cargo-deny \
    cargo-sbom \
    nickel-lang-cli \
    nu \
    --locked

# Stage 2: Final image - copy only binaries
FROM rust:1.92-slim

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

# Install just (pre-compiled binary)
RUN curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | bash -s -- --to /usr/local/bin

# Install Rust components
RUN rustup component add clippy rustfmt

# Copy compiled binaries from builder stage
COPY --from=builder /usr/local/cargo/bin/cargo-audit /usr/local/cargo/bin/
COPY --from=builder /usr/local/cargo/bin/cargo-deny /usr/local/cargo/bin/
COPY --from=builder /usr/local/cargo/bin/cargo-sbom /usr/local/cargo/bin/
COPY --from=builder /usr/local/cargo/bin/nickel /usr/local/cargo/bin/
COPY --from=builder /usr/local/cargo/bin/nu /usr/local/cargo/bin/

# Set working directory
WORKDIR /workspace

# Verify installations
RUN just --version && \
    cargo --version && \
    cargo audit --version && \
    cargo deny --version && \
    cargo sbom --version && \
    nickel --version && \
    nu --version
