FROM rust:1.85-bookworm AS deps

WORKDIR /app

COPY Cargo.toml Cargo.lock ./
COPY .sqlx ./.sqlx
COPY apps/admin-api/Cargo.toml apps/admin-api/Cargo.toml
COPY apps/mcp-server/Cargo.toml apps/mcp-server/Cargo.toml
COPY crates/crank-core/Cargo.toml crates/crank-core/Cargo.toml
COPY crates/crank-schema/Cargo.toml crates/crank-schema/Cargo.toml
COPY crates/crank-mapping/Cargo.toml crates/crank-mapping/Cargo.toml
COPY crates/crank-proto/Cargo.toml crates/crank-proto/Cargo.toml
COPY crates/crank-registry/Cargo.toml crates/crank-registry/Cargo.toml
COPY crates/crank-runtime/Cargo.toml crates/crank-runtime/Cargo.toml
COPY crates/crank-adapter-rest/Cargo.toml crates/crank-adapter-rest/Cargo.toml
COPY crates/crank-adapter-graphql/Cargo.toml crates/crank-adapter-graphql/Cargo.toml
COPY crates/crank-adapter-grpc/Cargo.toml crates/crank-adapter-grpc/Cargo.toml
COPY crates/crank-adapter-grpc/build.rs crates/crank-adapter-grpc/build.rs
COPY crates/crank-adapter-grpc/proto crates/crank-adapter-grpc/proto
COPY crates/crank-adapter-websocket/Cargo.toml crates/crank-adapter-websocket/Cargo.toml
COPY crates/crank-adapter-soap/Cargo.toml crates/crank-adapter-soap/Cargo.toml

RUN mkdir -p \
    apps/admin-api/src \
    apps/mcp-server/src \
    crates/crank-core/src \
    crates/crank-schema/src \
    crates/crank-mapping/src \
    crates/crank-proto/src \
    crates/crank-registry/src \
    crates/crank-runtime/src \
    crates/crank-adapter-rest/src \
    crates/crank-adapter-graphql/src \
    crates/crank-adapter-grpc/src \
    crates/crank-adapter-websocket/src \
    crates/crank-adapter-soap/src \
    && printf 'fn main() {}\n' > apps/admin-api/src/main.rs \
    && printf 'fn main() {}\n' > apps/mcp-server/src/main.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-core/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-schema/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-mapping/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-proto/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-registry/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-runtime/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-adapter-rest/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-adapter-graphql/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-adapter-grpc/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-adapter-websocket/src/lib.rs \
    && printf 'pub fn placeholder() {}\n' > crates/crank-adapter-soap/src/lib.rs

RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/usr/local/cargo/git/db \
    --mount=type=cache,target=/app/target \
    SQLX_OFFLINE=true cargo build --release -p admin-api

FROM rust:1.85-bookworm AS builder

WORKDIR /app

COPY Cargo.toml Cargo.lock ./
COPY .sqlx ./.sqlx
COPY apps ./apps
COPY crates ./crates

RUN --mount=type=cache,target=/usr/local/cargo/registry \
    --mount=type=cache,target=/usr/local/cargo/git/db \
    --mount=type=cache,target=/app/target \
    SQLX_OFFLINE=true cargo build --release -p admin-api \
    && cp /app/target/release/admin-api /tmp/admin-api

FROM debian:bookworm-slim

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

WORKDIR /app

COPY --from=builder /tmp/admin-api /usr/local/bin/admin-api

ENV CRANK_ADMIN_BIND=0.0.0.0:3001

EXPOSE 3001

CMD ["admin-api"]
