# syntax=docker/dockerfile:1.7
FROM node:22-bookworm-slim AS dependencies
WORKDIR /app
RUN apt-get update \
    && apt-get install -y --no-install-recommends python3 make g++ ca-certificates \
    && rm -rf /var/lib/apt/lists/*
COPY package.json package-lock.json .npmrc ./
RUN npm ci --omit=dev --no-audit --no-fund \
    && npm cache clean --force

FROM node:22-bookworm-slim AS runtime
ENV NODE_ENV=production \
    PORT=8124 \
    VENDOO_BIND_HOST=0.0.0.0 \
    VENDOO_DATA_DIR=/app/data \
    VENDOO_DB_PATH=/app/data/db/vendoo.db \
    VENDOO_UPLOADS_DIR=/app/data/uploads \
    VENDOO_BACKUP_DIR=/app/data/backups \
    VENDOO_OPERATIONS_DIR=/app/data/operations \
    VENDOO_LOG_DIR=/app/data/logs \
    VENDOO_CONFIG_PATH=/app/data/config/vendoo.env \
    VENDOO_MODULES_DIR=/app/data/modules \
    VENDOO_MODULE_STATE_PATH=/app/data/config/modules-state.json \
    VENDOO_INITIAL_DISABLED_MODULES=vendoo.flux-studio
WORKDIR /app
COPY --from=dependencies --chown=node:node /app/node_modules ./node_modules
COPY --chown=node:node . .
RUN mkdir -p /app/data/db /app/data/uploads /app/data/backups /app/data/operations /app/data/logs /app/data/config /app/data/modules \
    && chown -R node:node /app/data \
    && chmod +x /app/scripts/docker-entrypoint.sh
USER node
EXPOSE 8124
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
  CMD node -e "fetch('http://127.0.0.1:8124/readyz').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))"
ENTRYPOINT ["/app/scripts/docker-entrypoint.sh"]
CMD ["node", "bootstrap.mjs"]
