# ─── Stage 1: Builder ────────────────────────────────────────────────────────
FROM rust:1.96 AS builder

WORKDIR /app

# System deps needed to compile OpenSSL and link the binary
RUN apt-get update && apt-get install -y \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

# Add the WASM compilation target required by cargo-leptos
RUN rustup target add wasm32-unknown-unknown

# Install cargo-leptos (pinned for reproducibility).
# Done BEFORE copying source so this expensive step is cached
# as a separate layer and only re-runs when the Dockerfile changes.
RUN cargo install cargo-leptos --locked

# Copy the full source tree (respects .dockerignore).
# Everything above this line is layer-cached by Docker / the CI cache.
COPY . .

# Build server binary + WASM/CSS/JS site bundle in release mode
RUN cargo leptos build --release

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

WORKDIR /app

RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Server binary
COPY --from=builder /app/target/release/toollist ./toollist

# JS / WASM / CSS site bundle (default site-root = "target/site")
COPY --from=builder /app/target/site ./target/site

# Cargo.toml is read at startup by leptos get_configuration()
COPY --from=builder /app/Cargo.toml ./Cargo.toml

# Seed data – in production this directory is mounted as a volume
COPY --from=builder /app/data ./data

EXPOSE 3000

ENV LEPTOS_ENV=PROD

CMD ["./toollist"]
