# ── Stage 1: Build the Rust extension ────────────────────────────
FROM python:3.13-slim AS builder

RUN apt-get update && \
    apt-get install -y --no-install-recommends curl build-essential pkg-config libssl-dev cmake && \
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
    rm -rf /var/lib/apt/lists/*

ENV PATH="/root/.cargo/bin:${PATH}"

WORKDIR /app

# Copy only what maturin needs so Docker can cache layers
COPY Cargo.toml pyproject.toml README.md ./
COPY rust/ rust/
COPY oxidizer/ oxidizer/

RUN pip install --no-cache-dir "maturin[patchelf]" && \
    maturin build --release --out dist

# ── Stage 2: Runtime image ───────────────────────────────────────
FROM python:3.13-slim

WORKDIR /app

# Install the built wheel (includes the compiled Rust extension)
COPY --from=builder /app/dist/*.whl /tmp/
COPY requirements.txt .
RUN pip install --no-cache-dir /tmp/*.whl && \
    pip install --no-cache-dir -r requirements.txt && \
    rm -f /tmp/*.whl

# Copy the rest of the application (entrypoints, workers, etc.)
COPY examples/ /tmp/examples/
RUN find /tmp/examples -name '*.py' -exec cp {} ./ \; && rm -rf /tmp/examples

ENV PYTHONUNBUFFERED=1

# Default entrypoint — overridden by compose command
CMD ["python", "main.py"]
