# Dockerfile for building pack binaries independently
#
# This Dockerfile builds native pack binaries (sensors, etc.) with GLIBC compatibility
# The binaries are built separately from service containers and placed in ./packs/
#
# Usage:
#   docker build -f docker/Dockerfile.pack-binaries -t attune-pack-builder .
#   docker create --name pack-binaries attune-pack-builder
#   docker cp pack-binaries:/build/pack-binaries/. ./packs/
#   docker rm pack-binaries
#
# Or use the provided script:
#   ./scripts/build-pack-binaries.sh

ARG RUST_VERSION=1.92
ARG DEBIAN_VERSION=bookworm

# ============================================================================
# Stage 1: Builder - Build pack binaries with GLIBC 2.36
# ============================================================================
FROM rust:${RUST_VERSION}-${DEBIAN_VERSION} AS builder

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

WORKDIR /build

# Increase rustc stack size to prevent SIGSEGV during release builds
ENV RUST_MIN_STACK=16777216

# Copy workspace configuration
COPY Cargo.toml Cargo.lock ./

# Copy all workspace member manifests (required for workspace resolution)
COPY crates/common/Cargo.toml ./crates/common/Cargo.toml
COPY crates/api/Cargo.toml ./crates/api/Cargo.toml
COPY crates/executor/Cargo.toml ./crates/executor/Cargo.toml
COPY crates/sensor/Cargo.toml ./crates/sensor/Cargo.toml
COPY crates/core-timer-sensor/Cargo.toml ./crates/core-timer-sensor/Cargo.toml
COPY crates/worker/Cargo.toml ./crates/worker/Cargo.toml
COPY crates/notifier/Cargo.toml ./crates/notifier/Cargo.toml
COPY crates/cli/Cargo.toml ./crates/cli/Cargo.toml

# Create dummy source files for workspace members (not being built)
RUN mkdir -p crates/api/src && echo "fn main() {}" > crates/api/src/main.rs
RUN mkdir -p crates/executor/src && echo "fn main() {}" > crates/executor/src/main.rs
RUN mkdir -p crates/executor/benches && echo "fn main() {}" > crates/executor/benches/context_clone.rs
RUN mkdir -p crates/sensor/src && echo "fn main() {}" > crates/sensor/src/main.rs
RUN mkdir -p crates/worker/src && echo "fn main() {}" > crates/worker/src/main.rs
RUN mkdir -p crates/notifier/src && echo "fn main() {}" > crates/notifier/src/main.rs
RUN mkdir -p crates/cli/src && echo "fn main() {}" > crates/cli/src/main.rs

# Copy SQLx metadata for compile-time query checking
COPY .sqlx/ ./.sqlx/

# Copy only the source code needed for pack binaries
COPY crates/common/ ./crates/common/
COPY crates/core-timer-sensor/ ./crates/core-timer-sensor/

# Build pack binaries with BuildKit cache mounts
# These binaries will have GLIBC 2.36 compatibility (Debian Bookworm)
# - registry/git use sharing=shared (cargo handles concurrent access safely)
# - target uses dedicated cache for pack binaries (separate from service builds)
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=shared \
    --mount=type=cache,target=/usr/local/cargo/git,sharing=shared \
    --mount=type=cache,target=/build/target,id=target-pack-binaries \
    mkdir -p /build/pack-binaries && \
    cargo build --release --bin attune-core-timer-sensor && \
    cp /build/target/release/attune-core-timer-sensor /build/pack-binaries/attune-core-timer-sensor

# Verify binaries were built successfully
RUN ls -lah /build/pack-binaries/ && \
    file /build/pack-binaries/attune-core-timer-sensor && \
    ldd /build/pack-binaries/attune-core-timer-sensor && \
    /build/pack-binaries/attune-core-timer-sensor --version || echo "Built successfully"

# ============================================================================
# Stage 2: Output - Minimal image with just the binaries
# ============================================================================
FROM scratch AS output

# Copy binaries to output stage
# Extract with: docker cp <container>:/pack-binaries/. ./packs/
COPY --from=builder /build/pack-binaries/ /pack-binaries/

# Default command (not used in FROM scratch)
CMD ["/bin/sh"]
