FROM node:20-bookworm-slim AS frontend-builder
WORKDIR /src/frontend

COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci

COPY frontend/ ./
RUN npm run build

FROM rust:1.85-bookworm AS backend-builder
WORKDIR /src/backend

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

COPY backend/ ./
RUN cargo build --release

FROM debian:bookworm-slim
WORKDIR /app

RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates ripgrep libdbus-1-3 git openssh-client \
    && rm -rf /var/lib/apt/lists/*

COPY --from=backend-builder /src/backend/target/release/ironpad /app/ironpad
COPY --from=frontend-builder /src/frontend/dist /app/static

RUN mkdir -p /app/data

ENV IRONPAD_HOST=0.0.0.0
ENV IRONPAD_PORT=3000
ENV IRONPAD_DISABLE_TRAY=1
ENV IRONPAD_DATA_DIR=/app/data
ENV RUST_LOG=info

EXPOSE 3000
VOLUME ["/app/data"]

CMD ["./ironpad"]
