# syntax=docker/dockerfile:1.7

# ============================================================================
# wbxStoreProvision — Remote Agent
# ----------------------------------------------------------------------------
# Tiny WebSocket client that proxies HTTP requests (Store Info Web today,
# anything else in the future) from an internal network back to the main
# wbxStoreProvision bot. Ships as a standalone container so it can run
# inside the segmented network where SIW lives.
#
# Build context is docker/remote-agent (self-contained — the agent doesn't
# need any of the bot's source or dependencies).
#
# Build:
#   docker build -f docker/remote-agent/Dockerfile \
#     -t wbxprov-remote-agent:latest docker/remote-agent
#
# Run:
#   docker run --rm -it \
#     --env-file docker/remote-agent/.env \
#     --name wbxprov-remote-agent \
#     wbxprov-remote-agent:latest
# ============================================================================

# ---- Stage 1: dependencies ------------------------------------------------
FROM node:22-alpine AS deps

WORKDIR /app

# Minimal manifest: ws + axios + dotenv. No lockfile — three stable deps.
COPY package.json ./package.json
RUN npm install --omit=dev --no-audit --no-fund && npm cache clean --force

# ---- Stage 2: runtime -----------------------------------------------------
FROM node:22-alpine AS runtime

# tini reaps zombies and forwards signals correctly so `docker stop` reaches
# Node's SIGTERM handler for a clean websocket close.
RUN apk add --no-cache tini

WORKDIR /app
USER node

COPY --chown=node:node --from=deps /app/node_modules ./node_modules
COPY --chown=node:node package.json ./package.json
COPY --chown=node:node remoteAgent.js ./remoteAgent.js

ENV NODE_ENV=production

# The agent is a WebSocket CLIENT — it doesn't listen on any port, so we
# intentionally skip EXPOSE.

ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "remoteAgent.js"]
