FROM node:20.19-bullseye-slim AS base

# Set environment variables early for better layer caching
# Memory optimizations for low-RAM servers (2GB):
# - Limit Node.js heap to 1536MB to leave room for system
# - Disable NX daemon and cloud to reduce overhead
ENV LANG=en_US.UTF-8 \
    LANGUAGE=en_US:en \
    LC_ALL=en_US.UTF-8 \
    NX_DAEMON=false \
    NX_NO_CLOUD=true \
    NODE_OPTIONS="--max-old-space-size=1536"

# Install all system dependencies in a single layer with cache mounts
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        openssh-client \
        python3 \
        python3-pip \
        ruby \
        g++ \
        build-essential \
        git \
        poppler-utils \
        poppler-data \
        procps \
        locales \
        locales-all \
        unzip \
        curl \
        ca-certificates \
        libcap-dev && \
    yarn config set python /usr/bin/python3

# Install Bun using npm (more reliable than GitHub downloads)
RUN npm install -g bun@1.3.1

RUN bun --version

# Install global npm packages in a single layer
RUN --mount=type=cache,target=/root/.npm \
    npm install -g --no-fund --no-audit \
    node-gyp \
    npm@9.9.3 \
    pm2@6.0.10 \
    typescript@4.9.4

# Install isolated-vm globally (needed for sandboxes)
RUN --mount=type=cache,target=/root/.bun/install/cache \
    cd /usr/src && bun install isolated-vm@5.0.1

### STAGE 1: Build ###
FROM base AS build

WORKDIR /usr/src/app

# Copy only dependency files first for better layer caching
COPY .npmrc package.json bun.lock ./

# Install all dependencies
RUN --mount=type=cache,target=/root/.bun/install/cache \
    bun install

# Copy source code after dependency installation
COPY . .

# Build all projects including custom pieces
RUN npx nx run-many --target=build --projects=react-ui,server-api,pieces-smoothschedule,pieces-python-code,pieces-ruby-code,pieces-interfaces --configuration production --parallel=2 --skip-nx-cache

# Install production dependencies only for the backend API
RUN --mount=type=cache,target=/root/.bun/install/cache \
    cd dist/packages/server/api && \
    bun install --production --frozen-lockfile

# Install dependencies for custom pieces
RUN --mount=type=cache,target=/root/.bun/install/cache \
    cd dist/packages/pieces/community/smoothschedule && \
    bun install --production && \
    cd ../python-code && \
    bun install --production && \
    cd ../ruby-code && \
    bun install --production && \
    cd ../interfaces && \
    bun install --production

### STAGE 2: Run ###
FROM base AS run

WORKDIR /usr/src/app

# Install Nginx, gettext, and PostgreSQL client in a single layer with cache mount
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update && \
    apt-get install -y --no-install-recommends nginx gettext postgresql-client

# Copy static configuration files first (better layer caching)
COPY nginx.react.conf /etc/nginx/nginx.conf
COPY --from=build /usr/src/app/packages/server/api/src/assets/default.cf /usr/local/etc/isolate
COPY docker-entrypoint.sh .
COPY custom-pieces-metadata.sql .
COPY publish-pieces.sh .

# Create all necessary directories in one layer
# Also create symlink for AP_DEV_PIECES to find pieces in dist folder
# Structure: /packages/pieces/community -> /dist/packages/pieces/community
RUN mkdir -p \
    /usr/src/app/dist/packages/server \
    /usr/src/app/dist/packages/engine \
    /usr/src/app/dist/packages/shared \
    /usr/src/app/dist/packages/pieces \
    /usr/src/app/packages/pieces && \
    ln -sf /usr/src/app/dist/packages/pieces/community /usr/src/app/packages/pieces/community && \
    chmod +x docker-entrypoint.sh publish-pieces.sh

# Copy built artifacts from build stage
COPY --from=build /usr/src/app/LICENSE .
COPY --from=build /usr/src/app/dist/packages/engine/ ./dist/packages/engine/
COPY --from=build /usr/src/app/dist/packages/server/ ./dist/packages/server/
COPY --from=build /usr/src/app/dist/packages/shared/ ./dist/packages/shared/
COPY --from=build /usr/src/app/dist/packages/pieces/ ./dist/packages/pieces/
# Note: Don't copy /packages folder - it triggers dev piece auto-detection
# The pre-built pieces in dist/packages/pieces/ are sufficient for production

# Copy frontend files to Nginx document root
COPY --from=build /usr/src/app/dist/packages/react-ui /usr/share/nginx/html/

LABEL service=activepieces

ENTRYPOINT ["./docker-entrypoint.sh"]
EXPOSE 80