# ── Stage 1: build ────────────────────────────────────────────────────────────
FROM rust:1-slim AS builder

WORKDIR /app

# Cache dependency compilation separately from source changes.
COPY Cargo.toml Cargo.lock ./

# Real build
COPY src    ./src
COPY static ./static
RUN touch src/lib.rs src/main.rs \
    && cargo test \
    && cargo build --release

# ── Stage 2: runtime ──────────────────────────────────────────────────────────
FROM eclipse-temurin:21-jre-jammy

ARG PLANTUML_VERSION=1.2024.7

RUN apt-get update \
    && apt-get install -y --no-install-recommends wget graphviz \
    && wget -q -O /opt/plantuml.jar \
       "https://github.com/plantuml/plantuml/releases/download/v${PLANTUML_VERSION}/plantuml-${PLANTUML_VERSION}.jar" \
    && apt-get purge -y wget \
    && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/*

COPY --from=builder /app/target/release/pumlv /usr/local/bin/pumlv

EXPOSE 8080
ENV PLANTUML_JAR=/opt/plantuml.jar \
    PUMLV_ROOT=/data

ENTRYPOINT ["pumlv"]
