# Context: project root (needed so build.rs can access proto/)
FROM rust:slim AS builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    protobuf-compiler \
    pkg-config \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /src

# Copy proto first (referenced by build.rs as ../proto/...)
COPY proto/ ./proto/

# Cache dependencies: copy manifests, build with dummy main, then discard.
COPY agent/Cargo.toml agent/Cargo.lock ./agent/
COPY agent/build.rs ./agent/
WORKDIR /src/agent
RUN mkdir src && echo "fn main(){}" > src/main.rs && \
    cargo build --release && \
    rm -rf src

# Full build.
COPY agent/src ./src
RUN touch src/main.rs && cargo build --release

# ── Runtime ───────────────────────────────────────────────────────────────────
FROM debian:bookworm-slim

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

COPY --from=builder /src/agent/target/release/containarr-agent /usr/local/bin/containarr-agent

ENTRYPOINT ["containarr-agent"]
