# Multi-stage Dockerfile for project-lifecycle-manager
# Builds all services: API, CLI, Dashboard, TUI

# Stage 1: Builder
FROM rust:1.91 as builder

WORKDIR /app

# Copy Cargo files
COPY Cargo.toml Cargo.lock ./
COPY crates ./crates

# Build release binaries
RUN cargo build --release -p lifecycle-api
RUN cargo build --release -p lifecycle-cli
RUN cargo build --release -p lifecycle-dashboard

# Stage 2: Runtime - API Server
FROM debian:bookworm-slim as api-runtime

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

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

EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:3000/health || exit 1

ENTRYPOINT ["lifecycle-server"]

# Stage 3: Runtime - Dashboard Server
FROM debian:bookworm-slim as dashboard-runtime

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

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

EXPOSE 3001

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:3001/ || exit 1

ENTRYPOINT ["lifecycle-dashboard"]

# Stage 4: Runtime - CLI
FROM debian:bookworm-slim as cli-runtime

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

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

ENTRYPOINT ["project-lifecycle"]
